简体   繁体   中英

Why is WEXITSTATUS even needed?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM