<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.pcntl.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.pcntl-signal.php',
    1 => 'pcntl_signal',
    2 => 'Installs a signal handler',
  ),
  'up' => 
  array (
    0 => 'ref.pcntl.php',
    1 => 'PCNTL Functions',
  ),
  'prev' => 
  array (
    0 => 'function.pcntl-setqos-class.php',
    1 => 'pcntl_setqos_class',
  ),
  'next' => 
  array (
    0 => 'function.pcntl-signal-dispatch.php',
    1 => 'pcntl_signal_dispatch',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pcntl/functions/pcntl-signal.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.pcntl-signal" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">pcntl_signal</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">pcntl_signal</span> &mdash; <span class="dc-title">Installs a signal handler</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.pcntl-signal-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">function</span> <span class="methodname"><strong>pcntl_signal</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$signal</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span>|<span class="type"><a href="language.types.integer.php" class="type int">int</a></span></span> <code class="parameter">$handler</code></span>, <span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$restart_syscalls</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></span></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   The <span class="function"><strong>pcntl_signal()</strong></span> function installs a new
   signal handler or replaces the current signal handler for the signal indicated by <code class="parameter">signal</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.pcntl-signal-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">signal</code></dt>
     <dd>
      <p class="para">
       The signal number.
      </p>
     </dd>
    
    
     <dt><code class="parameter">handler</code></dt>
     <dd>
      <p class="simpara">
       The signal handler. This may be either a <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span>, which
       will be invoked to handle the signal, or either of the two global
       constants <strong><code><a href="pcntl.constants.php#constant.sig-ign">SIG_IGN</a></code></strong> or <strong><code><a href="pcntl.constants.php#constant.sig-dfl">SIG_DFL</a></code></strong>,
       which will ignore the signal or restore the default signal handler
       respectively.
      </p>
      <p class="para">
       If a <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> is given, it must implement the following
       signature:
      </p>
      <p class="para">
       <div class="methodsynopsis dc-description">
        <span class="modifier">function</span> <span class="methodname"><span class="replaceable">handler</span></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$signo</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$siginfo</code></span>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>

       <dl>
        
         <dt><code class="parameter">signo</code></dt>
         <dd>
          <p class="simpara">
           The signal being handled.
          </p>
         </dd>
        
        
         <dt><code class="parameter">siginfo</code></dt>
         <dd>
          <p class="simpara">
           If operating systems supports siginfo_t structures, this will be an array of signal information dependent on the signal.
          </p>
         </dd>
        
       </dl>
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="simpara">
        When a handler is set to an object method, the reference count of that
        object is increased, which makes it persist until the handler is
        changed to something else, or the script ends.
       </p>
      </p></blockquote>
     </dd>
    
    
     <dt><code class="parameter">restart_syscalls</code></dt>
     <dd>
      <p class="para">
       Specifies whether system call restarting should be used when this
       signal arrives.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="simpara">
        Although this parameter defaults to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, that default does not
        apply to <strong><code><a href="pcntl.constants.php#constant.sigalrm">SIGALRM</a></code></strong>: when
        <code class="parameter">restart_syscalls</code> is not passed and
        <code class="parameter">signal</code> is <strong><code><a href="pcntl.constants.php#constant.sigalrm">SIGALRM</a></code></strong>,
        system call restarting is disabled. Pass <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> explicitly to
        enable system call restarting for <strong><code><a href="pcntl.constants.php#constant.sigalrm">SIGALRM</a></code></strong>.
       </p>
      </p></blockquote>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.pcntl-signal-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> on success or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.pcntl-signal-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="simpara">
   A <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> is thrown if
   <code class="parameter">signal</code> is less than <code class="literal">1</code> or greater
   than or equal to <strong><code>NSIG</code></strong>, or if
   <code class="parameter">handler</code> is an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> other than
   <strong><code><a href="pcntl.constants.php#constant.sig-dfl">SIG_DFL</a></code></strong> or <strong><code><a href="pcntl.constants.php#constant.sig-ign">SIG_IGN</a></code></strong>.
  </p>
  <p class="simpara">
   A <span class="exceptionname"><a href="class.typeerror.php" class="exceptionname">TypeError</a></span> is thrown if
   <code class="parameter">handler</code> is neither a <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> nor an
   <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.pcntl-signal-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.4.0</td>
       <td>
        <code class="parameter">restart_syscalls</code> is now honoured for
        <strong><code><a href="pcntl.constants.php#constant.sigalrm">SIGALRM</a></code></strong>; previously system call restarting was
        always disabled for that signal. When the argument is not passed,
        system call restarting remains disabled for
        <strong><code><a href="pcntl.constants.php#constant.sigalrm">SIGALRM</a></code></strong>.
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        As of PHP 7.1.0 the handler callback is given a second argument
        containing the siginfo of the specific signal.  This data is only
        supplied if the operating system has the siginfo_t structure.
        If the OS does not implement siginfo_t NULL is supplied.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.pcntl-signal-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>pcntl_signal()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
pcntl_async_signals</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);

