What does WEXITSTATUS(status) return?

shaveenk picture shaveenk · Dec 9, 2013 · Viewed 95.6k times · Source

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?

Answer

alk picture alk · Dec 9, 2013

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.