// CS22B1067 Skanda S Bhat
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>

int main()
{
    int pipe_fd[2];  
    char buffer[100];
    

    if (pipe(pipe_fd) == -1) {
        perror("Pipe creation failed");
        return 1;
    }
    
    int pid = fork();  
    if (pid < 0) {
        perror("Fork failed");
        return 1;
    }


    if (pid > 0) {
        close(pipe_fd[0]); 

       
        printf("Parent: Enter a message to send to the child: ");
        fgets(buffer, sizeof(buffer), stdin);
        
        
        write(pipe_fd[1], buffer, sizeof(buffer));
        printf("Parent: Message sent to child.\n");

        close(pipe_fd[1]);  

        wait(NULL); 
        printf("Parent: Child process finished.\n");
    }

    else {
        close(pipe_fd[1]);  
        

        read(pipe_fd[0], buffer, sizeof(buffer));
        printf("Child: Message received from parent: %s", buffer);

        close(pipe_fd[0]);
        printf("Child: Exiting.\n");
    }
    
    return 0;
}