</span><span style="color: #FF8000">// signal handler function
</span><span style="color: #007700">function </span><span style="color: #0000BB">sig_handler</span><span style="color: #007700">(</span><span style="color: #0000BB">$signo</span><span style="color: #007700">)
{

     switch (</span><span style="color: #0000BB">$signo</span><span style="color: #007700">) {
         case </span><span style="color: #0000BB">SIGTERM</span><span style="color: #007700">:
             </span><span style="color: #FF8000">// handle shutdown tasks
             </span><span style="color: #007700">exit;
             break;
         case </span><span style="color: #0000BB">SIGHUP</span><span style="color: #007700">:
             </span><span style="color: #FF8000">// handle restart tasks
             </span><span style="color: #007700">break;
         case </span><span style="color: #0000BB">SIGUSR1</span><span style="color: #007700">:
             echo </span><span style="color: #DD0000">"Caught SIGUSR1...\n"</span><span style="color: #007700">;
             break;
         default:
             </span><span style="color: #FF8000">// handle all other signals
     </span><span style="color: #007700">}

}

echo </span><span style="color: #DD0000">"Installing signal handler...\n"</span><span style="color: #007700">;

</span><span style="color: #FF8000">// setup signal handlers
</span><span style="color: #0000BB">pcntl_signal</span><span style="color: #007700">(</span><span style="color: #0000BB">SIGTERM</span><span style="color: #007700">, </span><span style="color: #DD0000">"sig_handler"</span><span style="color: #007700">);
</span><span style="color: #0000BB">pcntl_signal</span><span style="color: #007700">(</span><span style="color: #0000BB">SIGHUP</span><span style="color: #007700">,  </span><span style="color: #DD0000">"sig_handler"</span><span style="color: #007700">);
</span><span style="color: #0000BB">pcntl_signal</span><span style="color: #007700">(</span><span style="color: #0000BB">SIGUSR1</span><span style="color: #007700">, </span><span style="color: #DD0000">"sig_handler"</span><span style="color: #007700">);

</span><span style="color: #FF8000">// or use an object
// pcntl_signal(SIGUSR1, array($obj, "do_something"));

</span><span style="color: #007700">echo</span><span style="color: #DD0000">"Generating signal SIGUSR1 to self...\n"</span><span style="color: #007700">;

</span><span style="color: #FF8000">// send SIGUSR1 to current process id
// posix_* functions require the posix extension
</span><span style="color: #0000BB">posix_kill</span><span style="color: #007700">(</span><span style="color: #0000BB">posix_getpid</span><span style="color: #007700">(), </span><span style="color: #0000BB">SIGUSR1</span><span style="color: #007700">);

echo </span><span style="color: #DD0000">"Done\n"</span><span style="color: #007700">;

</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.pcntl-signal-notes">
  <h3 class="title">Notes</h3>
  <p class="simpara">
   <span class="function"><strong>pcntl_signal()</strong></span> does not stack the signal handlers, but
   replaces them.
  </p>
  <div class="refsect2 unknown-25" id="refsect2-function.pcntl-signal-unknown-25">
   <h4 class="title">Dispatch Methods</h4>
   <p class="para">
    There are several methods of dispatching signal handlers:
    <ul class="simplelist">
     <li>Asynchronous dispatch with <span class="function"><a href="function.pcntl-async-signals.php" class="function">pcntl_async_signals()</a></span> enabled. This is the recommended method</li>
     <li>Setting <a href="control-structures.declare.php#control-structures.declare.ticks" class="link">tick</a> frequency</li>
     <li>Manual dispatch with <span class="function"><a href="function.pcntl-signal-dispatch.php" class="function">pcntl_signal_dispatch()</a></span></li>
    </ul>
   </p>
   <p class="para">
    When signals are dispatched asynchronously or using tick-based execution, blocking functions like
    <span class="function"><a href="function.sleep.php" class="function">sleep()</a></span> may be interrupted.
   </p>
  </div>

 </div>


 <div class="refsect1 seealso" id="refsect1-function.pcntl-signal-seealso">
  <h3 class="title">See Also</h3>
  <ul class="simplelist">
   <li><a href="https://en.wikipedia.org/wiki/Signal_(IPC)" class="link external">&raquo;&nbsp;Signal (IPC)</a> on Wikipedia</li>
   <li><span class="function"><a href="function.pcntl-async-signals.php" class="function" rel="rdfs-seeAlso">pcntl_async_signals()</a> - Enable/disable asynchronous signal handling or return the old setting</span></li>
   <li><span class="function"><a href="function.pcntl-fork.php" class="function" rel="rdfs-seeAlso">pcntl_fork()</a> - Forks the currently running process</span></li>
   <li><span class="function"><a href="function.pcntl-signal-dispatch.php" class="function" rel="rdfs-seeAlso">pcntl_signal_dispatch()</a> - Calls signal handlers for pending signals</span></li>
   <li><span class="function"><a href="function.pcntl-waitpid.php" class="function" rel="rdfs-seeAlso">pcntl_waitpid()</a> - Waits on or returns the status of a forked child</span></li>
  </ul>
 </div>


</div><?php manual_footer($setup); ?>