Skip to content
Tutorials Center
My learning

Topics

C Reference ยท ctype.h, math.h and others

C signal.h: signal() and raise()

Reference for signal.h: install handlers with signal(), send signals with raise(), use volatile sig_atomic_t flags, and learn what a handler may safely do.

A signal is an asynchronous notification to your program: the user pressed Ctrl+C (SIGINT), the system asks it to terminate (SIGTERM), or the program did something illegal (SIGSEGV, SIGFPE). With signal() you choose what happens: run your own handler function, ignore the signal, or restore the default action. With raise() the program sends a signal to itself.

ISO C defines only a small, portable core. For serious signal handling on POSIX systems use sigaction, which has well-defined semantics.

C
#include <signal.h>

void on_interrupt(int sig) { /* set a flag */ }
signal(SIGINT, on_interrupt);

Try it

C A handler that sets a flag

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Ignoring signals and restoring defaults

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
typedef void (*sighandler_t)(int);       /* not standard; shown for readability */

void (*signal(int sig, void (*func)(int)))(int);
int raise(int sig);
typedef /* integer type */ sig_atomic_t;
Name Description
sig A signal number: one of the macros below.
func Your handler, or SIG_DFL (default action) or SIG_IGN (ignore).
Signal When it happens Default action
SIGINT Interactive attention, Ctrl+C. Terminate.
SIGTERM Termination request. Terminate.
SIGABRT Abnormal termination, sent by abort(). Terminate (often with a core dump).
SIGFPE Erroneous arithmetic, like integer division by zero. Terminate.
SIGILL Illegal instruction. Terminate.
SIGSEGV Invalid memory access. Terminate.

Returns: signal() returns the previous handler, or SIG_ERR on failure (and sets errno). raise() returns 0 on success and nonzero on failure.

Errors / undefined behavior: the behavior is undefined if a handler for SIGFPE, SIGILL or SIGSEGV returns normally (restore the default and re-raise, or leave with exit/_Exit/longjmp carefully), and if a handler touches non-atomic or non-volatile objects, calls most library functions, or allocates memory.

Usage notes

  • Keep handlers tiny. The portable rule: assign to a volatile sig_atomic_t variable and return; check the flag in your main loop. Calling printf, malloc or free in a handler can deadlock or corrupt state. POSIX lists a small set of async-signal-safe functions (write, _exit, ...).
  • volatile sig_atomic_t is the only type the standard guarantees you can safely write from a handler and read in the program. Other objects need _Atomic types that are lock-free.
  • signal() semantics vary, and this really bites. Old UNIX systems and Windows reset the handler to SIG_DFL after each delivery. glibc keeps the handler installed (BSD semantics) only in its default GNU mode; when you compile in strict ISO mode such as -std=c23, as the runner does, signal() is the one-shot System V version. A program that raised SIGINT twice without re-arming was killed by the second one when we tried it. Portable ISO C code re-installs the handler inside itself, as the examples do; POSIX code should use sigaction().
  • raise(sig) calls the handler before returning, in the same thread, which makes signal code easy to test, as above.
  • SIGKILL and SIGSTOP cannot be caught or ignored (POSIX); neither can be used with signal().
  • The signals on this page are the ISO C ones. POSIX adds SIGUSR1, SIGUSR2, SIGALRM, SIGCHLD, SIGPIPE, SIGHUP and many more.
  • Do not use signals for general inter-thread communication. In multithreaded programs the delivery thread is unspecified; use a proper synchronization primitive or an atomic flag.
  • A blocking call may be interrupted: a system call such as read can fail with EINTR when a handler runs. Loop and retry, or use SA_RESTART with sigaction.
  • Graceful shutdown: the standard recipe is a handler for SIGINT and SIGTERM that sets a flag, plus a main loop that notices the flag, flushes files and exits normally.
  • Program a handler for SIGSEGV only to print a diagnostic and quit. It is not a way to recover from a bad pointer.
  • Standard version: C89. C11 adds atomic_signal_fence; C23 makes no major changes.