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.
int exit_code(int wstatus) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.