3

我正在进行父母/工人安排。父进程将工作进程 PID 保存在一个数组中,并通过以下循环不断检查它们是否还活着:

// $workers is an array of PIDs
foreach ($workers as $workerID => $pid) {
    // Check if this worker still exists as a process
    pcntl_waitpid($pid, $status, WNOHANG|WUNTRACED);

    // If the worker exited normally, stop tracking it
    if (pcntl_wifexited($status)) {
        $logger->info("Worker $workerID exited normally");
        array_splice($workers, $workerID, 1); 
    }

    // If it has a session ID, then it's still living
    if (posix_getsid($pid))⋅
        $living[] = $pid;
}

// $dead is the difference between workers we've started
// and those that are still running
$dead = array_diff($workers, $living);

问题是它pcntl_waitpid()总是设置$status为 0,所以第一次运行这个循环时,父级认为它的所有子级都正常退出,即使它们仍在运行。我是在pcntl_waitpid()错误地使用,还是期望它做一些它没有做的事情?

4

2 回答 2

3

很简单,孩子没有退出或停止。您添加了WNOHANG标志,因此它总是会立即返回(它告诉函数不要等待事件)。你应该做的是检查返回值,pcntl_waitpid看看是否返回了任何有价值的东西(假设你只想在状态发生变化时运行循环的内容):

foreach ($workers as $workerID => $pid) {
    // Check if this worker still exists as a process
    if (pcntl_waitpid($pid, $status, WNOHANG|WUNTRACED)) {
        // If the worker exited normally, stop tracking it
        if (pcntl_wifexited($status)) {
            $logger->info("Worker $workerID exited normally");
            array_splice($workers, $workerID, 1); 
        }
        // If it has a session ID, then it's still living
        if (posix_getsid($pid))⋅
            $living[] = $pid;
    }
}    
于 2011-01-03T16:27:01.837 回答
2

您确实“使用pcntl_waitpid()错误”(注意引号)

由于您使用的是WNOHANG,因此只有pcntl_waitpid()返回孩子的 PID,您才能评估$status.

请参阅的返回pcntl_waitpid()

于 2011-01-03T16:27:05.997 回答