Unix - UNIX Process Signals and Signal Handling
Introduction
In a UNIX operating system, processes are the fundamental units of execution. Every running program is represented as a process. UNIX provides a mechanism called signals to enable communication between processes and the operating system. Signals are software-generated notifications that inform a process about specific events, errors, or requests that require attention.
Signal handling is a critical aspect of UNIX system programming because it allows processes to respond appropriately to asynchronous events such as user interruptions, hardware exceptions, process termination requests, and system-generated notifications.
What is a Signal?
A signal is a software interrupt delivered to a process. Unlike regular function calls, signals occur asynchronously, meaning they can arrive at any time during a program's execution.
When a signal is sent to a process, the operating system temporarily interrupts the normal execution flow and performs a predefined action or invokes a user-defined signal handler.
Signals can be generated by:
-
The operating system
-
Another process
-
The user through keyboard actions
-
Hardware exceptions
-
System events
Why Signals are Needed
Signals serve several important purposes:
Process Control
Signals allow users and administrators to control running processes.
Examples:
-
Stopping a process
-
Resuming a process
-
Terminating a process
Event Notification
The operating system uses signals to notify processes about important events.
Examples:
-
Child process completion
-
Timer expiration
-
Memory access violations
Error Handling
Signals help detect exceptional conditions.
Examples:
-
Division by zero
-
Invalid memory access
-
Illegal instructions
Inter-Process Communication
Processes can send signals to communicate simple notifications to one another.
Signal Lifecycle
The lifecycle of a signal typically involves the following steps:
Signal Generation
A signal is created due to an event.
Example:
kill -SIGTERM 1234
This command generates a termination signal for process 1234.
Signal Delivery
The operating system delivers the signal to the target process.
Signal Reception
The process receives the signal and determines how to respond.
Signal Handling
The process:
-
Executes the default action
-
Ignores the signal
-
Executes a custom signal handler
Common UNIX Signals
SIGINT (Signal Interrupt)
Signal Number: 2
Generated when:
Ctrl + C
Purpose:
Interrupts a running process.
Example:
$ sleep 100
Ctrl+C
The sleep command terminates immediately.
Default Action:
Terminate the process.
SIGTERM (Signal Terminate)
Signal Number: 15
Purpose:
Requests graceful termination of a process.
Example:
kill -15 PID
or
kill PID
Applications can catch this signal and perform cleanup before exiting.
Default Action:
Terminate the process.
SIGKILL (Signal Kill)
Signal Number: 9
Purpose:
Immediately terminates a process.
Example:
kill -9 PID
Characteristics:
-
Cannot be ignored
-
Cannot be caught
-
Cannot be handled
Used when a process becomes unresponsive.
Default Action:
Immediate termination.
SIGHUP (Signal Hangup)
Signal Number: 1
Originally generated when a terminal connection was disconnected.
Modern Uses:
-
Reload configuration files
-
Restart services without stopping them
Example:
kill -HUP PID
Default Action:
Terminate process.
SIGQUIT
Signal Number: 3
Generated by:
Ctrl + \
Purpose:
Terminate a process and generate a core dump.
Default Action:
Terminate and create a core file.
SIGSTOP
Signal Number: 19
Purpose:
Pause a process.
Example:
kill -STOP PID
Characteristics:
-
Cannot be ignored
-
Cannot be handled
Default Action:
Stop process execution.
SIGCONT
Signal Number: 18
Purpose:
Resume a stopped process.
Example:
kill -CONT PID
Default Action:
Continue execution.
SIGCHLD
Purpose:
Sent to a parent process when a child process terminates.
Importance:
Allows the parent process to collect the child's exit status and avoid zombie processes.
SIGALRM
Purpose:
Generated when a timer expires.
Example Uses:
-
Timeout implementation
-
Scheduled actions
SIGSEGV (Segmentation Fault)
Signal Number: 11
Generated when:
A process attempts to access invalid memory.
Example:
int *ptr = NULL;
*ptr = 10;
Result:
Segmentation fault
Default Action:
Terminate process and generate a core dump.
Default Signal Actions
Every signal has a default action defined by the operating system.
Common actions include:
Terminate
Process exits immediately.
Examples:
-
SIGTERM
-
SIGINT
Ignore
Signal is discarded.
Examples:
Some signals can be configured to be ignored.
Stop
Process execution is suspended.
Examples:
-
SIGSTOP
Continue
Resumes a stopped process.
Examples:
-
SIGCONT
Core Dump
Process terminates and saves memory contents for debugging.
Examples:
-
SIGSEGV
-
SIGQUIT
Viewing Available Signals
The following command displays all supported signals:
kill -l
Example output:
1) SIGHUP
2) SIGINT
3) SIGQUIT
9) SIGKILL
15) SIGTERM
18) SIGCONT
19) SIGSTOP
Sending Signals
Using kill Command
Syntax:
kill -SIGNAL PID
Example:
kill -TERM 2345
Using Signal Number
kill -15 2345
Sending SIGKILL
kill -9 2345
Sending Signals to Multiple Processes
pkill firefox
This sends termination signals to all Firefox processes.
Signal Handling
Processes can define custom functions to handle signals.
When a signal arrives, the custom handler executes instead of the default action.
Basic Signal Handler Example
#include <stdio.h>
#include <signal.h>
void handler(int sig)
{
printf("Signal received: %d\n", sig);
}
int main()
{
signal(SIGINT, handler);
while(1);
return 0;
}
Behavior:
When the user presses Ctrl+C, the program does not terminate immediately. Instead, the custom handler executes.
Advantages of Signal Handling
Graceful Shutdown
Applications can save data before terminating.
Resource Cleanup
Programs can release:
-
Memory
-
Files
-
Network connections
Error Recovery
Some errors can be handled without crashing the application.
Event-Driven Programming
Signals enable asynchronous event processing.
Signal Masking and Blocking
Sometimes a process must temporarily prevent certain signals from interrupting critical operations.
This is called signal blocking.
Example situations:
-
Updating shared data
-
Performing critical file operations
-
Synchronizing processes
Blocked signals remain pending until they are unblocked.
Real-Time Signals
UNIX systems provide real-time signals that offer:
-
Guaranteed delivery
-
Signal queuing
-
User-defined priorities
Advantages:
-
Multiple signal instances are preserved.
-
Signals are processed in order.
Real-time signals are commonly used in advanced system programming and embedded applications.
Common Problems in Signal Handling
Race Conditions
Signals may arrive at unexpected times and interfere with program execution.
Lost Signals
Traditional signals may not queue multiple occurrences.
Reentrant Function Issues
Signal handlers should avoid calling functions that are not safe for asynchronous execution.
Zombie Processes
Improper handling of SIGCHLD can create zombie processes that consume system resources.
Best Practices
-
Use SIGTERM before SIGKILL whenever possible.
-
Keep signal handlers simple and fast.
-
Release resources before process termination.
-
Handle SIGCHLD correctly in parent processes.
-
Avoid performing complex operations inside signal handlers.
-
Test signal behavior under different conditions.
-
Use real-time signals when reliable signal queuing is required.
Conclusion
UNIX signals provide a powerful mechanism for communication between processes and the operating system. They allow programs to respond to interruptions, errors, process state changes, and administrative commands. Understanding common signals such as SIGINT, SIGTERM, SIGKILL, SIGHUP, SIGSTOP, and SIGCHLD is essential for effective UNIX system administration and application development. Proper signal handling improves application reliability, resource management, and system stability while enabling efficient control over running processes.