开发者

Why is WEXITSTATUS even needed?

开发者 https://www.devze.com 2023-02-28 19:26 出处:网络
The following code will wait for a child process to finish and then print its return code. int status;

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消