title: How to use dup2/close correctly to connect these three processes?
body: <p>I'm trying to properly connect three processes in order to allow inter-process communication between them. I have one process, scanner, which takes the parent's STDIN and then processes the words within the stream. If a word length is odd, it sends it to one process, if it is even, it sends it to another. These processes should take in these words via STDIN (I assume) and then output some info back to the scanner process via STDOUT. The STDOUT of even/odd should be redirected to scanner, which will then read (using read) and then output/process the words. It's an academic exercise, not a practical one. Here's what a picture of it would look like:</p>
  
  <p><img src="https://i.stack.imgur.com/wXtMq.png" alt="Pipe setup"></p>
  
  <p>Here's what my code currently looks like. The problem is I'm not exactly sure what to dup and what to close. Once I figure that out I should be good to go! Any advice would be appreciated.</p>
  
  <p>File descriptors:</p>
  
  <pre><code>int scannertoeven[2]; int scannertoodd[2];
  int eventoscanner[2]; int oddtoscanner[2];
  //Pipe stuff here (ommitted)
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code> //Create the child processes
   if ((scanner_pid = fork())  == 0) {
  
        //We need the scanner pid so even and odd can send signals to it
        char pidofparent[sizeof(getpid())];
        sprintf(pidofparent, "%i", getpid());
  
        //Even stuff
        if ((even_pid = fork()) == 0) {
             close(scannertoodd[0]); close(scannertoodd[1]);
             close(oddtoscanner[0]); close(oddtoscanner[1]);
  
             //Not sure which ones to close
             close(scannertoeven[0]); close(scannertoeven[1]);
             close(eventoscanner[0]); close(eventoscanner[1]);
  
  
             //Correct?
             close(STDIN_FILENO);
             dup2(scannertoeven[0], STDIN_FILENO);
             close(STDOUT_FILENO);
             dup2(eventoscanner[1], STDOUT_FILENO);
  
  
            if(execl("./evenodd", "even", pidofparent,  NULL ) == -1) {
                 printf("execl Error!");
                 exit(1);
            }
  
       //Odd Stuff
       } else if ((odd_pid = fork()) == 0){
             close(scannertoeven[0]); close(scannertoeven[1]);
             close(eventoscanner[0]); close(eventoscanner[1]);
  
             //Not sure which ones to close
             close(scannertoodd[0]); close(scannertoodd[1]);
             close(oddtoscanner[0]); close(oddtoscanner[1]);
  
             //Correct?
             close(STDIN_FILENO);
             dup2(scannertoodd[0], STDIN_FILENO);
             close(STDOUT_FILENO);
             dup2(oddtoscanner[1], STDOUT_FILENO);
  
            if(execl("./evenodd", "odd", pidofparent,  NULL ) == -1) {
                 printf("execl Error!");
                 exit(1);
            } 
  
  
        //Actual Scanner
        } else {
  
             // Not sure which ones to close- this is very wrong
             close(scannertoeven[0]); close(scannertoeven[1]);
             close(eventoscanner[0]); close(eventoscanner[1]);
             close(scannertoodd[0]); close(scannertoodd[1]);
             close(oddtoscanner[0]); close(oddtoscanner[1]);
  
             //Not sure what to dup either
             dup2(scannertoodd[1], STDOUT_FILENO);
             dup2(scannertoeven[1], STDOUT_FILENO);
             if(execl("./scanner", "scanner", stoeven, stoodd, eventos, oddtos, NULL ) == -1) {
                  printf("execl Error!");
                  exit(1);
             }
  
             //Wait twice here, or three times in main?
             waitpid(odd_pid, &amp;status2, 0);
             waitpid(even_pid, &amp;status3, 0);
        }
   //Main
   } else {
        //Close Pipes
        close(scannertoodd[0]); close(scannertoeven[0]); close(eventoscanner[0]); close(oddtoscanner[0]);
        close(scannertoodd[1]); close(scannertoeven[1]); close(eventoscanner[1]); close(oddtoscanner[1]);
        //Wait for children to finish
        waitpid(scanner_pid, &amp;status1, 0);
        printf("Done\n");
   }
  </code></pre>