C Signal Handling
In this page:
#include <signal.h>
void handler(int signum) {
// respond to the signal
}
signal(SIGINT, handler); // register handler for Ctrl+C
What is a Signal?
A signal is an asynchronous notification the operating system delivers to a running process to report an event like an illegal instruction, a segmentation fault, or the user pressing Ctrl+C, interrupting the program's normal flow so it can react to that event.
Example: What is a Signal?
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <signal.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("Signals notify a process of events like Ctrl+C");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
The signal() Function
The signal() function registers a custom handler function to run whenever a specific signal (identified by a constant like SIGINT or SIGSEGV) is delivered to your process, letting you replace the operating system's default behavior — which is usually to terminate the program outright — with your own response.
Example: The signal() Function
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <signal.h>
void handler(int sig) {
// Print formatted text to the screen
printf("Caught signal %d", sig);
}
// Program execution starts in main()
int main() {
signal(SIGINT, handler);
raise(SIGINT);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Catching SIGINT
SIGINT is the signal generated when the user presses Ctrl+C in the terminal running your program, and catching it with a custom handler lets your program close open files, free resources, and shut down gracefully instead of being killed abruptly mid-operation.
Example: Catching SIGINT
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <signal.h>
void handleSigint(int sig) {
// Print formatted text to the screen
printf("Ctrl+C caught, cleaning up");
}
// Program execution starts in main()
int main() {
signal(SIGINT, handleSigint);
raise(SIGINT);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Ignoring Signals
Passing the special value SIG_IGN as the handler argument to signal() tells the operating system to simply discard a specific signal whenever it arrives, rather than acting on it — a way to make a section of your program deliberately immune to an interrupt like SIGINT.
Example: Ignoring Signals
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <signal.h>
// Program execution starts in main()
int main() {
signal(SIGINT, SIG_IGN);
// Print formatted text to the screen
printf("SIGINT will now be ignored");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Raising Signals with raise()
The raise() function lets a program generate and send a signal to itself programmatically, which is useful for testing how your own signal handlers behave, or for deliberately triggering an internal fault-handling path from within the program's own logic.
Example: Raising Signals with raise()
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <signal.h>
void handler(int sig) {
// Print formatted text to the screen
printf("Handler triggered by raise()");
}
// Program execution starts in main()
int main() {
signal(SIGTERM, handler);
raise(SIGTERM);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Doing heavy work, such as
printformalloc, inside a signal handler, which is not safe. - Forgetting that
signalbehavior can vary across platforms. - Handling
SIGINTand not exiting, so the program becomes hard to stop.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: