title: fork() and wait() with two child processes
body: <p>I need to use the <code>fork()</code> and <code>wait()</code> functions to complete an assignment. We are modelling non-deterministic behavior and need the program to <code>fork()</code> if there is more than one possible transition.</p>
  <p>In order to try and work out how <code>fork</code> and <code>wait</code> work, I have just made a simple program. I think I understand now how the calls work and would be fine if the program only branched once because the parent process could use the exit status from the single child process to determine whether the child process reached the accept state or not.</p>
  <p>As you can see from the code that follows though, I want to be able to handle situations where there must be more than one child processes. My problem is that you seem to only be able to set the status using an <code>_exit</code> function once. So, as in my example the exit status that the parent process tests for shows that the first child process issued 0 as it's exit status, but has no information on the second child process.</p>
  <p>I tried simply not <code>_exit()</code>-ing on a reject, but then that child process would carry on, and in effect there would seem to be two parent processes.</p>
  <p>Sorry for the waffle, but I would be grateful if someone could tell me how my parent process could obtain the status information on more than one child process, or I would be happy for the parent process to only notice accept status's from the child processes, but in that case I would successfully need to exit from the child processes which have a reject status.</p>
  <p>My test code is as follows:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;sys/wait.h&gt;
  
  int main(void)  {
  
      pid_t child_pid, wpid, pid;
      int status = 0;
      int i;
  
      int a[3] = {1, 2, 1};
      for(i = 1; i &lt; 3; i++)  {
          printf(&quot;i = %d\n&quot;, i);
          pid = getpid();
          printf(&quot;pid after i = %d\n&quot;, pid);
          if((child_pid = fork()) == 0)  {
              printf(&quot;In child process\n&quot;);
              pid = getpid();
              printf(&quot;pid in child process is %d\n&quot;, pid);
              /* Is a child process */
              if(a[i] &lt; 2)  {
                  printf(&quot;Should be accept\n&quot;);
                  _exit(1);
              } else  {
                  printf(&quot;Should be reject\n&quot;);
                  _exit(0);
              }
          }
      }
  
      if(child_pid &gt; 0)  {
          /* Is the parent process */
          pid = getpid();
          printf(&quot;parent_pid = %d\n&quot;, pid);
          wpid = wait(&amp;status);
          if(wpid != -1)  {
              printf(&quot;Child's exit status was %d\n&quot;, status);
              if(status &gt; 0)  {
                  printf(&quot;Accept\n&quot;);
              } else  {
                  printf(&quot;Complete parent process\n&quot;);
                  if(a[0] &lt; 2)  {
                      printf(&quot;Accept\n&quot;);
                  } else  {
                      printf(&quot;Reject\n&quot;);
                  }
              }
          }
      }
      return 0;
  }
  </code></pre>