I'm learning about pipe() system call in Linux. Here is the code snippet I have question about:
#define MSGSIZE 16
char* msg1 = "hello, world #1";
char* msg2 = "hello, world #2";
char* msg3 = "hello, world #3";
int main()
{
char inbuf[MSGSIZE];
int p[2], i;
if (pipe(p) < 0)
exit(1);
int pid=fork();
if(pid==0){
sleep(2); ///Making parent process sleep so that child process moves ahead
write(p[1], msg1, MSGSIZE);
write(p[1], msg2, MSGSIZE);
write(p[1], msg3, MSGSIZE);
printf("Done writing");
}
else{
printf("child process\n");
for (i = 0; i < 3; i++) {
read(p[0], inbuf, MSGSIZE);
printf("%s\n", inbuf);
}
}
return 0;
}
I get output as follows
child process
hello, world #1
hello, world #2
hello, world #3
Done writing
As we see child process has started before write calls where completed. Here it seems that read call is waiting for something.I couldn't find docs in man pages regarding this behaviour. Can someone explain whats happening here in easy terms?