title: Implementing multiple pipe in C
body: <p>Can anyone give my any idea why my following codes trying to implement multiple pipes are incorrect? I am implementing a parent-child relationship, so no parent-grandchildren relationships are allowed, i.e, all the children should run in parallel.
  When i input the command ls | cat | cat, the contents of ls are output, but the prompt still prompt me for input and it won't stop even if I input another character.
  Am I doing this in a wrong way?</p>
  
  <pre><code>    int i;
      int pipefd[num_of_pipe][2];
      pid_t child_pid[num_of_child];
  
      for (i = 0; i &lt; num_of_child; i++) {
        pipe(pipefd[i]);
        if ((child_pid[i] = fork()) == 0) {
          if (i == 0) {
            close(pipefd[i][0]);
            dup2(pipefd[i][1], STDOUT_FILENO);
            close(pipefd[i][1]);
            /* execvp something; */
          } else if (i == num_of_child - 1) {
            close(pipefd[i - 1][1]);
            dup2(pipefd[i - 1][0], STDIN_FILENO);
            close(pipefd[i - 1][0]);
            /* execvp something */
          } else {
            close(pipefd[i - 1][1]);
            dup2(pipefd[i - 1][0],0);
            close(pipefd[i - 1][0]);
            close(pipefd[i][0]);
            dup2(pipefd[i][1],1);
            close(pipefd[i][1]);
            /* execvp something */
          }
        }
      }
  
      int k;
      for (k = 0; k &lt; num_of_child; k++) {
        waitpid(child_pid[k], NULL, WUNTRACED);
        printf("the %dth child returns\n", k + 1);
      }
  
      int j;
      for (j = 0; j &lt; num_of_pipe; j++) {
        close(pipefd[j][0]);
        close(pipefd[j][1]);
      }
    }
  </code></pre>