linux-sysprog · beginner · ~15 min
Translate a signal number back to a name.
The reverse mapping — number to name — is what strsignal and kill -l provide. Build it for a few common signals.
Implement const char *sig_name(int sig) that returns the name string for a given signal number.
sig: a signal number (compare against the <signal.h> constants)."SIGINT", "SIGTERM", or "SIGKILL" for those signals; "UNKNOWN" otherwise.
sig_name(SIGINT) -> "SIGINT"
sig_name(SIGKILL) -> "SIGKILL"
sig_name(99999) -> "UNKNOWN"
"UNKNOWN".sig: a signal number from <signal.h>.
The signal name string, or "UNKNOWN" if unrecognised.
Recognise SIGINT, SIGTERM, SIGKILL.
#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.