title: Creating a pipe with dup2
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  
  int main(void) {
      int pfd1[2];
      int pfd2[2];
      pid_t pid1, pid2, pid3;
      if(pipe(pfd1)==-1) {
          perror("Creazione pipe");
          exit(EXIT_FAILURE);
          }
      if(pipe(pfd2)==-1) {
          perror("Creazione pipe");
          exit(EXIT_FAILURE);
          }
      printf("Sono il padre\n");
      switch(pid1=fork()) {
          printf("%d\n", pid1);
          case -1: {
              perror("Creazione figlio 1");
              exit(EXIT_FAILURE);
              }
          case 0: { //figlio 1
              printf("Sono il figlio 1\n");
              if(dup2(pfd1[1],1)==-1) { //redirige lo stdout sul descrittore scrittura
                  perror("Prima redirezione");
                  exit(EXIT_FAILURE);
                  }
              close(pfd1[0]);
              close(pfd1[1]); //lo chiudo perchè sto redirigendo lo stdout
              close(pfd2[0]);
              close(pfd2[1]);
              execlp("ps", "ps", "-A", "-ostat,pid", (char*) NULL);
              }
          }
      waitpid(pid1,NULL,0);
      switch(pid2=fork()) {
          case -1: {
              perror("Creazione figlio 2");
              exit(EXIT_FAILURE);
              }
          case 0: { //figlio 2
              printf("Sono il figlio 2\n");
              if(dup2(pfd1[0],0)==-1) { //redirige lo stdin sul descrittore lettura
                  perror("Seconda redirezione");
                  exit(EXIT_FAILURE);
                  }
              printf("Prima redirezione figlio 2\n");
              if(dup2(pfd2[1],1)==-1) {
                  perror("Terza redirezione");
                  exit(EXIT_FAILURE);
                  }
              close(pfd1[1]);
              close(pfd1[0]);
              close(pfd2[0]);
              close(pfd2[1]);
              execlp("grep", "grep", "-e", "[zZ]", (char*) NULL);
              }
      waitpid(pid2, NULL,0);
      switch(pid3=fork()) {
          case -1: {
              perror("Creazione terzo figlio");
              exit(EXIT_FAILURE);
              }
          case 0: { //figlio 3
              printf("Sono il figlio 3\n");
              if(dup2(pfd2[0],0)==-1) {
                  perror("Quarta redirezione");
                  exit(EXIT_FAILURE);
                  }
              close(pfd1[0]);
              close(pfd1[1]);
              close(pfd2[0]);
              close(pfd2[1]);
              execlp("awk", "awk", "'{print $2}'", (char*) NULL);
              }
          }
      /*padre*/
      //waitpid(pid1, NULL, 0);
      //waitpid(pid2, NULL, 0);
      waitpid(pid3, NULL, 0);
      close(pfd2[0]);
      close(pfd2[1]);
      close(pfd1[0]);
      close(pfd1[1]);
      return 0;
      }
  
  }
  </code></pre>
  
  <p>Hello,
  I'm trying to create a pipe of shell bash commands using the system call dup2. 
  The output I expect should be the same as </p>
  
  <pre><code>bash $&gt; ps -A -ostat,pid | grep -e [zZ] | awk '{print $2}'
  </code></pre>
  
  <p>what I do is forking 3 children and make them communicate through two pipes. Each one of the children executes one part of the command.
  The problem is my program gets stuck on the second child which apparently doesn't even get to do the exec.
  I'm sure there are some problems with my code, but since it's the first time for me trying to use dup2 I'm a bit confused.
  Also, don't mind the printfs, they're just for debugging.
  Thanks a lot!</p>