title: Why do I need to close fds when reading and writing to the pipe?
body: <p>Here is an <a href="http://tldp.org/LDP/lpg/node11.html" rel="noreferrer">example</a> to illustrate what I mean:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  
  int main(void)
  {
          int     fd[2], nbytes;
          pid_t   childpid;
          char    string[] = "Hello, world!\n";
          char    readbuffer[80];
  
          pipe(fd);
  
          if((childpid = fork()) == -1)
          {
                  perror("fork");
                  exit(1);
          }
  
          if(childpid == 0)
          {
                  /* Child process closes up input side of pipe */
                  close(fd[0]);
  
                  /* Send "string" through the output side of pipe */
                  write(fd[1], string, (strlen(string)+1));
              exit(0);
      }
      else
      {
              /* Parent process closes up output side of pipe */
              close(fd[1]);
  
              /* Read in a string from the pipe */
              nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
              printf("Received string: %s", readbuffer);
      }
  
     return(0);
  </code></pre>
  
  <p>}</p>
  
  <p>However, what if one of my processes needs to continuously write to the pipe while the other pipe needs to read?</p>
  
  <p>The example above seems to work only for one write and one read.</p>