Site Tools


h-signal

<signal.h>

<signal.h> provides signal handling for asynchronous events: SIGINT (Ctrl+C), SIGTERM, SIGSEGV, etc. Register a handler with signal(SIGNUM, handler). Handlers run asynchronously and can only safely call async-signal-safe functions like write and _exit.

The standard pattern is to set a flag in the handler and check it in the main loop.

Example

This example catches SIGINT by setting a flag that the main loop polls.

// compile: gcc -o signalexample signalexample.c
// run: ./signalexample (press Ctrl+C)
// description: signal handler that sets a flag for clean shutdown
 
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
 
volatile sig_atomic_t stop = 0;
 
void handler(int sig) {
    stop = 1;
}
 
int main() {
    signal(SIGINT, handler);
    puts("running... press Ctrl+C to stop");
 
    while (!stop) {
        putchar('.');
        fflush(stdout);
        sleep(1);
    }
 
    puts("\nshutting down cleanly");
    return 0;
}

Common functions and types

Signal handling:

  • signal(sig, handler): register signal handler
    • Returns previous handler or SIG_ERR on error
    • Handler is void (*handler)(int sig) function
    • Called asynchronously when signal arrives

Handler special values:

  • SIG_DFL: restore default handler
  • SIG_IGN: ignore signal
  • SIG_ERR: error return value

Signal-safe types:

  • sig_atomic_t: type guaranteed safe for assignments in signal handlers
  • Volatile qualified: volatile sig_atomic_t flag;
  • Only safe operation in handler is assignment to volatile sig_atomic_t

Common signals (POSIX):

  • SIGINT: interrupt (Ctrl+C from terminal)
  • SIGTERM: termination signal (from kill command)
  • SIGABRT: abnormal termination (from abort())
  • SIGSEGV: segmentation fault (invalid memory access)
  • SIGFPE: floating-point exception (divide by zero)
  • SIGALRM: alarm clock signal (from alarm())
  • SIGCHLD: child process terminated/stopped
  • SIGPIPE: broken pipe (write to closed socket/pipe)
  • SIGKILL: kill signal (cannot be caught or ignored)
  • SIGSTOP: stop signal (cannot be caught, suspends process)

Modern alternative (POSIX):

  • sigaction(): preferred over signal() for more control
  • sigprocmask(): block/unblock signals
  • pause(): wait for signal
h-signal.md · Last modified: by 127.0.0.1