<?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 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.pcntl-signal.php',
    1 => 'pcntl_signal',
    2 => 'Installs a signal handler',
  ),
  'up' => 
  array (
    0 => 'ref.pcntl.php',
    1 => 'Функції PCNTL',
  ),
  'prev' => 
  array (
    0 => 'function.pcntl-setpriority.php',
    1 => 'pcntl_setpriority',
  ),
  '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">Опис</h3>
  <div class="methodsynopsis dc-description">
   <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">Параметри</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="para">
       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="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">signal</code></dt>
         <dd>
          <span class="simpara">
           The signal being handled.
          </span>
         </dd>
        
        
         <dt><code class="parameter">siginfo</code></dt>
         <dd>
          <span class="simpara">
           If operating systems supports siginfo_t structures, this will be an array of signal information dependent on the signal.
          </span>
         </dd>
        
       </dl>
      </p>
      <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
       <p class="para">
        Note that when you set a handler to an object method, that object&#039;s
        reference count is increased which makes it persist until you either
        change the handler to something else, or your 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>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.pcntl-signal-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="para">
   Повертає <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> у разі успіху або <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> в разі помилки.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.pcntl-signal-changelog">
  <h3 class="title">Журнал змін</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Версія</th>
       <th>Опис</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <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">Приклади</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Приклад #1 <span class="function"><strong>pcntl_signal()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />pcntl_async_signals</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// signal handler function<br /></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">)<br />{<br /><br />     switch (</span><span style="color: #0000BB">$signo</span><span style="color: #007700">) {<br />         case </span><span style="color: #0000BB">SIGTERM</span><span style="color: #007700">:<br />             </span><span style="color: #FF8000">// handle shutdown tasks<br />             </span><span style="color: #007700">exit;<br />             break;<br />         case </span><span style="color: #0000BB">SIGHUP</span><span style="color: #007700">:<br />             </span><span style="color: #FF8000">// handle restart tasks<br />             </span><span style="color: #007700">break;<br />         case </span><span style="color: #0000BB">SIGUSR1</span><span style="color: #007700">:<br />             echo </span><span style="color: #DD0000">"Caught SIGUSR1...\n"</span><span style="color: #007700">;<br />             break;<br />         default:<br />             </span><span style="color: #FF8000">// handle all other signals<br />     </span><span style="color: #007700">}<br /><br />}<br /><br />echo </span><span style="color: #DD0000">"Installing signal handler...\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// setup signal handlers<br /></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">);<br /></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">);<br /></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">);<br /><br /></span><span style="color: #FF8000">// or use an object<br />// pcntl_signal(SIGUSR1, array($obj, "do_something"));<br /><br /></span><span style="color: #007700">echo</span><span style="color: #DD0000">"Generating signal SIGUSR1 to self...\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// send SIGUSR1 to current process id<br />// posix_* functions require the posix extension<br /></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">);<br /><br />echo </span><span style="color: #DD0000">"Done\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

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


 <div class="refsect1 notes" id="refsect1-function.pcntl-signal-notes">
  <h3 class="title">Примітки</h3>
  <p class="para">
   <span class="function"><strong>pcntl_signal()</strong></span> doesn&#039;t 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">Прогляньте також</h3>
  <p class="para">
   <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>
  </p>
 </div>


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