title: Terminating zombie child processes forked from socket server
body: <blockquote>
    <h2>Disclaimer</h2>
    
    <p>I am well aware that PHP might not have been the best choice in this case for a socket server.  Please refrain from suggesting
    different languages/platforms - believe me - I've heard it from all
    directions.</p>
  </blockquote>
  
  <p>Working in a <strong>Unix environment</strong> and using <strong>PHP 5.2.17</strong>, my situation is as follows - I have constructed a socket server in PHP that communicates with flash clients.  My first hurtle was that each incoming connection blocked the sequential connections until it had finished being processed.  I <em>solved</em> this by utilizing PHP's <code>pcntl_fork()</code>.  I was successfully able to spawn numerous child processes (saving their PID in the parent) that took care of broadcasting messages to the other clients and therefore "releasing" the parent process and allowing it to continue to process the next connection[s].</p>
  
  <p>My main issue right now is dealing/handling with the collection of these dead/zombie child processes and terminating them.  I have read (over and over) the relevant PHP manual pages for <a href="http://www.php.net/manual/en/book.pcntl.php" rel="nofollow noreferrer">pcntl_fork()</a> and realize that <strong>the parent process is in charge of cleaning up its children</strong>.  The parent process receives a SIGNAL from its child when the child executes an <code>exit(0)</code>. I am able to "catch" that signal using the <code>pcntl_signal()</code> function to setup a <em>signal handler</em>.  </p>
  
  <p>My signal_handler looks like this :</p>
  
  <pre class="lang-php prettyprint-override"><code>declare(ticks = 1); 
  function sig_handler($signo){ 
    global $forks; // this is an array that holds all the child PID's
    foreach($forks AS $key=&gt;$childPid){
      echo "has my child {$childPid} gone away?".PHP_EOL;
      if (posix_kill($childPid, 9)){
        echo "Child {$childPid} has tragically died!".PHP_EOL;
        unset($forks[$key]);
      }
    }
  }
  </code></pre>
  
  <p><strong>I am indeed seeing both echo's</strong> including the relevant and correct child PID that needs to be removed but it seems that </p>
  
  <pre><code>posix_kill($childPid, 9)
  </code></pre>
  
  <p>Which I understand to be synonymous with <code>kill -9 $childPid</code> is returning TRUE although it is in fact NOT removing the process...  </p>
  
  <p>Taken from the <a href="http://php.net/manual/en/function.posix-kill.php" rel="nofollow noreferrer">man pages of <code>posix_kill</code></a> :</p>
  
  <blockquote>
    <p>Returns TRUE on success or FALSE on failure.</p>
  </blockquote>
  
  <hr>
  
  <p>I am monitoring the child processes with the <code>ps</code> command.  They appear like this on the system :</p>
  
  <pre class="lang-none prettyprint-override"><code>web5      5296  5234  0 14:51 ?        00:00:00 [php] &lt;defunct&gt;
  web5      5321  5234  0 14:51 ?        00:00:00 [php] &lt;defunct&gt;
  web5      5466  5234  0 14:52 ?        00:00:00 [php] &lt;defunct&gt;
  </code></pre>
  
  <p>As you can see all these processes are child processes of the parent which has the PID of <code>5234</code></p>
  
  <p>Am I missing something in my understanding?  I seem to have managed to get everything to work (and it does) but I am left with countless zombie processes on the system!</p>
  
  <p>My plans for a zombie apocalypse are rock solid -<br>
  but what on earth can I do when even <code>sudo kill -9</code> does not kill the zombie child processes?</p>
  
  <hr>
  
  <h2>Update 10 Days later</h2>
  
  <p>I've answered this question myself after some additional research, if you are still able to stand my ramblings <a href="https://stackoverflow.com/a/10114945/558021">proceed at will</a>. </p>