linux-sysprog · intermediate · ~25 min
Bit manipulation for signal sets.
Compute the bitmask of signals to block. Use these fixed signal numbers (we don't pull in <signal.h> for portability):
enum { SIG_HUP=1, SIG_INT=2, SIG_QUIT=3, SIG_TERM=15, SIG_USR1=10, SIG_USR2=12 };
Implement unsigned long mask_block(int *sigs, int n) returning a uint mask with bit sig-1 set for each signal in the input array.
Signal handling is one of the trickiest corners of UNIX. Computing the right mask to block a set of signals is the foundation of safe signal-handling code.
Array of signal ints (1..63). n >= 0.
Bitmask with set bits at position sig-1.
O(n).
enum { SIG_HUP=1, SIG_INT=2, SIG_QUIT=3, SIG_TERM=15, SIG_USR1=10, SIG_USR2=12 };
unsigned long mask_block(int *sigs, int n) { /* TODO */ return 0; }
Setting bit sig instead of sig-1 (a real source of off-by-one in libc sigset implementations); using int instead of unsigned (sign extension in shifts).
Duplicate signals — bit set once. n == 0 returns 0.
O(n).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.