What does WEXITSTATUS(status) return?

旧城冷巷雨未停 提交于 2019-11-30 00:21:10

问题


I am trying to understand how WEXITSTATUS(status) works. I have come across a piece of code where the return value of WEXITSTATUS(status) is being added to a variable.

Here is the snippet:

waitpid(-1, &status, 0);
counter += WEXITSTATUS(status);

How can the return value of WEXITSTATUS be calculated?


回答1:


WEXITSTATUS(stat_val) is a macro (so in fact it does not "return" something, but "evaluates" to something).

For how it works you might like to look it up in the headers (which should be #included via <sys/wait.h>) that come with the C-compiler you use.

The implementation of this macro might differ from one C-implementation to the other.

Please note, that this macro only gives a sane value, if the macro WIFEXITED(stat_val) gave you a value unequal to 0.

Verbatim from waitpid()'s POSIX specification:

WEXITSTATUS(stat_val)

If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().


The motivation behind adding up the return code(s?) of a particular program is only known to the code's author and the hopefully existing documentation.



来源:https://stackoverflow.com/questions/20465039/what-does-wexitstatusstatus-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!