0

I read this code in an implementation of the system function of unix (problem 8.22):

        int status;
        if (wait(&status) > 0) {
             if (WIFEXITED(status)) {
                 return WEXITSTATUS(status);
             } else {
                 return status;
             }
        }

I don't understand what the if condition is for here: Isn't the status returned by WEXITSTATUS the same as the one in wait? If not, then what is the difference between the two?

teaLeef
  • 1,879
  • 2
  • 16
  • 26

1 Answers1

1

The status value is a sort of composite value which includes the process "exit status" plus other bits. From the Linux man page regarding WEXITSTATUS:

If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().

John Zwinck
  • 239,568
  • 38
  • 324
  • 436