Signals
Overview
This week introduces signals as a fundamental mechanism for asynchronous inter-process communication in Unix/Linux systems.
Students will learn how signals are generated, how processes can handle them, and how to send signals between processes. We will explore the standard signal API and signal handling mechanisms.
By the end of this week, students will understand how processes communicate through signals, how to handle exceptional events, and how to register custom signal handlers.
Key Concepts
What are Signals?
- Signals as software interrupts for asynchronous event notification
- Uses of signals: termination, suspension, error notification, timer events, user-defined IPC
- Signal lifecycle: generation → delivery → handling
- Synchronous vs. asynchronous signals
- Signals as a form of lightweight IPC between processes
Standard Signals
- Process control signals:
SIGTERM— polite termination requestSIGKILL— forced termination (uncatchable)SIGSTOP/SIGCONT— suspend and resume execution
- Error and fault signals:
SIGSEGV— segmentation faultSIGFPE— floating-point exceptionSIGILL— illegal instructionSIGBUS— bus error
- User and timer signals:
SIGUSR1/SIGUSR2— user-defined signalsSIGALRM— timer expiration (fromalarm())
- Job control signals:
SIGINT— interrupt from keyboard (Ctrl+C)SIGQUIT— quit from keyboard (Ctrl+\)SIGTSTP— terminal stop (Ctrl+Z)
- I/O and pipe signals:
SIGPIPE— write to a pipe with no readerSIGIO— asynchronous I/O event
- Child process signals:
SIGCHLD— child process terminated or stopped
Signal Handling
- Default actions: terminate, ignore, stop, continue, core dump
- Custom signal handlers:
sigaction()— modern, reliable signal handling APIsignal()— traditional (unreliable) signal handler registration [do not use]
- Handler function signature:
void handler(int signum) - Persistent vs. one-shot handlers
- Signal disposition: default, ignore (
SIG_IGN), custom handler (SIG_DFL)
Sending and Raising Signals
kill()— send a signal to a process or process groupraise()— send a signal to the calling processkillpg()— send a signal to a process grouppthread_kill()— send a signal to a specific thread- Command-line tools:
kill,killall,pkill
Practice / Lab
Signal Handling
- Write a program that handles
SIGINT(Ctrl+C) and prints a message instead of terminating. - Register a handler using
sigaction()and observe the behavior. - Use the
killcommand from the shell to send signals to your running program.
Inter-Process Signaling
- Write a parent process that forks a child and sends
SIGUSR1usingkill(). - Have the child process handle
SIGUSR1and respond withSIGUSR2to the parent.
Timers
- Use
alarm()to scheduleSIGALRMand implement a timeout mechanism.
Homework
References & Resources
Required
- Signals in C programming
- Signal Handling in Linux
- Signals in C Language
- Signal handling — sigaction
- Kerrisk, The Linux Programming Interface
- Chapter 20: Signals — Fundamental Concepts
- Chapter 21: Signals — Signal Handlers
- Advanced Programming in the UNIX Environment — W. Richard Stevens, Chapter 10 “Signals”
Recommended
- Linux manual page - signal(7)
- Linux manual page - sigaction(2)
- Linux manual page - kill(2)
- Linux manual page - raise(3)
- Linux manual page - alarm(2)
- POSIX Signals (Wikipedia)
- Understanding sigaction
Quiz (Self-check)
- What is the difference between
SIGTERMandSIGKILL? - Why is
sigaction()preferred oversignal()for signal handling? - What are the default actions a process can take when receiving a signal?
- How do you send a signal from one process to another?
- What is the purpose of
SIGUSR1andSIGUSR2? - What happens when you press Ctrl+C in a terminal running a program?
- How does the
alarm()function use signals?
Suggested Tools
kill— send signals to processes by PIDkillall— send signals to processes by namepkill— send signals based on pattern matchingtrap— shell built-in for handling signals in scriptsstrace— trace signal delivery and system callsgdb— debug signal handling behavior