title: C: Using Fork() and Pipe() to add numbers in child processes
body: <p>I'm trying to send numbers from a file to child processes with <code>fork()</code> and <code>pipe()</code>, which the child processes should add and send back to the parent process which will then add the child sums to get a total sum.</p>
  
  <p>For a simplified version of that problem, I've got an Array of 4 numbers, and am only using 1 Child process (2 pipes).</p>
  
  <p>I'm having difficulties seeing where control in my program goes, which makes it hard for me to troubleshoot what else is going wrong.</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
  int numChildProcesses = 1;
  int testArray[4] = {2,7,9,4};
  
  printf("Will use 1 child process; %d pipes.\n", numChildProcesses*2);
  int fd[numChildProcesses*2][2]; //parent and child
  
  int val = 0, len, i;
  
  // create all the descriptor pairs we need
  for (i=0; i&lt;numChildProcesses*2; ++i) // 2 pipes // parent + child
  {
      if (pipe(fd[i]) &lt; 0)
      {
          perror("Failed to allocate pipes.");
          exit(EXIT_FAILURE);
      }
  }
  
  for (i=0;i&lt;numChildProcesses;i++)
  {
  
      //CHILD/////////////////////////////////////////////////////////////////////
      if (fork() == 0)
      {
          int total = 0, xCount = 0;
  
          while (xCount &lt; 4)
          {
              // wait for parent to send us a value
              len = read(fd[i][0], &amp;val, sizeof(val));
              if (len &lt; 0)
              {
                  perror("Child: Failed to read data from pipe.\n");
                  exit(EXIT_FAILURE);
              }
              else if (len == 0)
              {
                  // not an error, but certainly unexpected
                  fprintf(stderr, "Child: Read EOF from pipe\n");
              }
              else // Successfully read from Parent
              {
                  total += val;
                  xCount += 1;
                  printf("Child: Recieved %d\tTotal: %d\tCount: %d\n", val, total, xCount);
  
              }
          }
  
          // send the value back to the parent
          printf("Child: Sending %d back\n", total);
          if (write(fd[i][1], &amp;total, sizeof(total)) &lt; 0)
          {
              perror("Child: Failed to write response value");
              exit(EXIT_FAILURE);
          }
  
          return EXIT_SUCCESS;
      }
  
      //PARENT/////////////////////////////////////////////////////////////////////
      if (fork() &gt; 0)
      {
          int total = 0;
  
          // send array to child as well as starting point
          printf("\nParent: Sending numbers to child\n");
          //if (write(fd[i][1], 0, (fileNumbers/numChildProcesses)*5) != sizeof((fileNumbers/numChildProcesses)*5));
          if (write(fd[i][1], &amp;testArray, sizeof(testArray)) != sizeof(testArray))
          {
              perror("Parent: Failed to send value to child ");
              exit(EXIT_FAILURE);
          }
  
          // now wait for a response
          len = read(fd[i][0], &amp;val, sizeof(val));
          if (len &lt; 0)
          {
              perror("Parent: failed to read value from pipe");
              exit(EXIT_FAILURE);
          }
          else if (len == 0)
          {
              // not an error, but certainly unexpected
              fprintf(stderr, "Parent: Read EOF from pipe\n");
          }
          else
          {
              // report what we received
              total += val;
              printf("Parent: Received %d\tTotal: %d\n", val, total);
          }
  
          // wait for child termination
          wait(NULL);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <hr>
  
  <p>My output is as follows:</p>
  
  <pre><code>Will use 1 child process; 2 pipes.
  
  Parent: Sending numbers to child
  Parent: Received 2      Total: 2
  Child: Recieved 7       Total: 7        Count: 1
  Child: Recieved 9       Total: 16       Count: 2
  Child: Recieved 4       Total: 20       Count: 3
  </code></pre>
  
  <p>Furthermore, if I try something like <code>printf("%d", fork());</code> as soon as I enter my <code>for()</code> loop to see what it taking control, it gets a little crazy. It acts like using <code>fork()</code> affects the way the program runs, as if it is a <code>pop()</code> or something of the sort.</p>
  
  <hr>
  
  <p>Anyways, thank you for any insight you can offer.</p>
  
  <p>-Tom</p>