title: Using fork() to find the sum of digits in a number
body: <p>I'm writing a small program that takes a number in the command line and uses fork() to create a chain of processes that sum the number's digits. So far, it is as follows:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/wait.h&gt;
  
  int main(int argc, char* argv[]){
  
    if(argc != 2){
      printf("Usage: sod [number] \n");  /* -o sod when compiled */
      exit(1); 
    }
  
    pid_t childpid = 0; 
    int sum = 0;   
  
    int i; 
    for(i = 0; i &lt; strlen(argv[1]); i++){
      /* atoi needs a string */
      char str[2]; 
      str[0] = argv[1][i]; str[1] = 0;  
      sum += atoi(str);   
      if(childpid = fork()) 
        break;  
    }
  
    wait(NULL); 
    printf("sum: %d\n", sum);
    exit(0); /* probably redundant... */
    return 0; 
  }
  </code></pre>
  
  <p>From what I can understand in my systems book, the if statement ensures that the processes are in a chain (each parent can have one child, and it continues down the line), because <code>fork()</code> returns the child's id to a parent process and 0 to a child. Thus, the result of the assignment is nonzero/true for a parent and forces it to break from the loop:  </p>
  
  <pre><code> if(childpid = fork()) 
            break; 
  </code></pre>
  
  <p>Before I added the <code>wait(NULL)</code> call, the function printed multiple lines of the sum, not necessarily in order, which I suppose should be expected because the processes finish at different times. Interestingly, though, if I call the program with a large number of digits like 11221121, it sometimes prints some of the sums, outputs my shell prompt right in the middle of them, and prints a few more before hanging there as if in an infinite loop: </p>
  
  <pre><code>[nvj]@sun ~/313/sod&gt; (12:15:21 02/10/13)
  :: sod 11221121
  sum: 1
  sum: 2
  sum: 4
  sum: 6
  sum: 7
  sum: 8
  sum: 10
  
  [nvj]@sun ~/313/sod&gt; (12:15:24 02/10/13)
  :: sum: 11
  sum: 11
  [hangs here...]
  </code></pre>
  
  <p>My only guess as to why this might be happening is that something is timing out or things that need to wait for others are getting into a rut. To make the program actually end (and print the sums in some kind of order), I added the <code>wait(NULL)</code> call. From what I can understand, it forces a parent to wait for all of its children before continuing. Unsurprisingly, this causes the actual sum to print first as the last created child finishes, and lets the rest follow in order:<br>
  [</p>
  
  <pre><code>nvj]@sun ~/313/sod&gt; (12:25:02 02/10/13)
  :: sod 12389492182398
  sum: 69
  sum: 69
  sum: 61
  sum: 52
  sum: 49
  sum: 47
  sum: 39
  sum: 38
  sum: 36
  sum: 27
  sum: 23
  sum: 14
  sum: 6
  sum: 3
  sum: 1
  </code></pre>
  
  <p>Thankfully, the program actually ends in this case. But is there a way to make it end right when the child with the actual sum returns and displays its result? I have experimented with things like flat-out calling <code>exit</code>(as you can see...), but this seems to operate in a different 'realm' than the child processes and does not immediately happen after the first print.  </p>