The following code will wait for a child process to finish and then print its return code.
int status;
wait(&status);
cout << "return code = " << WEXITSTATUS(status) << endl;
Why can't the return code just be stored in the int variable? Why does it have to be converted with the function WEXITSTATUS? What does the value of the unconverted int variable represent?
The int
holds more than just the exit code - it also stores information about how the process terminated, for example if it was signalled (WIFSIGNALED
) or if exit()
was called (WIFEXITED
), etc.
The W
macros are used to extract the various pieces of information from the int
.
status
contains not only the return value of the process, but also why the wait(2,3p)
call returned (which may not always be normal exiting of the process). The various W*()
macros are used to break up the returned value into its constituent pieces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With