title: Creating multiple child processes in C for one parent
body: <p>So I've been working on code for my OS class, and in my project, I have to create a new child process for each file on the command line, and pipe information from the child to the parent. We're supposed to be re-creating Unix's wc utility (I've handled this part already).</p>
  
  <p>What I've tried thus far is:</p>
  
  <pre><code>            for(i=0; i&lt;argcount; i++){
                      int pid;
                      pid = fork();
                      if(pid == 0){
                              /* Child Process */
                              /* Close read pipes */
                              close(l_pipe[0]);
                              close(w_pipe[0]);
                              close(c_pipe[0]);
                              wc(lflag, wflag, cflag, filenames[i]);
                      } else {
                              /* Parent Process for piping */
                              /* Close write pipes */
                              close(l_pipe[1]);
                              close(w_pipe[1]);
                              close(c_pipe[1]);
  
                              /* Read from pipes */
                              read(l_pipe[0], &amp;buffer, sizeof(count_t));
                              lines+=buffer;
  
                              read(w_pipe[0], &amp;buffer, sizeof(count_t));
                              words+=buffer;
  
                              read(c_pipe[0], &amp;buffer, sizeof(count_t));
                              bytes+=buffer;
                      }
              }
  </code></pre>
  
  <p>However, this creates as many parents as there are children, which is obviously wrong. I'm not exactly sure where I should be forking. I <i>have</i> to use a pipeline between the child and parent processes, and I'm certain that the parent needs to read() as many times as the child write()s.</p>
  
  <p>Thank you for any suggestions you can provide.</p>