title: How do I use $status returned by pcntl_waitpid()?
body: <p>I have a parent/worker arrangement going on.  The parent keeps the worker PIDs in an array, constantly checking that they are still alive with the following loop:</p>
  
  <pre><code>// $workers is an array of PIDs
  foreach ($workers as $workerID =&gt; $pid) {
      // Check if this worker still exists as a process
      pcntl_waitpid($pid, $status, WNOHANG|WUNTRACED);
  
      // If the worker exited normally, stop tracking it
      if (pcntl_wifexited($status)) {
          $logger-&gt;info("Worker $workerID exited normally");
          array_splice($workers, $workerID, 1); 
      }
  
      // If it has a session ID, then it's still living
      if (posix_getsid($pid))⋅
          $living[] = $pid;
  }
  
  // $dead is the difference between workers we've started
  // and those that are still running
  $dead = array_diff($workers, $living);
  </code></pre>
  
  <p>The problem is that <code>pcntl_waitpid()</code> is always setting <code>$status</code> to 0, so the very first time this loop is run, the parent thinks that all of its children have exited normally, even though they are still running.  Am I using <code>pcntl_waitpid()</code> incorrectly, or expecting it to do something that it doesn't?</p>