In this lesson, you will learn how signals are distributed across process groups and terminal sessions to manage related collections of processes simultaneously.
The Hierarchy of Signals
When you send a signal (like SIGINT or SIGTERM) to a process, you often want to influence its "family"—its children and other processes sharing the same context. In Linux, process trees are organized into Process Groups and Sessions, which provide a mechanism for bulk signal delivery.
Signaling Process Groups
A Process Group is a collection of processes that share a single process group ID (PGID). When a user presses Ctrl+C in a terminal, the terminal driver doesn't just send a signal to the shell; it sends a SIGINT to the entire foreground process group.
This is why, if you run a pipeline like cat file | grep "search", both processes belong to the same process group. Sending a signal to that PGID ensures that both the reader and the filter terminate together, preventing orphaned "dangling" processes.
Terminal Sessions and SIGHUP
While process groups handle local groups, Sessions manage the lifecycle of a terminal interaction. If you close a terminal window, the session leader (usually your shell) receives a SIGHUP (hang-up) signal.
When a session leader terminates, the kernel propagates the SIGHUP to all processes in the foreground process group. This is the mechanism that cleans up your terminal window; it ensures that your text editors, shells, and background jobs are notified that their controlling terminal is disappearing.
Visualization
Think of the process tree as a structure where signals travel "downwards" through group relationships:
- Terminal Input: You trigger a command (e.g.,
Ctrl+C). - Signal Broadcast: The terminal emulator identifies the Foreground Process Group.
- Propagation: The kernel delivers the signal to every process sharing that specific group ID.
If you want to send a signal to an entire group manually from the command line, you use a negative PID with the kill command:
# Sends SIGTERM to every process in process group 1234
kill -TERM -1234
By targeting the negative ID, you tell the kernel to broadcast the signal to the entire process group rather than a single PID.
Key Takeaway: Signals propagate through process groups and sessions to ensure that related tasks are managed collectively, preventing orphaned processes when a terminal or task group is closed.