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.
#include <signal.h> void on_interrupt(int sig) { /* set a flag */ } signal(SIGINT, on_interrupt);
Try it
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.
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
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_tvariable and return; check the flag in your main loop. Callingprintf,mallocorfreein a handler can deadlock or corrupt state. POSIX lists a small set of async-signal-safe functions (write,_exit, ...). volatile sig_atomic_tis the only type the standard guarantees you can safely write from a handler and read in the program. Other objects need_Atomictypes that are lock-free.signal()semantics vary, and this really bites. Old UNIX systems and Windows reset the handler toSIG_DFLafter 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 raisedSIGINTtwice 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 usesigaction().raise(sig)calls the handler before returning, in the same thread, which makes signal code easy to test, as above.SIGKILLandSIGSTOPcannot be caught or ignored (POSIX); neither can be used withsignal().- The signals on this page are the ISO C ones. POSIX adds
SIGUSR1,SIGUSR2,SIGALRM,SIGCHLD,SIGPIPE,SIGHUPand 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
readcan fail withEINTRwhen a handler runs. Loop and retry, or useSA_RESTARTwithsigaction. - Graceful shutdown: the standard recipe is a handler for
SIGINTandSIGTERMthat sets a flag, plus a main loop that notices the flag, flushes files and exits normally. - Program a handler for
SIGSEGVonly 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.