我正在进行父母/工人安排。父进程将工作进程 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()
错误地使用,还是期望它做一些它没有做的事情?