Ch20.5 β Signal Dispositions
Linux System Programming Β· EmbeddedPathashala
π‘ Topic 5 of 19
π― BeginnerβIntermediate
π» 2 Code Examples
β Interview Q&A
π Key Terms
Disposition Default Action SIG_DFL SIG_IGN Signal Handler Core Dump Term / Stop / Cont
What is a Signal Disposition?
When a signal is delivered, the process does something in response. That “something” is called the signal’s disposition. By default every signal has a built-in action defined by the kernel. But your program can change this.
There are exactly three dispositions you can set for a signal: use the default, ignore it, or run your own handler function.
π Five Possible Default Actions
| Default Action | What Happens | Example Signal |
|---|---|---|
| term | Process is terminated (killed) | SIGTERM, SIGINT, SIGALRM |
| core | Core dump file written, then process terminated | SIGSEGV, SIGFPE, SIGQUIT |
| ignore | Signal is silently discarded β process never knows | SIGCHLD, SIGURG |
| stop | Process execution is suspended (paused) | SIGSTOP, SIGTSTP |
| cont | Stopped process is resumed | SIGCONT |
ποΈ Three Dispositions You Can Set
| Disposition | How to Set | When to Use |
|---|---|---|
| Default (SIG_DFL) | signal(SIGX, SIG_DFL) | Undo a previous change; revert to built-in behaviour |
| Ignore (SIG_IGN) | signal(SIGX, SIG_IGN) | Suppress a signal whose default action would harm you |
| Handler function | signal(SIGX, myHandler) | Run your own code when the signal arrives |
Restriction: You cannot set the disposition to “terminate” or “core dump” directly. To get that effect you install a handler that calls exit() or abort().
π» Code Example 1 β Ignore SIGINT (SIG_IGN)
This process ignores Ctrl+C completely. The only way to stop it is with SIGKILL (kill -9).
/* Ignore SIGINT β process becomes immune to Ctrl+C
Compile: gcc -o ignore_sigint ignore_sigint.c
Run: ./ignore_sigint then try Ctrl+C, then kill -9 <PID> */
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
int main(void)
{
/* Set disposition of SIGINT to ignore */
signal(SIGINT, SIG_IGN);
printf("SIGINT (Ctrl+C) is now IGNORED.\n");
printf("My PID: %d\n", getpid());
printf("Only 'kill -9 %d' can stop me.\n", getpid());
while (1) {
printf("Running...\n");
sleep(2);
}
return 0;
}
π» Code Example 2 β Restore Default Disposition After Ignoring
This shows how to temporarily ignore a signal and then restore SIG_DFL so the default action applies again.
/* Temporarily ignore SIGINT, then restore default
Compile: gcc -o restore_default restore_default.c */
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
int main(void)
{
printf("Phase 1: Normal β Ctrl+C will TERMINATE the process.\n");
sleep(3);
/* Ignore SIGINT */
signal(SIGINT, SIG_IGN);
printf("Phase 2: SIGINT IGNORED β Ctrl+C does nothing.\n");
sleep(5);
/* Restore default */
signal(SIGINT, SIG_DFL);
printf("Phase 3: Default RESTORED β Ctrl+C will TERMINATE again.\n");
sleep(5);
printf("Done.\n");
return 0;
}
Try it: Press Ctrl+C during each phase to see the different behaviours.
β Interview Questions
Q1. What is a signal disposition?
The disposition of a signal is the action that occurs when the signal is delivered to a process. It can be the default kernel-defined action, ignore, or a custom signal handler function.
Q2. What are the five possible default actions for a signal?
term (terminate), core (terminate with core dump), ignore (silently discard), stop (suspend process), cont (resume stopped process).
Q3. What is the difference between SIG_DFL and SIG_IGN?
SIG_DFL resets the disposition to the default built-in action for that signal. SIG_IGN causes the signal to be silently discarded β the process is never informed that the signal was received.
Q4. Can you set a signal’s disposition to “terminate with core dump”?
Not directly β you cannot set a custom disposition that just dumps core. You can install a handler that calls abort() which generates SIGABRT, causing a core dump. Or if the default action is already “core”, you can restore SIG_DFL.
Q5. What happens to an ignored signal if the same process later changes disposition to a handler?
Any future occurrences of that signal will invoke the handler. Previously ignored signals are gone β they were discarded at the time they arrived while SIG_IGN was in effect.
Next Topic β
Signal Handlers β Writing your own function to catch signals
