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!!

Hello,

Could someone reply?

Thank you!

Hello Dragon,

Thank you! That was very helpful.

HI...
As per my operating system knowledge, PIPE is a file type which resides in RAM.
Pipe has two ends one is read and another is write.
when u call system call pipe u ll have two typea of disriptor in user file descriptor table.
while writting it will open a file and ll write as we are writting in a file, the only difference is that while reading we have to read in sequence that is first in first out.

Hello Rajenar,

Thank you!