如何解释waitpid函数发出的进程终止状态

发布于 2025-01-11 21:10:25 字数 1333 浏览 0 评论 0原文

我正在调试以下代码:

if(0 == (pid = fork()))
{
    if(-1 == execv(p_Command[0], (char **)p_Command))
    {
        ret = -1;
        printf("Fork error on command '%s'", (nullptr == p_Command[0])?"nullptr":p_Command[0]);
    }
}
// Fork error
else if(-1 == pid)
{
    printf("Fork error on command '%s'", (nullptr == p_Command[0])?"nullptr":p_Command[0]);
    ret = -1;
}
// In parent process
else
{
    // Wait for child
    while((0 == waitpid(pid , &status , WNOHANG)) && (timeout != 0))
    {
        if(timeout > 0)
        {
            --timeout;
            sleep(1);
        }
    }

    TRACE("after wait pid = %d", pid);

    TRACE("status = %d", status);

    if(1 != WIFEXITED(status))
    {
        ret = -1;
        printf("WIFEXITED error");
    }
    else if(0 != WEXITSTATUS(status))
    {
        ret = -1;
        printf("WEXITSTATUS error");
    }

    else
    {
        ret = pid;
    }
}
return ret;

}

我主要关心的是 waitpid 函数,它返回状态 65280,因此 WEXITSTATUS(status) 计算结果为 254。我试图理解为什么它计算为该值以及作者的原因希望它评估为 0 以表示成功返回。

有人知道吗?

我读过这个发布 WEXITSTATUS(status) 计算结果为 0 到 255 之间的值,但到目前为止我还没有找到有关与这些值关联的含义的任何信息,以便我可以调查宏计算结果的原因255

I am debugging the following code :

if(0 == (pid = fork()))
{
    if(-1 == execv(p_Command[0], (char **)p_Command))
    {
        ret = -1;
        printf("Fork error on command '%s'", (nullptr == p_Command[0])?"nullptr":p_Command[0]);
    }
}
// Fork error
else if(-1 == pid)
{
    printf("Fork error on command '%s'", (nullptr == p_Command[0])?"nullptr":p_Command[0]);
    ret = -1;
}
// In parent process
else
{
    // Wait for child
    while((0 == waitpid(pid , &status , WNOHANG)) && (timeout != 0))
    {
        if(timeout > 0)
        {
            --timeout;
            sleep(1);
        }
    }

    TRACE("after wait pid = %d", pid);

    TRACE("status = %d", status);

    if(1 != WIFEXITED(status))
    {
        ret = -1;
        printf("WIFEXITED error");
    }
    else if(0 != WEXITSTATUS(status))
    {
        ret = -1;
        printf("WEXITSTATUS error");
    }

    else
    {
        ret = pid;
    }
}
return ret;

}

My principal concern is about the waitpid function, it is returning a status of 65280 and so WEXITSTATUS(status) evaluates to 254. I am trying to understand why it is evaluating to that value and why the author wants it to evaluate to 0 for a success return.

Does anyone have a clue?

I read in this post that WEXITSTATUS(status) evaluates to a value between 0 and 255, but I haven't till now found any information about the meaning associated to those values so that I can investigate on why the macro evaluates to 255

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

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

发布评论

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

评论(1

星星的轨迹 2025-01-18 21:10:25

参数 wstatus 包含两种状态:

  • 终止状态(正常或异常终止)
  • 退出状态(仅在正常终止时有效)

使用宏 WIFEXITED 可以测试终止状态,并使用宏 WEXITSTATUS 提取退出状态(传递给 exit 的代码或从 main 函数返回的代码)。

请参阅男人等待

WIFEXITED(wstatus)
如果子进程正常终止,即通过调用 exit(3) 或 _exit(2),或者通过返回,则返回 true来自 main()。

WEXITSTATUSwstatus
返回子进程的退出状态。这包括
状态参数的最低有效 8 位
在调用 exit(3) 或 _exit(2) 时指定的子级或作为
main() 中 return 语句的参数。这个宏
仅当 WIFEXITED 返回 true 时才应使用。

从您发布的链接:

...

status的低位字节包含终止状态,0为正常终止,其他值为各种错误情况。高位字节包含子进程的退出状态(0 到 255)。”

最后,作者测试返回值(退出值 - 正常终止)是否为 0,按照惯例,它代表 <强>成功。

The parameter wstatus contains two states:

  • termination status (normal or abnormal termination)
  • exit status (only valid on normal termination)

With the macro WIFEXITED you test the termination status and with the macro WEXITSTATUS you extract the exit status (the code passed to exit or returned from the main function).

See man wait

WIFEXITED(wstatus)
returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main().

WEXITSTATUS(wstatus)
returns the exit status of the child. This consists of
the least significant 8 bits of the status argument that
the child specified in a call to exit(3) or _exit(2) or as
the argument for a return statement in main(). This macro
should be employed only if WIFEXITED returned true.

From the link you've posted:

...

The low-order byte of status contains the termination status, with 0 being normal termination and the other values being various error conditions. The high-order byte contains the child's exit status (0 to 255)."

And lastly, the author tests if the return value (exit value - normal termination) is 0, which by convention stands for success.

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