title: PHP - Killing child process started by pcntl_fork
body: <p>I am using pcntl_fork to start a child process to send an email via SMTP.</p>
  
  <p>The child process uses the PEAR Mail package to send the email, but the trouble is if the remote server doesn't respond the process just runs forever waiting for a response, regardless of any time limit that is set in php.ini.</p>
  
  <p>To get around this I'm using pcntl_alarm function to trigger a function after 30 seconds that kills the child process if it's still running.</p>
  
  <pre><code>function handlesig($sig) {
      global $pid,$node,$resend;
      posix_kill($pid,SIGKILL);
      mysql_query("insert into log (event) values ('Timed out!')");
  }
  </code></pre>
  
  <p>When I kill the child process though I'm left with a defunct process on the system.</p>
  
  <p>Is there a different signal I should use that will still force the child process to die without waiting for the connection (because the connection will never finish) and avoid a build up of defunct processes?</p>