linux-sysprog · intermediate · ~15 min
Recall which calls are legal inside a signal handler.
Only async-signal-safe functions may be called from inside a signal handler; functions that use locks or global state (like printf, malloc) can deadlock or corrupt. Classify a function name against a safe allowlist.
Implement int is_async_signal_safe(const char *fn) that returns 1 if fn names an async-signal-safe function, else 0.
fn: a function name string.1 for "write", "_exit", "signal", "kill", "read"; 0 for anything else (e.g. "printf", "malloc", "free").
is_async_signal_safe("write") -> 1
is_async_signal_safe("_exit") -> 1
is_async_signal_safe("printf") -> 0
is_async_signal_safe("malloc") -> 0
fn: a function name string.
1 if fn is in the safe allowlist, else 0.
Safe set: write, _exit, signal, kill, read.
#include <string.h>
int is_async_signal_safe(const char *fn) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.