Lesson 13 of 15

Signal Table

Signals

Signals are the simplest form of IPC in Linux — asynchronous notifications sent to a process. Common signals:

NumberNameDefault action
2SIGINTTerminate (Ctrl+C)
9SIGKILLTerminate (cannot catch)
11SIGSEGVTerminate + core dump
15SIGTERMGraceful terminate

Each process has a signal table — an array of function pointers indexed by signal number. When a signal is delivered, the kernel looks up the handler and calls it.

typedef void (*handler_t)(int);
#define NSIG 32

void my_sigaction(handler_t table[], int sig, handler_t h) {
    if (sig >= 0 && sig < NSIG) table[sig] = h;
}

void my_raise(handler_t table[], int sig) {
    if (sig >= 0 && sig < NSIG && table[sig])
        table[sig](sig);
}

SIG_DFL and SIG_IGN

Two special handler values:

  • SIG_DFL (0) — default action (usually terminate)
  • SIG_IGN — ignore the signal

In our simulation, a NULL handler means SIG_DFL (no-op).

Your Task

Implement my_sigaction that registers a handler, and my_raise that delivers a signal by calling its handler.

TCC compiler loading...
Loading...
Click "Run" to execute your code.