linux-sysprog · intermediate · ~25 min

Compute a sigprocmask blocking SIGINT/SIGTERM

Bit manipulation for signal sets.

Challenge

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.

Why this matters

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.

Input format

Array of signal ints (1..63). n >= 0.

Output format

Bitmask with set bits at position sig-1.

Constraints

O(n).

Starter code

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; }

Common mistakes

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

Edge cases to handle

Duplicate signals — bit set once. n == 0 returns 0.

Complexity

O(n).

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.