What are signals?
- C course
- Advanced
- Lesson 13/83
- 5 min
Learn the signal concept and basic signals.
What are signals?
A signal is a software interrupt sent by the operating system to a process. It reports that an event occurred; the process can catch it and respond with a function.
Each signal has a name and a number. Common ones: SIGINT (interrupt via Ctrl+C), SIGTERM (termination request) and SIGKILL (forced kill). SIGKILL and SIGSTOP cannot be caught. Signals are defined in <signal.h>.
Signal sources
Signals can come from various sources: user (Ctrl+C → SIGINT), kernel (divide by zero → SIGFPE), another process (via kill) or the process itself (via raise).
A default action applies: terminate, core dump, ignore or stop. You can install your own function instead of the default; this is called a signal handler. You register it with signal or sigaction.
Practice Print signal names
Print three signal names, one per line, with printf: printf("signal: SIGINT\n");, printf("signal: SIGTERM\n"); and printf("signal: SIGKILL\n");.
Starter code
#include <stdio.h>
#include <signal.h>
// Print signal: SIGINT
// Print signal: SIGTERM
// Print signal: SIGKILL
int main(void) {
return 0;
}Sample solution
#include <stdio.h>
#include <signal.h>
int main(void) {
printf("signal: SIGINT\n");
printf("signal: SIGTERM\n");
printf("signal: SIGKILL\n");
return 0;
}Expected output
signal: SIGINT signal: SIGTERM signal: SIGKILL
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which signal is sent with Ctrl+C?
- SIGINT Correct answer
- SIGTERM
- SIGKILL
- SIGHUP
Explanation: SIGINT is the user interrupt via Ctrl+C.
2 Signal header file
#include <____.h>
#include <signal.h>
Explanation: signal.h defines signal constants and functions.
3 Match signals to meanings
| Term | Meaning |
|---|---|
SIGINT |
Ctrl+C interrupt |
SIGTERM |
Termination request |
SIGKILL |
Forced kill |
SIGHUP |
Hangup detected |