Signals and Daemon
Process
MODULE-5
Signals
What are Signals in UNIX?
A signal is a software interrupt sent to a process to notify it that a specific event has
occurred.
It is used for process control, communication, and error handling in UNIX systems.
In simple words:
A signal tells a process that something important has happened.
It is used for:
Process control
Error handling
Inter-process communication (IPC)
Signals
Key Characteristics of Signals Why Signals Are Needed (Purpose)
Signals are asynchronous Signals are used to:
Signals are process-specific Terminate processes (SIGKILL, SIGTERM)
They interrupt the normal flow of execution
Detect illegal memory access (SIGSEGV)
Handle keyboard interrupts (SIGINT)
A signal can:
Terminate a process Synchronize parent and child (SIGCHLD)
Stop a process
Handle timeouts (SIGALRM)
Resume a process
Be handled by a user-defined function
Support IPC (process communication)
Common UNIX Signals
Signal Name Signal No Purpose
SIGINT 2 Interrupt from keyboard (Ctrl + C)
SIGKILL 9 Kill process forcefully
SIGTERM 15 Request termination
SIGSTOP 19 Stop process
SIGCONT 18 Continue stopped process
SIGSEGV 11 Segmentation fault
SIGALRM 14 Timer signal
SIGCHLD 17 Child process terminated
How Signals Are Generated
How Signals Are Generated
Signals can be generated by:
Keyboard input
◦ Ctrl + C → SIGINT
◦ Ctrl + Z → SIGTSTP
Hardware errors
◦ Divide by zero
◦ Invalid memory access
System calls
◦ kill()
◦ raise()
Process events
◦ Child termination → SIGCHLD
SIGCLD / SIGCHLD –
SEMANTICS
1. What is SIGCLD / SIGCHLD?
SIGCLD is the old System V name
SIGCHLD is the modern POSIX/Linux name
It is a signal sent to the parent process when its child process changes state
A child process may:
Terminate normally
Terminate abnormally
Stop
Resume
In all these cases, SIGCHLD is sent to the parent
SIGCLD / SIGCHLD –
SEMANTICS
4. Default Action of SIGCHLD
2. Purpose of SIGCHLD
SIGCHLD is mainly used to:
Action Meaning
Inform parent that child has finished Ignore Parent does nothing
Avoid zombie processes Terminate Never happens by default
Perform parent–child synchronization Custom
Parent can handle it
handler
Collect child exit status using wait() or waitpid()
5. Relationship Between SIGCHLD and Zombie Processes
When a child terminates:
3. When is SIGCHLD Generated?
◦ It becomes a zombie
SIGCHLD is generated when: ◦ Parent must call wait() or waitpid()
Child process terminates If parent does not handle SIGCHLD:
◦ Zombie process remains in the process table
Child process is stopped (Ctrl + Z)
Child process resumes (continued) Handling SIGCHLD properly automatically cleans zombies
7. SIGCLD Semantics in System V UNIX
In old System V UNIX:
Signal name used → SIGCLD
Special behavior:
Multiple child terminations may generate only one SIGCLD
Parent must loop using wait() to collect all children
SIGCLD SIGCHLD
System V
POSIX / Linux name
UNIX name
Older
Modern systems
systems
Same
Same purpose
purpose
kill() and raise() Functions
1. kill() Function
Definition:
The kill() function is used to send a signal to any process whose process ID (PID) is known.
It can:
Kill a process
Stop a process
Resume a process
Send any custom signal
Kill()
raise() Function
Definition:
The raise() function is used to send a signal to the calling process itself.
It is mainly used for:
Testing signal handlers
Generating software interrupts inside the program
raise()
alarm() and pause()
Both alarm() and pause() are used for process control using signals, especially with SIGALRM.
1. alarm() Function
Definition:
The alarm() function is used to schedule a signal (SIGALRM) after a specified number of seconds.
It is mainly used for:
Timeouts
Automatic process wake-up
Time-based programming
Alarm()
Pause()
Definition:
The pause() function suspends the execution of a process until any signal is received.
It does NOT consume CPU while waiting.
Pause()
Alarm() & pause()
SIGNAL SETS IN UNIX
1. What is a Signal Set?
A signal set is a collection (group) of signals used by a process to:
Block signals
Unblock signals
Check pending signals
Modify signal masks
Technically, a signal set is stored using the data type:
SIGNAL SETS
2. Why Are Signal Sets Needed?
Signal sets are used to:
Temporarily block signals
Prevent race conditions
Protect critical sections of code
Handle multiple signals efficiently
Perform reliable signal handling using sigprocmask(), sigpending(), etc.
sigprocmask()
What is sigprocmask()?
sigprocmask() is used to block, unblock, or replace the set of signals (signal mask) for a process.
It controls which signals are allowed or temporarily blocked from being delivered to a process.
Purpose of sigprocmask()
It is used to:
Block signals temporarily
Unblock previously blocked signals
Prevent race conditions
Protect critical sections of code
Control signal delivery safely
sigprocmask()
Program: Blocking and
Unblocking SIGINT
sigpending()
1. What is sigpending()?
sigpending() is used to find out which signals are currently pending for a process.
Pending signals = signals that are generated but not yet delivered because they are blocked.
2. Purpose of sigpending()
It is used to:
Check which signals are waiting for delivery
Debug signal blocking programs
Verify whether blocked signals are pending
Work with sigprocmask() and sigismember()
sigpending()
Program Using sigpending()
sigaction() in UNIX
sigaction() is the most powerful and modern way to define how a process handles signals.
It is used instead of the old signal() function because it provides:
More control
Reliable behavior
Prevents signal loss
Allows setting extra options
1. Purpose of sigaction()
sigaction() is used to:
Set a custom signal handler
Specify whether a signal should be:
◦ Handled by a user function
◦ Ignored Parameter Meaning
◦ Use default action
signum Signal number (e.g., SIGINT, SIGTERM)
Control additional behavior using flags act New action for the signal
Examine the old (previous) action for a signal oldact Stores previous action (optional)
sigaction()
Example
void handler(int sig, siginfo_t *info, void *context) {
printf("Signal %d from process %d\n", sig, info->si_pid);
struct sigaction act;
act.sa_sigaction = handler;
act.sa_flags = SA_SIGINFO;
sigemptyset(&act.sa_mask);
sigaction(SIGINT, &act, NULL);
sigsetjmp()
Normally, a function returns to its caller.
But with non-local jump, a program can:
Jump back to a previously saved execution point
Skip multiple function calls
Recover from errors or signals
2. sigsetjmp()s
Definition:
sigsetjmp() saves the current execution context and
optionally the signal mask.
siglongjmp()
Definition:
siglongjmp() restores the saved execution context and signal mask, and jumps back to sigsetjmp().
Work flow
Program Using sigsetjmp() &
siglongjmp()
sigsuspend()
1. What is sigsuspend()?
sigsuspend() is used to temporarily replace the current signal mask and suspend the process until a signal is received.
In simple words:
sigsuspend() puts the process to sleep and waits safely for a specific signal.
2. Why sigsuspend() Is Needed
sigsuspend() is used to:
Wait for a signal safely
Avoid race conditions
Work with signal blocking (sigprocmask)
Perform proper signal synchronization
Replace unsafe use of pause()
sigsuspend()
Example Program Using
sigsuspend()
abort()
1. What is abort()?
abort() is a library function used to immediately terminate a process
abnormally.
It forces the process to stop and generates the SIGABRT signal.
In simple words:
abort() is used to crash the program intentionally.
2. Purpose of abort()
abort() is mainly used to:
Stop a program when a serious runtime error occurs
Detect logical/programming errors
Debug programs
Terminate when recovery is not possible
Abort()
5. What Happens When abort() is Called?
When abort() is executed:
The process gets SIGABRT
If SIGABRT is not caught or ignored:
◦ Process is terminated immediately
◦ A core dump may be generated
Normal cleanup does NOT happen:
◦ return statements are skipped
◦ exit() handlers may not run
◦ Open files may not close properly
This makes abort() a violent termination method
sleep() Function
Definition
sleep() is used to suspend the execution of a process for a specified number of seconds.
nanosleep() Function
Definition
nanosleep() suspends a process with nanosecond precision.
It is a higher resolution and more accurate version of sleep().
nanosleep()
clock_nanosleep() Function
clock_nanosleep() is an advanced high-precision sleep function that allows sleeping based on a specific system clock.
clock_nanosleep()
Key Differences
Feature sleep() nanosleep() clock_nanosleep()
Seconds + Seconds +
Time unit Seconds
Nanoseconds Nanoseconds
Accuracy Low High Very High
Precise resume Precise resume
Signal handling Interrupted
possible possible
Uses clock No No Yes
Real-time use No Yes Best
sigqueue()
1. What is sigqueue()?
sigqueue() is a function used to send a signal to another process along with user-defined data.
Unlike kill(), which sends only a signal,
sigqueue() sends both a signal + an integer or pointer value.
It is mainly used with real-time signals.
2. Purpose of sigqueue()
sigqueue() is used to:
Send signals with additional information
Perform advanced inter-process communication (IPC)
Work with real-time queued signals
Avoid signal loss (signals are queued, not overwritten)
sigqueue()
sigqueue()