You are on page 1of 1

#include<stdio.

h>
#include<stdlib.h>
#include<string.h>
int main(int c,char *argv[])
{
int fd[2],nbytes;
pid_t childpid,pid;
char string[]="Hello world\n";
char readbuffer[80];
pipe(fd);
if((childpid=fork())==-1)
{
printf("fork error\n");
exit(1);
}
if(childpid==0)
{
close(fd[0]);
printf("child process id no is %d\n",pid);
write(fd[1],string,strlen(string)+1);
printf("wrote to the pipe & to the console\n");
printf("child process finished its work\n");
exit(0);
}
else
{
close(fd[1]);
printf("parent process id no is %d\n",pid);
nbytes=read(fd[0],readbuffer,sizeof(readbuffer));
write(1,readbuffer,sizeof(readbuffer));
printf("read from the pipe & write to the console\n");
printf("parent process finished its work\n");
}
}

You might also like