linux-sysprog · intermediate · ~15 min

Does the default action terminate?

Recall which signals kill by default.

Challenge

Each signal has a default action; for many it is to terminate the process. Classify a signal by its default action.

Task

Implement int default_terminates(int sig) that returns 1 if sig's default action terminates the process, else 0.

Input

  • sig: a signal number (compare against the <signal.h> constants).

Output

1 for SIGINT, SIGTERM, SIGKILL, SIGQUIT, SIGSEGV; else 0.

Example

default_terminates(SIGINT)    ->   1
default_terminates(SIGKILL)   ->   1
default_terminates(SIGCHLD)   ->   0   (ignored by default)

Edge cases

  • Signals ignored by default (e.g. SIGCHLD): return 0.

Input format

sig: a signal number from <signal.h>.

Output format

1 for SIGINT/SIGTERM/SIGKILL/SIGQUIT/SIGSEGV, else 0.

Constraints

Match by the signal.h constants.

Starter code

#include <signal.h>

int default_terminates(int sig) {
    /* TODO */
    return 0;
}

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