<?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-waitpid.php',
    1 => 'pcntl_waitpid',
    2 => 'Waits on or returns the status of a forked child',
  ),
  'up' => 
  array (
    0 => 'ref.pcntl.php',
    1 => 'Функції PCNTL',
  ),
  'prev' => 
  array (
    0 => 'function.pcntl-waitid.php',
    1 => 'pcntl_waitid',
  ),
  'next' => 
  array (
    0 => 'function.pcntl-wexitstatus.php',
    1 => 'pcntl_wexitstatus',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pcntl/functions/pcntl-waitpid.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.pcntl-waitpid" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">pcntl_waitpid</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">pcntl_waitpid</span> &mdash; <span class="dc-title">Waits on or returns the status of a forked child</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.pcntl-waitpid-description">
  <h3 class="title">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">function</span> <span class="methodname"><strong>pcntl_waitpid</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$process_id</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter reference">&$status</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter reference">&$resource_usage</code><span class="initializer"> = []</span></span><br>): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

  <p class="para rdfs-comment">
   Suspends execution of the current process until a child as specified by
   the <code class="parameter">process_id</code> argument has exited, or until a signal is
   delivered whose action is to terminate the current process or to call a
   signal handling function.
  </p>
  <p class="para">
   If a child as requested by <code class="parameter">process_id</code> has already exited
   by the time of the call (a so-called &quot;zombie&quot; process), the function
   returns immediately.  Any system resources used by the child are freed.
   Please see your system&#039;s waitpid(2) man page for specific details as to
   how waitpid works on your system.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.pcntl-waitpid-parameters">
  <h3 class="title">Параметри</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">process_id</code></dt>
     <dd>
      <p class="para">
       The value of <code class="parameter">process_id</code> can be one of the following:
       <table class="doctable table">
        <caption><strong>possible values for <code class="parameter">process_id</code></strong></caption>
        
         <tbody class="tbody">
          <tr>
           <td><code class="literal">&lt; -1</code></td>
           <td>
            wait for any child process whose process group ID is equal to
            the absolute value of <code class="parameter">process_id</code>.
           </td>
          </tr>

          <tr>
           <td><code class="literal">-1</code></td>
           <td>
            wait for any child process; this is the same behaviour that
            the <span class="function"><a href="function.pcntl-wait.php" class="function">pcntl_wait()</a></span> function exhibits.
           </td>
          </tr>

          <tr>
           <td><code class="literal">0</code></td>
           <td>
            wait for any child process whose process group ID is equal to
            that of the calling process.
           </td>
          </tr>

          <tr>
           <td><code class="literal">&gt; 0</code></td>
           <td>
            wait for the child whose process ID is equal to the value of
            <code class="parameter">process_id</code>.
           </td>
          </tr>

         </tbody>
        
       </table>

      </p>
      <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
       <p class="para">
        Specifying <code class="literal">-1</code> as the <code class="parameter">process_id</code> is
        equivalent to the functionality <span class="function"><a href="function.pcntl-wait.php" class="function">pcntl_wait()</a></span> provides
        (minus <code class="parameter">flags</code>).
       </p>
      </p></blockquote>
     </dd>
    
    
     <dt><code class="parameter">status</code></dt>
     <dd>
      <p class="para">
       <span class="function"><strong>pcntl_waitpid()</strong></span> will store status information
       in the <code class="parameter">status</code> parameter which can be
       evaluated using the following functions:
       <span class="function"><a href="function.pcntl-wifexited.php" class="function">pcntl_wifexited()</a></span>,
       <span class="function"><a href="function.pcntl-wifstopped.php" class="function">pcntl_wifstopped()</a></span>,
       <span class="function"><a href="function.pcntl-wifsignaled.php" class="function">pcntl_wifsignaled()</a></span>,
       <span class="function"><a href="function.pcntl-wexitstatus.php" class="function">pcntl_wexitstatus()</a></span>,
       <span class="function"><a href="function.pcntl-wtermsig.php" class="function">pcntl_wtermsig()</a></span> and
       <span class="function"><a href="function.pcntl-wstopsig.php" class="function">pcntl_wstopsig()</a></span>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       The value of <code class="parameter">flags</code> is the value of zero
       or more of the following two global constants
       <code class="literal">OR</code>&#039;ed together:
       <table class="doctable table">
        <caption><strong>possible values for <code class="parameter">flags</code></strong></caption>
        
         <tbody class="tbody">
          <tr>
           <td><strong><code><a href="pcntl.constants.php#constant.wnohang">WNOHANG</a></code></strong></td>
           <td>
            return immediately if no child has exited.
           </td>
          </tr>

          <tr>
           <td><strong><code><a href="pcntl.constants.php#constant.wuntraced">WUNTRACED</a></code></strong></td>
           <td>
            return for children which are stopped, and whose status has
            not been reported.
           </td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>
    
    
     <dt><code class="parameter">resource_usage</code></dt>
     <dd>
      <p class="simpara">
       The <code class="parameter">resource_usage</code> parameter is set to an
       associative <span class="type"><a href="language.types.array.php" class="type array">array</a></span> containing resource usage statistics
       from the child process. For information on the contents see
       <span class="function"><a href="function.getrusage.php" class="function">getrusage()</a></span>.
      </p>
      <p class="simpara">
       This is only supported if the <code class="literal">wait4(2)</code> system call is
       available on the system; otherwise the parameter is left untouched.
       If no child process was reaped, that is if the function returns
       <code class="literal">0</code> or <code class="literal">-1</code>, the parameter is set to
       an empty <span class="type"><a href="language.types.array.php" class="type array">array</a></span>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.pcntl-waitpid-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="para">
   <span class="function"><strong>pcntl_waitpid()</strong></span> returns the process ID of the
   child which exited, -1 on error or zero if <strong><code><a href="pcntl.constants.php#constant.wnohang">WNOHANG</a></code></strong> was used and no
   child was available
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.pcntl-waitpid-examples">
  <h3 class="title">Приклади</h3>
  <div class="example" id="example-1">
   <p><strong>Приклад #1 <span class="function"><strong>pcntl_waitpid()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">function </span><span style="color: #0000BB">checkChild</span><span style="color: #007700">(</span><span style="color: #0000BB">int $pid</span><span style="color: #007700">, </span><span style="color: #0000BB">bool $wait</span><span style="color: #007700">): </span><span style="color: #0000BB">void
</span><span style="color: #007700">{
    </span><span style="color: #0000BB">$flags </span><span style="color: #007700">= (</span><span style="color: #0000BB">$wait </span><span style="color: #007700">? </span><span style="color: #0000BB">0 </span><span style="color: #007700">: </span><span style="color: #0000BB">WNOHANG</span><span style="color: #007700">);
    </span><span style="color: #0000BB">$deadPid </span><span style="color: #007700">= </span><span style="color: #0000BB">pcntl_waitpid</span><span style="color: #007700">(</span><span style="color: #0000BB">$pid</span><span style="color: #007700">, </span><span style="color: #0000BB">$status</span><span style="color: #007700">, </span><span style="color: #0000BB">$flags</span><span style="color: #007700">, </span><span style="color: #0000BB">$rusage</span><span style="color: #007700">);
    if (</span><span style="color: #0000BB">$deadPid </span><span style="color: #007700">=== -</span><span style="color: #0000BB">1</span><span style="color: #007700">) {
        </span><span style="color: #0000BB">$errNo </span><span style="color: #007700">= </span><span style="color: #0000BB">pcntl_get_last_error</span><span style="color: #007700">();
        </span><span style="color: #0000BB">$errMsg </span><span style="color: #007700">= </span><span style="color: #0000BB">pcntl_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">$errNo</span><span style="color: #007700">);
        print </span><span style="color: #DD0000">"PARENT pcntl_waitpid FAILED (</span><span style="color: #007700">{</span><span style="color: #0000BB">$errNo</span><span style="color: #007700">}</span><span style="color: #DD0000">): </span><span style="color: #007700">{</span><span style="color: #0000BB">$errMsg</span><span style="color: #007700">}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;
    } elseif (</span><span style="color: #0000BB">$deadPid </span><span style="color: #007700">=== </span><span style="color: #0000BB">0</span><span style="color: #007700">) {
        print </span><span style="color: #DD0000">"PARENT pcntl_waitpid: No exited children.\n"</span><span style="color: #007700">;
    } elseif (</span><span style="color: #0000BB">pcntl_wifexited</span><span style="color: #007700">(</span><span style="color: #0000BB">$status</span><span style="color: #007700">)) {
        print </span><span style="color: #DD0000">"PARENT pcntl_waitpid: Child PID </span><span style="color: #007700">{</span><span style="color: #0000BB">$deadPid</span><span style="color: #007700">}</span><span style="color: #DD0000"> exited with status: "
            </span><span style="color: #007700">. </span><span style="color: #0000BB">pcntl_wexitstatus</span><span style="color: #007700">(</span><span style="color: #0000BB">$status</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
        </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$rusage</span><span style="color: #007700">);
    } else {
        print </span><span style="color: #DD0000">"PARENT pcntl_waitpid: Child PID </span><span style="color: #007700">{</span><span style="color: #0000BB">$deadPid</span><span style="color: #007700">}</span><span style="color: #DD0000"> was terminated by signal: "
            </span><span style="color: #007700">. </span><span style="color: #0000BB">pcntl_wtermsig</span><span style="color: #007700">(</span><span style="color: #0000BB">$status</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
    }
}

</span><span style="color: #0000BB">$parentPid </span><span style="color: #007700">= </span><span style="color: #0000BB">getmypid</span><span style="color: #007700">();
</span><span style="color: #0000BB">$forkedPid </span><span style="color: #007700">= </span><span style="color: #0000BB">pcntl_fork</span><span style="color: #007700">();
if (</span><span style="color: #0000BB">$forkedPid </span><span style="color: #007700">== -</span><span style="color: #0000BB">1</span><span style="color: #007700">) {
    die(</span><span style="color: #DD0000">'Could not fork'</span><span style="color: #007700">);
} else if (</span><span style="color: #0000BB">$forkedPid</span><span style="color: #007700">) {
    </span><span style="color: #FF8000">// Parent process
    </span><span style="color: #007700">print </span><span style="color: #DD0000">"PARENT </span><span style="color: #007700">{</span><span style="color: #0000BB">$parentPid</span><span style="color: #007700">}</span><span style="color: #DD0000"> has forked </span><span style="color: #007700">{</span><span style="color: #0000BB">$forkedPid</span><span style="color: #007700">}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;

    print </span><span style="color: #DD0000">"PARENT is checking for exited children\n"</span><span style="color: #007700">;
    </span><span style="color: #0000BB">checkChild</span><span style="color: #007700">(</span><span style="color: #0000BB">$forkedPid</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">);

    </span><span style="color: #FF8000">// Prevents children remaining as a "zombie process"
    </span><span style="color: #007700">print </span><span style="color: #DD0000">"PARENT is waiting for a child to exit\n"</span><span style="color: #007700">;
    </span><span style="color: #0000BB">checkChild</span><span style="color: #007700">(</span><span style="color: #0000BB">$forkedPid</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);

    print </span><span style="color: #DD0000">"PARENT is checking for exited children\n"</span><span style="color: #007700">;
    </span><span style="color: #0000BB">checkChild</span><span style="color: #007700">(</span><span style="color: #0000BB">$forkedPid</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">);

    print </span><span style="color: #DD0000">"PARENT is exiting\n"</span><span style="color: #007700">;
} else {
    </span><span style="color: #FF8000">// Child process
    </span><span style="color: #0000BB">$pid </span><span style="color: #007700">= </span><span style="color: #0000BB">getmypid</span><span style="color: #007700">();
    print </span><span style="color: #DD0000">"CHILD </span><span style="color: #007700">{</span><span style="color: #0000BB">$pid</span><span style="color: #007700">}</span><span style="color: #DD0000"> is sleeping\n"</span><span style="color: #007700">;
    </span><span style="color: #0000BB">sleep</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">);
    print </span><span style="color: #DD0000">"CHILD is exiting\n"</span><span style="color: #007700">;
    exit(</span><span style="color: #0000BB">7</span><span style="color: #007700">);
}</span></code></pre></div>
   </div>

  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.pcntl-waitpid-seealso">
  <h3 class="title">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <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.php" class="function" rel="rdfs-seeAlso">pcntl_signal()</a> - Installs a signal handler</span></li>
    <li><span class="function"><a href="function.pcntl-wait.php" class="function" rel="rdfs-seeAlso">pcntl_wait()</a> - Waits on or returns the status of a forked child</span></li>
    <li><span class="function"><a href="function.pcntl-wifexited.php" class="function" rel="rdfs-seeAlso">pcntl_wifexited()</a> - Checks if status code represents a normal exit</span></li>
    <li><span class="function"><a href="function.pcntl-wifstopped.php" class="function" rel="rdfs-seeAlso">pcntl_wifstopped()</a> - Checks whether the child process is currently stopped</span></li>
    <li><span class="function"><a href="function.pcntl-wifsignaled.php" class="function" rel="rdfs-seeAlso">pcntl_wifsignaled()</a> - Checks whether the status code represents a termination due to a signal</span></li>
    <li><span class="function"><a href="function.pcntl-wexitstatus.php" class="function" rel="rdfs-seeAlso">pcntl_wexitstatus()</a> - Returns the return code of a terminated child</span></li>
    <li><span class="function"><a href="function.pcntl-wtermsig.php" class="function" rel="rdfs-seeAlso">pcntl_wtermsig()</a> - Returns the signal which caused the child to terminate</span></li>
    <li><span class="function"><a href="function.pcntl-wstopsig.php" class="function" rel="rdfs-seeAlso">pcntl_wstopsig()</a> - Returns the signal which caused the child to stop</span></li>
   </ul>
  </p>
 </div>


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