<signal.h> is the C standard header for signals—asynchronous notifications such as Ctrl+C (SIGINT). Use signal() to install a handler and raise() to deliver a signal to the current program.
Remember
#include <signal.h>
signal(sig, handler) → install what happens for sig
raise(sig) → send sig to this process now
SIGINT → usually Ctrl+C
SIG_DFL → default action (often terminate)
SIG_IGN → ignore the signal
handler: set a flag — do not call printf
In the main path, call raise(SIGINT) to test—or wait for a real Ctrl+C—then check the flag and print or exit.
Piece
Required?
Role
signal(sig, fn)
Yes (to catch)
Registers the handler for that signal
raise(sig)
No
Delivers the signal for testing
volatile sig_atomic_t
Recommended
Safe flag set inside the handler
Pseudocode
Pseudocode
flag = 0
handler(sig):
flag = 1 // only simple, async-safe work
main:
signal(SIGINT, handler)
raise(SIGINT) // or wait for real Ctrl+C
if flag == 1:
print / cleanup / exit
else:
continue
Reference
Cheat Sheet
Keep these facts handy while writing signal code.
Topic
Detail
Header
#include <signal.h>
Install
signal(sig, handler) → previous handler or SIG_ERR
Deliver
raise(sig)
Common signal
SIGINT (Ctrl+C)
Special handlers
SIG_DFL (default), SIG_IGN (ignore)
Safe pattern
Handler sets flag; main loop reads flag
Unsafe in handler
printf, malloc, most library I/O
Try it
Live Preview
Simulate what happens when SIGINT arrives: catch with a flag, ignore with SIG_IGN, or leave default behavior.
Browser simulation of signal/raise (same idea as the C examples below).
Result
signal(SIGINT, handler)
raise(SIGINT)
→ handler sets flag = 1
→ main sees flag
Caught SIGINT — handler ran safely
Trace
Worked Walkthrough — Catch SIGINT Safely
Trace installing a handler, raising SIGINT, and reacting in main without calling printf inside the handler.
Calculation
got_sigint = 0
signal(SIGINT, handle_sigint)
raise(SIGINT)
→ OS runs handle_sigint
→ got_sigint = 1
→ returns to raise / main
main checks got_sigint:
true → print "Caught SIGINT…"
(printing happens here, not in the handler) ✓
Code
C Examples
Compile with gcc file.c -std=c11 -o out. Examples use raise() so output is reproducible without pressing Ctrl+C.
Example 1 — Catch SIGINT with a sig_atomic_t Flag
The safe beginner pattern: the handler only sets a flag; main prints the message.
C
#include <signal.h>
#include <stdio.h>
static volatile sig_atomic_t got_sigint = 0;
void handle_sigint(int sig) {
(void)sig;
got_sigint = 1;
}
int main(void) {
if (signal(SIGINT, handle_sigint) == SIG_ERR) {
printf("Could not install SIGINT handler\n");
return 1;
}
printf("Simulating Ctrl+C with raise(SIGINT)...\n");
raise(SIGINT);
if (got_sigint) {
printf("Caught SIGINT (signal %d) — handler ran safely\n", SIGINT);
}
return 0;
}
Output
Simulating Ctrl+C with raise(SIGINT)...
Caught SIGINT (signal 2) — handler ran safely
How It Works
Install the handler.signal(SIGINT, handle_sigint) registers what to do for Ctrl+C.
Deliver the signal.raise(SIGINT) sends the same signal as a keyboard interrupt.
Handler stays tiny. It only sets got_sigint = 1—no printf inside.
Main reacts. After raise returns, main checks the flag and prints safely.
Example 2 — Ignore SIGINT with SIG_IGN
Compare a custom handler with SIG_IGN, which discards the signal.
Register once.signal(SIGINT, on_signal) installs the handler.
Raise three times. Each raise(SIGINT) invokes on_signal.
Increment safely. The handler only does count++ on a sig_atomic_t.
Report in main. Printing happens after all raises—still outside the handler.
Edge Cases & Pitfalls
Check these before relying on signal handlers in real projects.
safety
No printf in handlers
Most library I/O and malloc are not async-signal-safe. Set a flag; do real work in the main path.
portability
signal() behavior varies
Some platforms reset the handler to default after one delivery. Prefer simple flag patterns; on Unix, production code often uses POSIX sigaction.
default
Default often terminates
With SIG_DFL, raising SIGINT may kill the process. Do not casually raise after restoring default in demos.
scope
Not for normal errors
Bad user input belongs with return codes and errno—not signals.
Analysis
Performance Notes
Approach
Cost
Best for
Flag in handler
Tiny write + poll in loop
Graceful Ctrl+C shutdown
raise in tests
One kernel/user delivery
Reproducible demos
Heavy work in handler
Unsafe / unpredictable
Avoid entirely
Signal delivery is rare compared with normal code. Keep handlers minimal so your program stays correct under interruption—clarity beats cleverness here.
Remember
Key Takeaways
Role: catch or ignore asynchronous signals like SIGINT.
One line: catch signals with a tiny flag handler—do the real work (print, cleanup, exit) back in normal code.
Frequently Asked Questions
signal.h declares the standard C signal interface: signal() to install a handler for a signal number, raise() to send a signal to the current process, and macros like SIGINT, SIGFPE, SIG_DFL, and SIG_IGN. Signals are asynchronous notifications (for example Ctrl+C).
signal(sig, handler) registers what happens when signal number sig arrives. raise(sig) delivers signal sig to the calling program right now—useful for testing handlers or reporting errors as signals.
Avoid it. printf is not guaranteed async-signal-safe and can deadlock if interrupted while holding a lock. The safe pattern: set a volatile sig_atomic_t flag in the handler, then check the flag in your main loop and print there.
Special handler values passed to signal(). SIG_DFL restores default behavior (often terminate the process). SIG_IGN ignores the signal. Use them instead of a custom function when you want default or no action.
sigaction, sigprocmask, and kill are POSIX extensions—not part of standard C signal.h. They offer more control on Linux/macOS. Standard C programs use signal() and raise(); production Unix code often prefers sigaction over signal().
SIGINT is the interrupt signal, usually sent when the user presses Ctrl+C in a terminal. Default action is to terminate the process. You can catch it with signal(SIGINT, handler) for graceful shutdown.