linux-sysprog · intermediate · ~15 min

Was the child signalled?

Distinguish a signalled death from a normal exit.

Challenge

WIFSIGNALED tells you whether a child died from a signal rather than exiting normally. Reproduce that test from the status word.

Task

Implement int was_signaled(int wstatus) that returns 1 if the low 7 bits of wstatus encode a real terminating signal, else 0.

Input

  • wstatus: a wait status word (integer).

Output

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.)

Example

was_signaled(0)      ->   0   (normal exit)
was_signaled(9)      ->   1   (killed by signal 9)
was_signaled(0x7f)   ->   0   (stopped, not terminated)

Edge cases

  • Low 7 bits == 0: normal exit, return 0.
  • Low 7 bits == 0x7f: stopped, return 0.

Input format

wstatus: a wait status word.

Output format

1 if the low 7 bits are a terminating signal (non-zero, not 0x7f), else 0.

Constraints

0 means normal exit; 0x7f means stopped.

Starter code

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.