linux-sysprog · intermediate · ~15 min
Enforce the 'only set a flag' handler discipline.
The safe signal-handler discipline is: set a flag and return, then do the real work in the main loop. Check a handler's op trace against that rule.
Implement int handler_is_safe(const char *ops) that returns 1 only if every op in the trace is safe.
ops: a string of handler ops, where 's' = set a volatile sig_atomic_t flag (safe) and 'p' = call printf (unsafe).1 if every op is 's'; 0 if any op is not 's'.
handler_is_safe("sss") -> 1
handler_is_safe("sps") -> 0 (contains an unsafe printf)
handler_is_safe("") -> 1 (no unsafe ops)
ops: string of handler ops ('s'=set flag, 'p'=printf).
1 if every op is 's', else 0.
Any op other than 's' is unsafe.
int handler_is_safe(const char *ops) {
/* TODO */
return 1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.