glibc documentation and endianness
glibc documentation on process com开发者_如何学编程pletion status states that the macro WEXITSTATUS returns the low order 8 bytes of the completion status.
Macro: int WEXITSTATUS (int status)
If WIFEXITED is true of status, this macro returns the low-order 8 bits of the exit status value from the child process.
However, /usr/include/sys/wait.h
says:
# define WEXITSTATUS(status) __WEXITSTATUS (__WAIT_INT (status))
And, /usr/include/bits/waitstatus.h
mentions:
/* If WIFEXITED(STATUS), the low-order 8 bits of the status. */
#define __WEXITSTATUS(status) (((status) & 0xff00) >> 8)
Unless my endian-ness concepts are all messed up, how is this the low-order 8 bits? Or is libc assuming that the data is kept in a small-endian way?
This is not an endianness issue. Endianness refers to how the data is stored in memory; on either a big- or little-endian machine, (((status) & 0xff00) >> 8)
extracts bits 15 through 8, i.e. the 8th through 15th least significant bits of the status
macro argument.
The documentation and comments are confusing because status refers to two different things.
The process that exits returns a status code. This exit status has type int
in the source (either as return value from main
, or as argument to exit
), however, the value should be between 0 and 255.
The wait
and waitpid
system calls also provide a status
back to the caller. This status is different; the low-order 8 bits of the original exit status are now in bits 15 through 8. I assume the documentation says WEXITSTATUS returns the "low-order 8 bits" because that was the packing of the exit status from the perspective of the exiting process.
精彩评论