pcntl_wifexited 总是返回 true

发布于 2024-10-14 00:00:12 字数 871 浏览 9 评论 0原文

好吧,子进程可能会因错误退出,但 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 选项有关吗?

Well the child process may exit with error but pcntl_wifexited always return 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);

I may use the signal to find errors, but I don't get what's wrong with pcntl_wifexited().

Is this related to WNOHANG option?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久随 2024-10-21 00:00:12

我想 pcntl_waitpid 的行为与正常的 waitpid 调用类似。

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

因此,退出代码看起来是正常的。

I imagine the pcntl_waitpid behaves similarly to the normal waitpid call.

WNOHANG forces waitpid to return 0 if no process is terminated [it's akin to a zero timeout].

Hence, exit code will appear to be normal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文