Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does WEXITSTATUS(status) return?

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?

like image 373
shaveenk Avatar asked Dec 09 '13 07:12

shaveenk


1 Answers

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.

like image 54
alk Avatar answered Sep 28 '22 05:09

alk