linux-sysprog · beginner · ~15 min
Bound-check a signal number before using it.
Before passing a signal number to kill, bound-check it. Deliverable signals occupy the range 1..64; signal 0 is the "does this process exist?" probe, not a real signal.
Implement int valid_signal(int sig) that returns 1 if sig is in the inclusive range 1..64, else 0.
sig: a candidate signal number.1 if 1 <= sig <= 64, else 0.
valid_signal(1) -> 1
valid_signal(0) -> 0 (the existence-probe value)
valid_signal(65) -> 0
valid_signal(64) -> 1
sig == 0: return 0.sig: a candidate signal number.
1 if 1 <= sig <= 64, else 0.
Signal 0 and out-of-range values are invalid.
int valid_signal(int sig) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.