title: Implementing pipe in C
body: <p>I am trying to implement pipe in C. eg - <code>$ ls | wc | wc</code></p>
  
  <p>I have written the following code - </p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  void run_cmd(char *cmd, int* fd_in, int* fd_out)
  {
      int c = fork();
  
      if (c==0)
      {
          if (fd_in != NULL)
          {
              close(fd_in[1]);
              dup2(fd_in[0], 0);
          }
          if (fd_out != NULL)
          {
              close(fd_out[0]);
              dup2(fd_out[1],1);
          }
          execlp(cmd, cmd, NULL);
      }
  }
  
  int main(int argc, char **argv)
  {
      int fd_1[2], fd_2[2], i;
      pipe(fd_1);
      pipe(fd_2);
  
      run_cmd(argv[1], NULL, fd_1);
  
      for( i=2; i&lt;argc-1; i++)
      {
          if (i%2 == 0)
              run_cmd(argv[i], fd_1, fd_2);
          else
              run_cmd(argv[i], fd_2, fd_1);
      }
      if (i%2 == 0)
          run_cmd(argv[i], fd_1, NULL);
      else
          run_cmd(argv[i], fd_2, NULL);
  }
  </code></pre>
  
  <p>This works fine with two arguments, eg - <code>$./a.out ls wc</code></p>
  
  <p>But when I try with more than two arguments it does not work.</p>
  
  <p>Would anyone please tell me what's wrong with my code, or any other way to do this?</p>