linux-sysprog · intermediate · ~15 min
Decode the wait status bitfield.
wait fills a status word; WEXITSTATUS extracts the child's exit code from it. Reproduce that decoding.
Implement int exit_code(int wstatus) that returns the child's exit code, which lives in bits 8-15 of wstatus.
wstatus: a wait status word (integer).The exit code: (wstatus >> 8) & 0xFF.
exit_code(0) -> 0
exit_code(5 << 8) -> 5
exit_code(256) -> 1
wstatus: a wait status word.
The exit code in bits 8-15: (wstatus >> 8) & 0xFF.
wstatus is an integer status word filled by wait() or waitpid().
int exit_code(int wstatus) {
/* TODO */
return 0;
}
Assuming the exit code is in the lowest 8 bits; in standard POSIX wait status, exit code is in bits 8-15.
Exit code 0, exit code 255, exit code 1.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.