downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

pcntl_wexitstatus> <pcntl_wait
Last updated: Fri, 13 Nov 2009

view this page in

pcntl_waitpid

(PHP 4 >= 4.1.0, PHP 5)

pcntl_waitpid待つかフォークした子プロセスのステータスを返す

説明

int pcntl_waitpid ( int $pid , int &$status [, int $options = 0 ] )

引数 pid で指定した子プロセスが終了する・ 現在のプロセスを終了させるシグナルが送信される・シグナル処理関数を コールするシグナルが送信される のいずれかが発生するまで、現在のプロセスの実行を中断します。

pid でリクエストされた子プロセスが、 コール時に 既に終了している場合("ゾンビ"プロセスと呼ばれます)、この関数は 直ちに処理を返します。子プロセスにより使用される全てのシステム リソースは、解放されます。waitpid のシステムでの動作に関する詳細は、 システムの waitpid(2) についての man ページを参照ください。

パラメータ

pid

pid の値は、次のどれかとなります。

pid のとりうる値
< -1 プロセスグループ ID が pid の絶対値に等しい 子プロセスを待ちます。
-1 全ての子プロセスを待ちます。これは、wait 関数の動作と同じです。
0 プロセスグループ ID がコール側のプロセスと等しい子プロセスを 待ちます。
> 0 プロセス ID が pid の値に等しい 子プロセスを待ちます。

注意: -1pid に指定した際の動きは、 pcntl_wait() の機能と (options を除いて) 同じです。

status

pcntl_waitpid() は、パラメータ status の中にステータス情報を保存します。 このステータスは、次の関数を用いて評価可能です。 pcntl_wifexited()pcntl_wifstopped()pcntl_wifsignaled()pcntl_wexitstatus()pcntl_wtermsig() および pcntl_wstopsig()

options

options の値は、次の 2 つのグローバル定数の ゼロまたはそれ以上の論理和です。

options のとりうる値
WNOHANG 子プロセスが終了していない場合に直ちに処理を返します。
WUNTRACED 停止した子プロセスの場合に処理を返します。そして、ステータス は報告されません。

返り値

pcntl_waitpid() は、終了した子プロセスの プロセス ID を返します。エラーの場合は -1、WNOHANG が使用され、 子プロセスが利用できない場合に 0 を返します。

参考

  • pcntl_fork() - 現在実行中のプロセスをフォークする
  • pcntl_signal() - シグナルハンドラを設定する
  • pcntl_wifexited() - ステータスコードが正常終了を表しているかどうかを調べる
  • pcntl_wifstopped() - 子プロセスが現在停止しているかどうかを調べる
  • pcntl_wifsignaled() - ステータスコードがシグナルによる終了を表しているかどうかを調べる
  • pcntl_wexitstatus() - 終了した子プロセスのリターンコードを返す
  • pcntl_wtermsig() - 子プロセスの終了を生じたシグナルを返す
  • pcntl_wstopsig() - 子プロセスを停止させたシグナルを返す



pcntl_wexitstatus> <pcntl_wait
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
pcntl_waitpid
brian dot ngure at gmail dot com
08-Jul-2009 03:03
Please note that you must use bitwise OR | in the following:

"The value of options is the value of zero or more of the following two global constants OR'ed together"

i.e.

WNOHANG | WUNTRACED
tunderzone at gmail dot com
25-Jun-2009 01:26
A better way to do this and not end up having zombie processes until all child processes ends is like this:

<?php
        $i
= 0;
       
$starttime = microtime(TRUE);
       
$pid_arr = array();
        while (
$i < intval($argv[1]))
        {
           
$pid = pcntl_fork();
            if (
$pid == -1)
            {
                die(
'could not fork');
            }
            else
            {
                if (
$pid) // parent
               
{
                   
$pid_arr[$i] = $pid;
                }
                else
// child
               
{
                   
performSomeFunction($i+1);
                }
            }
           
$i++;
        }

        while(
count($pid_arr) > 0)
        {
               
$myId = pcntl_waitpid(-1, $status, WNOHANG);
                foreach(
$pid_arr as $key => $pid)
                {
                        if(
$myId == $pid) unset($pid_arr[$key]);
                }
               
usleep(100);
        }

       
$elapsed = microtime(TRUE) - $starttime;
        print
"\n==> total elapsed: " . sprintf("%f secs.\n", $elapsed);
?>
saguto dot l7cc at gmail dot com
10-Apr-2008 04:09
please note, if you using configure option --enable-sigchild(Enable PHP's own SIGCHLD handler) when complie php(under linux 2.6.18-53.1.13.el5.centos.plus and php 5.2.5 as I know), pcntl_waitpid and pcntl_wait in php script would never return the child pid, because the build in handle get it first.
Kevin
10-May-2006 10:40
---
       while ($i < intval($argv[1]))
       {
           $pid = pcntl_fork();
           if ($pid == -1)
           {
               die('could not fork');
           }
           else
           {
               if ($pid) // parent
               {
                   $pid_arr[$i] = $pid;
               }
               else // child
               {
                   performSomeFunction($i+1);
               }
           }
           $i++;
       }
---

careful, this will create a lot more children than you probably expect. You must return or exit after performSomeFunction($i+1); ie,

               else // child
               {
                   performSomeFunction($i+1);
                   exit(0);
               }
admin at albert dot us dot com
06-Mar-2006 06:48
Here's a decent example of the pcntl_waitpid() call:

        $i = 0;
        $starttime = microtime(TRUE);
        $pid_arr = array();
        while ($i < intval($argv[1]))
        {
            $pid = pcntl_fork();
            if ($pid == -1)
            {
                die('could not fork');
            }
            else
            {
                if ($pid) // parent
                {
                    $pid_arr[$i] = $pid;
                }
                else // child
                {
                    performSomeFunction($i+1);
                }
            }
            $i++;
        }
        foreach ($pid_arr as $pid)
        {
            // we are the parent
            pcntl_waitpid($pid, $status);
        }
        $elapsed = microtime(TRUE) - $starttime;
        print "\n==> total elapsed: " . sprintf("%f secs.\n", $elapsed);

pcntl_wexitstatus> <pcntl_wait
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites