Hello Members,
I am trying to understand the pipe() system call. I found this program online:
#include <unistd.h>
#include <stdio.h>
#define MSGSIZE 16
char msg1[] = "hello #1";
char msg2[] = "hello #2";
char msg3[] = "hello #3";
main()
{ char inbuf[MSGSIZE];
int p[2], j;
/* open pipe */
if(pipe(p) == -1)
{ perror("pipe call error");
exit(1);
}
/* write down pipe */
write(p[1], msg1, MSGSIZE);
write(p[1], msg2, MSGSIZE);
write(p[1], msg3, MSGSIZE);
/* read pipe */
for(j=0; j<3; j++)
{ read(p[0], inbuf, MSGSIZE);
printf("%s\n", inbuf);
}
exit(0);
}
What I am unable to understand is :
a) How is a string(msg1, msg2, msg3) written to an integer array(p[1])?
b) Are the above strings written one after the other at the same location(p[1])?
c) If all the three strings are written one after the other to p[1]( a single location), how is the program able to read all three of them?
Any answers will be much appreciated.
Thank you!!