Program To Demonstrate Inter Procession Communication Using Pipes

Aim: To create inter process communication using pipes.
These Linux program is used to perform inter Process communication using pipes. For string performance we include string.h and fcntl.h header file. And we set error Status as 1 .
Linux shell Source code Programming
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
int main()
{
int fd[2],p;
int status = 0;
int error_status = 1;
pid_t pid;
char msg[80],msg1[80];
bzero((char*) &msg1,sizeof(msg1));
printf("\n\t\t program to demonstrate inter process communication\n");
if((p=pipe(fd))==-1)
{
perror("\n\t unable to create pipes\n");
return(1);
}
pid=fork();
if(pid==0)
{
close(fd[0]);
printf("\n Inside child");
printf("\n\t Writing to pipe");
printf("\n\t Enter the message:");
scanf("%[^\n]s",msg);
if((write(fd[1],msg,strlen(msg)))==-1)
{
perror("\n\t unable to write to pipe");
return(1);
}
printf("\n\t Message is written\n");
close(fd[1]);
return(0);
}
else
{
close(fd[1]);
if((read(fd[0],msg1,sizeof(msg1)))==-1)
{
perror("\n\t unable to write to pipe");
return(1);
}
printf("\n Inside parent");
printf("\n\t Reading from pipe");
printf("\n\t Message read from pipe:%s",msg1);
close(fd[0]);
wait();
getchar();
}
}
OUTPUT: program to demonstrate inter process communication
Inside child
Writing to pipe
Enter the message: This is new mail
Message is written
Inside parent
Reading from pipe
Message read from pipe: This is new mail
CONCLUSION:
The Program c program demonstrates inter process communication using pipes.

Related post