linux-sysprog · beginner · ~15 min

Signal name to number

Translate a signal name to its number.

Challenge

Tools like kill -s map a signal name to its number. Build that lookup for a few common signals.

Task

Implement int sig_number(const char *name) that returns the signal constant for a given name.

Input

  • name: a signal name string.

Output

SIGINT, SIGTERM, or SIGKILL for those names; -1 for anything else.

Example

sig_number("SIGINT")    ->   SIGINT
sig_number("SIGKILL")   ->   SIGKILL
sig_number("SIGNOPE")   ->   -1

Edge cases

  • Unrecognised name: return -1.

Input format

name: a signal name string.

Output format

The matching signal constant, or -1 if unrecognised.

Constraints

Recognise SIGINT, SIGTERM, SIGKILL.

Starter code

#include <signal.h>
#include <string.h>

int sig_number(const char *name) {
    /* TODO */
    return -1;
}

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