title: Exercise in C - forks
body: <p>I am trying to understand how the <code>fork()</code> method works in C.  This is an example problem from my textbook:</p>
  
  <blockquote>
  <pre><code>int
  main(void)
  {
    pid_t process_id;
    int status;
  
    if (fork() == 0) { 
      if (fork() == 0) { 
        printf("A"); 
      } else {
        process_id = wait(&amp;status);
        printf("B");
      }
    } else {
      if (fork() == 0) { 
        printf("C");
        exit(0);
      }
      printf("D");
    }
    printf("0");
    return (0);
  }
  </code></pre>
    
    <p>Which of the following outputs are possible?</p>
    
    <p>1) D0A0B0C </p>
    
    <p>2) CDAB000 </p>
    
    <p>3) DA00CB0 </p>
    
    <p>4) D0AB0CD </p>
    
    <p>5) AD00BC0</p>
  </blockquote>
  
  <p>I sketched it out on paper and I think the right answer is: 1, 3, 5. I had some difficulty working around the <code>wait(&amp;status)</code>, though. Is my answer right?</p>