Q: Piping three commands together in C I am trying to create my own 'unix-style' shell. I would like to be able to pipe together N commands using a loop. However I have been unsuccessful so far. I was able to pipe 2 commands together, now I am trying to pipe 3 commands together. When I execute, nothing happens, and it seems like the parent process is just waiting for the child processes. For reference, the command I am trying to execute is: ls | head -3 | tail -1.
      int p1[2], p2[2]; //pipes
      pipe(p1);
      int pid1, pid2;
      pid1 = fork(); //create child
      if (pid1 ==0) { //child
        close(1); //close stdout
        dup(p1[1]); 
        close(p1[0]); //closes pipes we dont need
        close(p1[1]);
        exec(command[0][0], command[0]); //execute command
        exit(0);
      }
      pipe(p2); //create 2nd pipe
      pid2=fork(); //create child
      if(pid2 == 0) { //child process
        close(0); //close stdin
        dup(p1[0]);
        close(p1[0]); //close pipes we dont need
        close(p1[1]);
        close(1); //close stdout
        dup(p2[1]);
        close(p2[0]);
        close(p2[1]);
        exec(command[1][0], command[0]); //execute command
        exit(0);
      }
      else
      { //parent
        wait(NULL); //stops here
        wait(NULL); //waiting for child processes to complete
        close(p1[0]);
        close(p1[1]);
        close(0); //close stdin
        dup(p2[0]);
        close(p2[0]); //close pipes we dont need
        close(p2[1]);
        exec(command[2][0], command[2]); //execute command
      }

