C Program to show an example of Communication using Pipe

 


C Program to show an example of Communication using Pipe
#include<stdio.h>
#include<unistd.h>
int main()
{
        printf("Starting the program\n");
        int n;
        char buf[14];
        int fd[2];
        printf("Create the pipe\n");
        pipe(fd);
        printf("Create the child process using fork\n");
        if(fork()==0)
        {
                close(fd[1]);
                printf("Child is running...\n");
                printf("Reading from the pipe\n");
                n=read(fd[0],buf,14);
                printf("After reading from the pipe\n");
                printf("The value in the buffer is: %s\n",buf);
        }
        else
        {
                close(fd[0]);
                printf("Parent process is running...\n");
                printf("Write to the pipe\n");
                write(fd[1],"Hai...hello\n",14);
                printf("'Hai...hello...' is write to the pipe...\nAfter writing to the pipe\n");
        }
}

Post a Comment

Previous Post Next Post