linux-sysprog · beginner · ~15 min

Valid signal number?

Bound-check a signal number before using it.

Challenge

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.

Task

Implement int valid_signal(int sig) that returns 1 if sig is in the inclusive range 1..64, else 0.

Input

  • sig: a candidate signal number.

Output

1 if 1 <= sig <= 64, else 0.

Example

valid_signal(1)    ->   1
valid_signal(0)    ->   0   (the existence-probe value)
valid_signal(65)   ->   0
valid_signal(64)   ->   1

Edge cases

  • sig == 0: return 0.
  • Out of range (negative or > 64): return 0.

Input format

sig: a candidate signal number.

Output format

1 if 1 <= sig <= 64, else 0.

Constraints

Signal 0 and out-of-range values are invalid.

Starter code

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.