程序退出状态
我有一个关于 Linux 中程序退出状态的问题。在我的程序中,我分叉了一个子进程并调用 waitpid 来获取它。当 waitpid
返回时,我想检查子进程的退出状态。我转向手册寻求帮助,发现 waitpid
的第二个参数将保存退出状态,我可以使用宏 WEXITSTATE
来读取它。然而,这个宏只是提取真实退出状态的最低有效8位,而在函数exit(int ret_val)
手册中,它将以ret_val & 退出。 0x377
,而不是最低有效 8 位。
我的问题是,其他更多位在哪里?我们就简单地放弃它们吗?为什么Linux采用这种策略?这样的实现不会给我们的程序带来麻烦吗?
谢谢并致以最诚挚的问候。
I have a question about program exit state in Linux. In my program, I fork a child process and invoke waitpid
to reap it. When waitpid
returns, I wanna check exit state of my child process. I turn to manual for help and find that the second argument of waitpid
will hold exit state and I can use macro WEXITSTATE
to read it. However, this macro just extract least significant 8 bits of the real exit state, while in manual of function exit(int ret_val)
, it will exit with ret_val & 0x377
, instead of least significant 8 bits.
My question is, where is the other more bits? Do we simply drop them? Why Linux employ this strategy? Doesn't this implementation introduce trouble to our program?
Thanks and Best Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想您会发现
0x377
确实是,或者应该是0377
。它是八进制的,所以 3778 是8位。
I think you will find that
0x377
is really, or should have been,0377
.It's octal, so 3778 is 8 bits.
根据 POSIX 规范,退出返回值仅假定在 0 到 255 之间。您不应该返回高于该值的值(换句话说,符合 POSIX 标准的操作系统将只关心退出返回值的低八位,这就是传递给父进程的全部内容)。
Exit return values are only suppose to be between 0 and 255 per the POSIX spec. You shouldn't be returning values higher than that (in other words a POSIX-compliant OS will only be concerned with the lower eight-bits of your exit return value, and that's all that will be passed to the parent process).
Unix/POSIX 仅支持 8 位。 10 位将是一个奇数(在数学和逻辑意义上)值,所以我必须同意@DigitalRoss。
Unix/POSIX only supports 8 bits. 10 bits would be an odd (in both mathematical and logical senses) value, so I'd have to agree with @DigitalRoss.