title: PHP sleep, pcntl_signal, and ticks
body: <p>Ok, so I've been trying to get <a href="http://www.advancedlinuxprogramming.com/alp-folder/alp-ch03-processes.pdf" rel="noreferrer"><code>Process Signals</code></a> to work with <code>PHP</code> scripts. I've run into a lot of stuff where I am just not sure what is going on, and I feel like the documentation doesn't do a very good job of explaining it.</p>
  
  <p>So let's say I follow the example on <a href="http://www.php.net/manual/en/function.pcntl-signal.php" rel="noreferrer">registering signals</a>:</p>
  
  <pre><code>&lt;?php
  // tick use required as of PHP 4.3.0
  declare(ticks = 1);
  
  // signal handler function
  function sig_handler($signo)
  {
  
       switch ($signo) {
           case SIGTERM:
               // handle shutdown tasks
               exit;
               break;
           case SIGHUP:
               // handle restart tasks
               break;
           case SIGUSR1:
               echo "Caught SIGUSR1...\n";
               break;
           default:
               // handle all other signals
       }
  
  }
  
  echo "Installing signal handler...\n";
  
  // setup signal handlers
  pcntl_signal(SIGTERM, "sig_handler");
  pcntl_signal(SIGHUP,  "sig_handler");
  pcntl_signal(SIGUSR1, "sig_handler");
  
  // or use an object, available as of PHP 4.3.0
  // pcntl_signal(SIGUSR1, array($obj, "do_something"));
  
  echo"Generating signal SIGTERM to self...\n";
  
  // send SIGUSR1 to current process id
  posix_kill(posix_getpid(), SIGUSR1);
  
  echo "Done\n";
  
  ?&gt;
  </code></pre>
  
  <p>However, within my <code>sig_handler</code> function, I add an <code>exit(1)</code> at the end. Will the <code>echo "Done\n";</code> still be executed? I have attempted something similar, albeit in a class structure, and it was- only <em>before</em> my <code>sig_handler</code> ever got called.</p>
  
  <p>My class <code>__construct</code> calls the function <code>registerSignals</code>:</p>
  
  <pre><code>private function registerSignals(){
      if(function_exists("pcntl_signal")){
          //call_user_func([$this, "sigintCleanup"]);
          pcntl_signal(SIGINT, [$this, "sigintCleanup"]);
          pcntl_signal(SIGTERM, [$this, "sigtermCleanup"]);
          pcntl_signal(SIGHUP, [$this, "sighupCleanup"]);
          pcntl_signal(SIGUSR1, [$this, "sigusr1Cleanup"]);
      }
  }
  
  protected function sigintCleanup(){
      echo "In SIGINT cleanup\n";
      $this-&gt;cleanup();
  }
  
  protected function sigtermCleanup(){
      echo "In SIGTERM cleanup\n";
      $this-&gt;cleanup();
  }
  
  protected function sighupCleanup(){
      echo "In SIGHUP cleanup\n";
      $this-&gt;cleanup();
  }
  
  protected function sigusr1Cleanup(){
      echo "In SIGUSR1 cleanup\n";
      $this-&gt;cleanup();
  }
  
  protected function cleanup(){
      echo "Shutting down\n";
      exit(1);
  }
  </code></pre>
  
  <p>And then within my testing of this process, I do some simple <code>sleep</code>s and <code>echo</code>s (<code>start</code> is just a function called after the construction and signal registration is finished):</p>
  
  <pre><code>function start(){
      echo "-- Testing the test script --\n";
      sleep(10000);
      echo "given how ticks work, this might not be called...\n";
      echo "\nAfter sleep... this should not be executed if there was a signal interrupt.\n";
  }
  </code></pre>
  
  <p>So I use <code>ctl-c</code> during the <code>sleep</code>. My result?</p>
  
  <pre><code>-- Testing the test script --
  ^Cgiven how ticks work, this might not be called...
  
  After sleep... this should not be executed if there was a signal interrupt.
  In SIGINT cleanup
  Shutting down
  </code></pre>
  
  <p>As you can see from my <code>echo</code>s, I kinda figured maybe the way <a href="http://www.phpdig.net/ref/rn62.html" rel="noreferrer">ticks</a> work was responsible for finishing the <code>start</code> function <em>before</em> handling the signal. I still think this might be the case. The truth is, I just don't know.</p>
  
  <p>I do declare <code>ticks</code> at the top of my program:</p>
  
  <pre><code>declare(ticks = 1);
  </code></pre>
  
  <p>Not handling signals immediately is causing problems for me. What if, for instance, I had this bit of code in my <code>start</code> function (and I've tested this)?</p>
  
  <pre><code>$count = 0;
  while(true){
      count++;
      echo "count\n";
      sleep(5);
  }
  </code></pre>
  
  <p>Even if my <code>cleanup</code> function has an <code>exit</code>, I never reach that because the <code>SIGINT</code> just knocks me out of <code>sleep</code>, and I continue in the loop and begin <code>sleep</code>ing again.</p>
  
  <p>So here's my question: How does <code>signal</code> handling work in <code>PHP</code>? How can I guarantee that when I get a signal, it is handled immediately? I don't just want some piece of code that will fix this, I would like an <em>explanation</em> (or at least a link to a good one and a summary).</p>
  
  <p>PHP version: 5.4.22</p>
  
  <p>OS: Ubuntu 12.10</p>