pcntl_wifexited总是返回true

时间:2011-01-20 17:06:23

标签: php multiprocessing pcntl

子进程可能会因错误而退出,但pcntl_wifexited总是返回true

<?php

//Sample code :
$child_pid = pcntl_fork();

if ($child_pid === 0) 
{
    //This is child process

    $my_var = rand(1,5);

    if($my_var == 2)
    {
        //2 is not allowed
        exit(1); //exit with error
    }

    if($my_var == 4)
    {
        //4 is unknown
        i_crash_now(); //crash
    }

    echo 'end of child' . "\n";
    exit(0); //exit standard

}
else
{
    sleep(1); //waiting for child with ninja code (don't do that at home, this was made by professional ninjas)
    pcntl_waitpid($child_pid, $status, WNOHANG);

    var_dump(pcntl_wstopsig($status));  //in case 2 > 1, in case 4 > 255, else 0
    var_dump(pcntl_wifexited($status)); //always true;
}

exit(0);

我可能会使用该信号来查找错误,但我不知道pcntl_wifexited()有什么问题。

这与WNOHANG选项有关吗?

1 个答案:

答案 0 :(得分:0)

我认为pcntl_waitpid的行为类似于正常的waitpid调用。

如果没有进程终止,WNOHANG强制waitpid返回0 [它类似于零超时]。

因此,退出代码似乎是正常的。