What's the return value of system/pclose/waitpid?
The return value ofsystem()
,pclose()
, or
waitpid()
doesn't seem to be the exit value of my process...
or the exit value is shifted left 8 bits... what's the deal?
The man page is right, and so are you! If you read the man page for
waitpid()
you'll find that the return code for the process is
encoded. The value returned by the process is normally in the top 16
bits, and the rest is used for other things. You can't rely on this
though, not if you want to be portable, so the suggestion is that you
use the macros provided. These are usually documented under
wait()
or wstat
.
Macros defined for the purpose (in `<sys/wait.h>') include (stat is
the value returned by waitpid()
):
WIFEXITED(stat)
-
Non zero if child exited normally.
WEXITSTATUS(stat)
-
exit code returned by child
WIFSIGNALED(stat)
-
Non-zero if child was terminated by a signal
WTERMSIG(stat)
-
signal number that terminated child
WIFSTOPPED(stat)
-
non-zero if child is stopped
WSTOPSIG(stat)
-
number of signal that stopped child
WIFCONTINUED(stat)
-
non-zero if status was for continued child
WCOREDUMP(stat)
-
IfWIFSIGNALED(stat)
is non-zero, this is non-zero if the process
left behind a core dump.
Home | FAQ |