CakeFest 2024: The Official CakePHP Conference

pcntl_waitpid

(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)

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

説明

pcntl_waitpid(
    int $process_id,
    int &$status,
    int $flags = 0,
    array &$resource_usage = []
): int

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

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

パラメータ

process_id

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

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

注意:

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

status

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

flags

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

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

戻り値

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

参考

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

add a note

User Contributed Notes 3 notes

up
3
saguto dot l7cc at gmail dot com
15 years ago
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.
up
0
fx4084 at gmail dot com
9 years ago
<?php
$childs
= array();

// Fork some process.
for($i = 0; $i < 10; $i++) {
$pid = pcntl_fork();
if(
$pid == -1)
die(
'Could not fork');

if (
$pid) {
echo
"parent \n";
$childs[] = $pid;
} else {
// Sleep $i+1 (s). The child process can get this parameters($i).
sleep($i+1);

// The child process needed to end the loop.
exit();
}
}

while(
count($childs) > 0) {
foreach(
$childs as $key => $pid) {
$res = pcntl_waitpid($pid, $status, WNOHANG);

// If the process has already exited
if($res == -1 || $res > 0)
unset(
$childs[$key]);
}

sleep(1);
}
?>
up
-1
renmengyang567 at gmail dot com
4 years ago
<?php

declare(ticks = 1);
function
zp_handler($signal) {
$id = pcntl_waitpid(-1, $status, WNOHANG);
if (
pcntl_wifexited($status))
{
printf("Removed Chlid id: %d \n",$id);
printf("Chlid status: %d \n",pcntl_wexitstatus($status));
}
}

//pcntl_signal_dispatch();
pcntl_signal(SIGCHLD, "zp_handler");
//pcntl_signal_dispatch();
//

$pid = pcntl_fork();
if (
$pid == 0)
{
print
"#1 Hi, I'm child process".PHP_EOL;
sleep(3);
return
10;
}
else
{
print
"#1parent process id:".$pid.PHP_EOL;
$pid = pcntl_fork();
if (
$pid == 0)
{ print
"#2 Hi, I'm child process".PHP_EOL;
sleep(10);
exit(
20);
}
else
{
print
"#2parent process id:".$pid.PHP_EOL;
for (
$i=0; $i <10 ; $i++) {
print
"wait..".PHP_EOL;
sleep(10);
}
}
}
?>
To Top