41

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?

alk
  • 69,737
  • 10
  • 105
  • 255
shaveenk
  • 1,983
  • 7
  • 25
  • 37
  • 5
    If all the commands succeed, then the value in `counter` will be zero; if any of the commands exits with a failure (non-zero) status, the value in `counter` will be non-zero. If you run so many commands that you run into overflow issues, then you might coincidentally get 0 even though almost everything failed, but the chances of that are minuscule. Note that if commands die because of signals (WIFSIGNALED), then the WEXITSTATUS will be zero, but the command still failed. – Jonathan Leffler Dec 09 '13 at 07:25
  • Right. I understand that part. I really want to know what status value gets set to based on the signals that it receives. Does WIFSIGNALED, WIFSTOPPED, etc set status to an integer value? How do we know what this value is? – shaveenk Dec 09 '13 at 07:40
  • 1
    The macros *evaluate* `status`, which contains the exit code of the process. If process 12345 calls `exit(7)` and you `waitpid(12345, &status, 0)` then `WEXISTATUS(status)` will yield `7`. – Matthew Read Mar 17 '15 at 23:33

1 Answers1

40

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.

alk
  • 69,737
  • 10
  • 105
  • 255
  • 1
    I had referred to man waitpid. I am not able to run this piece of code. How am I supposed to get the value for the lower order 8 bits of the status argument? How can I get the integer values that status takes? – shaveenk Dec 09 '13 at 07:15
  • 3
    @shaveenk: You are "*...unable to run ...*" or unable to compile this code? Whhich errors do you get? – alk Dec 09 '13 at 07:18
  • My guess would be, they were expecting the return code to be 0 or 1, with 0 being a success and 1 being used to increment the counter of errors. It makes less sense if other values can be returned, but could still make sense if they represented a sub-count of something, such as errors. – Daniel Russell Jan 10 '23 at 13:00