如何解释waitpid函数发出的进程终止状态
我正在调试以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
参数
wstatus
包含两种状态:使用宏
WIFEXITED
可以测试终止状态,并使用宏WEXITSTATUS
提取退出状态(传递给exit
的代码或从main
函数返回的代码)。请参阅男人等待
从您发布的链接:
最后,作者测试返回值(退出值 - 正常终止)是否为
0
,按照惯例,它代表 <强>成功。The parameter
wstatus
contains two states:With the macro
WIFEXITED
you test the termination status and with the macroWEXITSTATUS
you extract the exit status (the code passed toexit
or returned from themain
function).See man wait
From the link you've posted:
And lastly, the author tests if the return value (exit value - normal termination) is
0
, which by convention stands for success.