C Program to show an example of Direct Communication


 

C Program to show an example of Direct Communication

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>
int main(void)
{
int fd[2],nbytes;
pid_t childpid;
char string[40];
char readbuffer[80];
pipe(fd);
if((childpid=fork())==-1)
{
perror("fork");
exit(1);
}
if(childpid==0)
{
printf("Enter message to send into the parent process:");
scanf("%s",string);
close(fd[0]);
write(fd[1],string,(strlen(string)+1));
exit(0);
}
else
{
close(fd[1]);
nbytes=read(fd[0],readbuffer,sizeof(readbuffer));
printf("Recieved message from the child process:%s",readbuffer);
printf("\n");
}
return(0);
}

Post a Comment

Previous Post Next Post