title: exec() and pipe() between child process in C
body: <p>I'm trying to implement this using <code>pipe()</code> and <code>fork()</code> :</p>
  
  <blockquote>
    <p>ls | wc</p>
  </blockquote>
  
  <p>First I checked if pipe works fine, and here is the code.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  
  int main(void){
  
      char *param1[]={"ls",NULL};
      char *param2[]={"wc",NULL};
  
      int p[2];
      pipe(p);
  
      pid_t process1, process2;
  
      if((process1=fork())==0){ // child 1
          dup2(p[1],1); // redirect stdout to pipe
          close(p[0]);
          execvp("ls", param1);
          perror("execvp ls failed");
      }
      else if(process1&gt;0){ // parent
          wait(NULL);
      }
  
      if((process2=fork())==0){ // child 2
          dup2(p[0],0); // get stdin from pipe
          close(p[1]);
  
          char buff[1000]={0};
          read(STDIN_FILENO,buff,250);
          printf("---- in process2 -----\n%s\n",buff);
      }
      else if(process2&gt;0){ // parent
          wait(NULL);
      }
      return 0;
  }
  </code></pre>
  
  <p>I checked it works fine. I replaced <code>read()</code> and <code>printf()</code> to <code>exec()</code>, like :</p>
  
  <pre><code>if((process2=fork())==0){ // child 2
      dup2(p[0],0); // get stdin from pipe
      close(p[1]);
  
      execvp("wc",param2);
      perror("execvp wc failed");
  }
  </code></pre>
  
  <p>My terminal just hang. I think <code>wc</code> process doesn't get any input. So my question is, why <code>read()</code> and <code>printf()</code> works, and <code>execvp()</code> doesn't work?</p>