title: C fork/exec with non-blocking pipe IO
body: <p>This seems to be a fairly common thing to do, and I've managed to teach myself everything that I need to make it work, except that I now have a single problem, which is defying my troubleshooting.</p>
  
  <pre><code>int nonBlockingPOpen(char *const argv[]){
      int inpipe;
      pid_t pid;
      /* open both ends of pipe nonblockingly */
      pid = fork();
  
      switch(pid){
          case 0:         /*child*/
              sleep(1); /*child should open after parent has open for reading*/
  
              /*redirect stdout to opened pipe*/
              int outpipe = open("./fifo", O_WRONLY);
              /*SHOULD BLOCK UNTIL MAIN PROCESS OPENS FOR WRITING*/
              dup2(outpipe, 1);
              fcntl(1, F_SETFL, fcntl(1, F_GETFL) | O_NONBLOCK);
  
              printf("HELLO WORLD I AM A CHILD PROCESS\n");
              /*This seems to be written to the pipe immediately, blocking or not.*/
              execvp(*argv, argv);
              /*All output from this program, which outputs "one" sleeps for 1 second
               *outputs "two" sleeps for a second, etc, is captured only after the
               *exec'd program exits!
               */
              break;
  
          default:        /*parent*/
              inpipe = open("./fifo", O_RDONLY | O_NONBLOCK);
              sleep(2);
              /*no need to do anything special here*/
              break;
      }
  
      return inpipe;
  }
  </code></pre>
  
  <p>Why won't the child process write its stdout to the pipe each time a line is generated? Is there something I'm missing in the way execvp or dup2 work? I'm aware that my approach to all this is a bit strange, but I can't find another way to capture output of closed-source binaries programatically.</p>