title: Process programming model using Linux pipe()
body: <p>From <a href="http://pubs.opengroup.org/onlinepubs/009604599/functions/pipe.html" rel="nofollow">http://pubs.opengroup.org/onlinepubs/009604599/functions/pipe.html</a>:</p>
  
  <blockquote>
    <p>The pipe() function shall create a pipe and place two file
    descriptors, one each into the arguments fildes[0] and fildes[1], that
    refer to the open file descriptions for the read and write ends of the
    pipe.</p>
  </blockquote>
  
  <p>There is an example where a parent writes data to its child:</p>
  
  <pre><code>int fildes[2];
  const int BSIZE = 100;
  char buf[BSIZE];
  ssize_t nbytes;
  int status;
  
  
  status = pipe(fildes);
  if (status == -1 ) {
      /* an error occurred */
      ...
  }
  
  
  switch (fork()) {
  case -1: /* Handle error */
      break;
  
  
  case 0:  /* Child - reads from pipe */
      close(fildes[1]);                       /* Write end is unused */
      nbytes = read(fildes[0], buf, BSIZE);   /* Get data from pipe */
      /* At this point, a further read would see end of file ... */
      close(fildes[0]);                       /* Finished with pipe */
      exit(EXIT_SUCCESS);
  
  
  default:  /* Parent - writes to pipe */
      close(fildes[0]);                       /* Read end is unused */
      write(fildes[1], "Hello world\n", 12);  /* Write data on pipe */
      close(fildes[1]);                       /* Child will see EOF */
      exit(EXIT_SUCCESS);
  }
  </code></pre>
  
  <ol>
  <li><p>I was wondering how a parent using a pipe to communicate with its child can make sure it will not happen that the child runs read() before the parent runs write, and the child
  finish reading before the parent finish writing?</p></li>
  <li><p>I was wondering if a pipe created in the parent can be used for
  two-way communication between parent and child, or just one way from
  the parent to the child not the other way? </p>
  
  <p>If the child can send data to parent via the pipe, what will the
  program looks like?</p></li>
  <li><p>Is a pipe like a real pipe with two ends. fildes[0] and fildes[1]
  are used to represent the two ends respectively? </p>
  
  <p>If a pipe is two way, what is the meaning of read end and write end
  with respect to,i.e.which one read (write), the parent or the child?</p></li>
  </ol>
  
  <p>Thanks and regards!</p>