glibc 文档和字节顺序
有关进程完成状态的 glibc 文档 指出宏 WEXITSTATUS 返回完成状态的低 8 字节。
宏:int WEXITSTATUS (int status)
如果 WIFEXITED 状态为 true,则该宏返回子进程退出状态值的低 8 位。
然而,/usr/include/sys/wait.h
说:
# define WEXITSTATUS(status) __WEXITSTATUS (__WAIT_INT (status))
并且,/usr/include/bits/waitstatus.h
提到:
/* If WIFEXITED(STATUS), the low-order 8 bits of the status. */
#define __WEXITSTATUS(status) (((status) & 0xff00) >> 8)
除非我的字节顺序概念都是搞砸了,这怎么是低位 8 位?或者 libc 是否假设数据以小端方式保存?
glibc documentation on process completion 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是字节顺序问题。字节序是指数据在内存中的存储方式;在大端或小端机器上,
(((status) & 0xff00) >> 8)
提取位 15 到 8,即 < 的第 8 到 15 个最低有效位。 code>status 宏参数。文档和注释令人困惑,因为状态指的是两个不同的事物。
退出的进程返回一个状态码。此退出状态在源中具有
int
类型(作为main
的返回值,或作为exit
的参数),但是,该值应该介于 0 和 255 之间。wait
和waitpid
系统调用还向调用者提供状态
。这种状态是不同的;原始退出状态的低位 8 位现在位于位 15 到 8。我假设文档说 WEXITSTATUS 返回“低位 8 位”,因为从退出进程的角度来看,这是退出状态的打包。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 thestatus
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 frommain
, or as argument toexit
), however, the value should be between 0 and 255.The
wait
andwaitpid
system calls also provide astatus
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.