为什么等待()将状态设置为256,而不是派生进程的-1退出状态? [英] Why does wait() set status to 256 instead of the -1 exit status of the forked process?
问题描述
我想从一个子进程返回一个整数值。
I'm trying to return an integer value from a child process.
不过,如果我使用退出(1)我得到的 256 作为输出。 退出(-1)给 65280
However, if I use exit(1) i get 256 as the output. exit(-1) gives 65280.
有没有一种方法,我可以得到我从子进程发送实际的int值?
Is there a way I can get the actual int value that I send from the child process?
if(!(pid=fork()))
{
exit(1);
}
waitpid(pid,&status,0);
printf("%d",status);
编辑:是退出(-1)(这是我真正想要的)我得到255的输出WEXITSTATUS(状态)。难道应该是无符号的?
Using exit(-1) (which is what I actually want) I am getting 255 as the output for WEXITSTATUS(status). Is it supposed to be unsigned?
推荐答案
您是否尝试过人waitpid函数?
Have you tried "man waitpid"?
从waitpid函数()调用返回的值是退出值的编码。有一组宏,将提供原出口价值。或者你可以尝试用权移入8位的值,如果你不关心可移植性。
The value returned from the waitpid() call is an encoding of the exit value. There are a set of macros that will provide the original exit value. Or you can try right shifting the value by 8 bits, if you don't care about portability.
您code的移植版将是:
The portable version of your code would be:
if(!(pid=fork()))
{
exit(1);
}
waitpid(pid,&status,0);
if (WIFEXITED(status)) {
printf("%d", WEXITSTATUS(status));
}
这篇关于为什么等待()将状态设置为256,而不是派生进程的-1退出状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!