title: Plumbing pipes with pipe() in c++
body: <p>I am currently trying to connect pipes between a parent and its children. The children are execing <code>sort</code> and are sorting input they receive from the parent. The children then write to a separate pipe. There are two pipes for each process. One so the parent may send input to child. Another so the parent may receive the result of sort.</p>
  
  <p>My problem thus far has been reading the input. I have been getting confirmation from <code>fputs()</code> that I am successfully writing to the childs input. After that I <code>fflush(NULL)</code> and try to read the child's output. The read blocks, as in it never returns or reaches a statement after <code>fputs</code>. Which is rather odd, as I believe that I have set the reads to <code>O_NONBLOCK</code>. The output is listed below.  </p>
  
  <pre><code>line 174 
  line 176 
  line 178 
  </code></pre>
  
  <p>Here is a snippet of code:</p>
  
  <pre><code>int sort_writes[num_sort][2];
      int sort_reads[num_sort][2];
      int i;
      int *status;
      int flags;
      char buffer[BUFFER_SIZE];
      // this should contain a bunch of write fds wrapped in fdopen
      FILE*  to_sort[num_sort];
      // the same except with reads
      FILE*  from_sort[num_sort];
      //this only include for simplicity and so that exec will happen proper
      char *sort_argv[2];
      sort_argv[0]=(char*)"sort";
      sort_argv[1]= (char *)NULL;
  
      // This will create all of the pipes for the sorts.
      // The parent will read  0 and the even pipes. it will write to the odd.
      for(i=0; i&lt; num_sort; i++){
              //parent  reads from this pipe. child writes to it.
                      assert(pipe(sort_writes[i]) == 0);
  
              //parent write to this pipe. child reads from it.
                      assert(pipe(sort_reads[i]) ==0);
                      switch(fork()){
                              case 0: //this is the child
                                      //this closes unnecessary fds
                                      _close_less_than(i, sort_writes);
                                      _close_less_than(i, sort_reads);
                                      dup2(sort_reads[i][0], STDIN_FILENO);
                                      // standard out becomes parent pipe in
                                      dup2(sort_writes[i][1], STDOUT_FILENO);
                                                                                                     execv(SORT_LOC.c_str(), sort_argv);
                              default: // this the parent.
                                      //clean up. close unused.
                                      close(sort_writes[i][1]);
                                      close(sort_reads[i][0]);
                      }
  
      }
      //Creates a file pointer for all of the fds I will use to communicate with my sorts
      //It also sets all reads to nonblock and the parent write stdio buffers to zero.        
      for(i=0; i&lt; num_sort; i++){
              assert((from_sort[i]= fdopen(sort_writes[i][0] ,"r")) != NULL);
              assert((to_sort[i]= fdopen(sort_reads[i][1] , "w")) != NULL);         //pipes ignore truncate
  
              flags = fcntl(sort_writes[i][0], F_GETFL);
              flags |= O_NONBLOCK;
              fcntl(sort_writes[i][0], F_SETFL, flags);
  
  
      }
  
      for(i=0; i&lt;(int)theArray.size(); i++){
              fputs(theArray.back().c_str(), to_sort[i % num_sort]);
              theArray.pop_back();
              fflush(NULL); // so that the data gets from stdio buffers to pipe buffers.      
      }
      cout &lt;&lt; "line 174 \n";
      for(i=0; i &lt;1; i++){
              cout &lt;&lt; "line 176 \n";
              while(!feof(from_sort[i])){
                      cout &lt;&lt; "line 178 \n";
                      cout &lt;&lt; fgets(buffer, BUFFER_SIZE, from_sort[i]);
                      cout &lt;&lt; buffer;
                      cout &lt;&lt; "at least i tried \n";
              }
  </code></pre>