C语言 WEXITSTATUS(status) 返回什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20465039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does WEXITSTATUS(status) return?
提问by shaveenk
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.
我试图了解如何WEXITSTATUS(status)工作。我遇到了一段代码,其中将 的返回值WEXITSTATUS(status)添加到变量中。
Here is the snippet:
这是片段:
waitpid(-1, &status, 0);
counter += WEXITSTATUS(status);
How can the return value of WEXITSTATUSbe calculated?
的返回值如何WEXITSTATUS计算?
采纳答案by alk
WEXITSTATUS(stat_val)is a macro (so in fact it does not "return" something, but "evaluates" to something).
WEXITSTATUS(stat_val)是一个宏(所以实际上它不会“返回”某物,而是“评估”某物)。
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.
对于它是如何工作的,您可能希望在您使用的 C 编译器附带的头文件(应该是#included via <sys/wait.h>)中查找它。
The implementation of this macro might differ from one C-implementation to the other.
此宏的实现可能因一个 C 实现而异。
Please note, that this macro only gives a sane value, if the macro WIFEXITED(stat_val)gave you a value unequal to 0.
请注意,如果宏WIFEXITED(stat_val)给您的值不等于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().
出口状态( stat_val)
如果 WIFEXITED( stat_val) 的值不为零,则此宏计算子进程传递给_exit()或exit()的状态参数的低位 8 位,或子进程从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.
将特定程序的返回代码相加的动机只有代码的作者和希望现有的文档知道。

