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.
I think you will find that 0x377
is really, or should have been, 0377
.
It's octal, so 377 8 is 8 bits.
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 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.