title: A doubt on pipes in Unix
body: <p>This code below is for executing ls -l | wc -l.
  In the code, if I comment close(p[1]) in parent then the program just hangs, waiting for some input. Why it is so? The child writes output of ls on p1 and parent should have taken that output from p0.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  
  main ()
  {
    int i;
    int p[2];
    pid_t ret;
    pipe (p);
    ret = fork ();
  
    if (ret == 0)
      {
        close (1);
        dup (p[1]);
        close (p[0]);
        execlp ("ls", "ls", "-l", (char *) 0);
      }
  
    if (ret &gt; 0)
      {
        close (0);
        dup (p[0]);
        //Doubt, Commenting the line below does not work WHy?
        close (p[1]);
        wait (NULL);
        execlp ("wc", "wc", "-l", (char *) 0);
      }
  }
  </code></pre>