linux-sysprog · beginner · ~15 min

Signal number to name

Translate a signal number back to a name.

Challenge

The reverse mapping — number to name — is what strsignal and kill -l provide. Build it for a few common signals.

Task

Implement const char *sig_name(int sig) that returns the name string for a given signal number.

Input

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

Output

"SIGINT", "SIGTERM", or "SIGKILL" for those signals; "UNKNOWN" otherwise.

Example

sig_name(SIGINT)    ->   "SIGINT"
sig_name(SIGKILL)   ->   "SIGKILL"
sig_name(99999)     ->   "UNKNOWN"

Edge cases

  • Unrecognised number: return "UNKNOWN".

Input format

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

Output format

The signal name string, or "UNKNOWN" if unrecognised.

Constraints

Recognise SIGINT, SIGTERM, SIGKILL.

Starter code

#include <signal.h>

const char *sig_name(int sig) {
    /* TODO */
    return "UNKNOWN";
}

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