linux-sysprog · intermediate · ~15 min
Distinguish a signalled death from a normal exit.
WIFSIGNALED tells you whether a child died from a signal rather than exiting normally. Reproduce that test from the status word.
Implement int was_signaled(int wstatus) that returns 1 if the low 7 bits of wstatus encode a real terminating signal, else 0.
wstatus: a wait status word (integer).1 if term_sig = wstatus & 0x7f is non-zero and not 0x7f; else 0. (0 means a normal exit; 0x7f means the child stopped, not died.)
was_signaled(0) -> 0 (normal exit)
was_signaled(9) -> 1 (killed by signal 9)
was_signaled(0x7f) -> 0 (stopped, not terminated)
wstatus: a wait status word.
1 if the low 7 bits are a terminating signal (non-zero, not 0x7f), else 0.
0 means normal exit; 0x7f means stopped.
int was_signaled(int wstatus) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.