Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 288
node ninja Avatar asked Apr 27 '11 06:04

node ninja


2 Answers

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.

like image 79
trojanfoe Avatar answered Oct 03 '22 23:10

trojanfoe


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.

like image 23
Ignacio Vazquez-Abrams Avatar answered Oct 03 '22 23:10

Ignacio Vazquez-Abrams