简体   繁体   English

程序退出 state

[英]Program exit state

I have a question about program exit state in Linux.我对 Linux 中的程序退出 state 有疑问。 In my program, I fork a child process and invoke waitpid to reap it.在我的程序中,我 fork 一个子进程并调用waitpid来获取它。 When waitpid returns, I wanna check exit state of my child process.waitpid返回时,我想检查我的子进程的退出 state。 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.我转向手册寻求帮助,发现waitpid的第二个参数将保留 exit state ,我可以使用宏WEXITSTATE来读取它。 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.但是,此宏仅提取实际退出 state 的最低有效 8 位,而在 function exit(int ret_val)的手册中,它将以ret_val & 0x377退出,而不是最低有效 8 位。

My question is, where is the other more bits?我的问题是,其他更多的位在哪里? Do we simply drop them?我们只是放弃它们吗? Why Linux employ this strategy?为什么 Linux 采用这种策略? Doesn't this implementation introduce trouble to our program?这个实现不会给我们的程序带来麻烦吗?

Thanks and Best Regards.谢谢和最好的问候。

I think you will find that 0x377 is really, or should have been, 0377 .我想你会发现0x377真的是,或者应该是, 0377

It's octal, so 377 8 is 8 bits.它是八进制的,所以377 8是 8 位。

Exit return values are only suppose to be between 0 and 255 per the POSIX spec.根据 POSIX 规范,退出返回值仅假设在 0 到 255 之间。 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).您不应该返回高于该值的值(换句话说,符合 POSIX 的操作系统将只关心退出返回值的低八位,这就是将传递给父进程的全部内容)。

Unix/POSIX only supports 8 bits. Unix/POSIX 仅支持 8 位。 10 bits would be an odd (in both mathematical and logical senses) value, so I'd have to agree with @DigitalRoss. 10 位将是一个奇数(在数学和逻辑意义上)值,所以我必须同意@DigitalRoss。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM