Process GroupsEdit
Process groups are a fundamental organizational concept in many operating systems, designed to simplify the management of related processes that share a common purpose or terminal. In Unix-like environments, a process group is a collection of one or more processes that are identified by a single process group ID (PGID). The group acts as a unit for certain operations, most notably for delivering signals and for coordinating interactions with the controlling terminal. Each process in a system belongs to exactly one process group, and the group’s leader—the process whose PID equals the PGID—defines the identity of the group.
For interactive work, process groups enable shells to manage foreground and background jobs, so a user can run several programs at once and control which one is actively interacting with the terminal. The integration of process groups with sessions and the controlling terminal also helps isolate tasks and prevent unintended interference between unrelated programs. In this sense, process groups are a key piece of the broader Job control (computing) model that underpins many command-line environments, including the behavior of bash and other shells.
Definition and scope
- A process group is identified by a nonzero PGID. The PGID is, by convention, the PID of the group leader, which is the first process that formed the group. All other processes in the group share that same PGID.
- A session can contain one or more process groups, and each session has a single controlling terminal. The relationship between sessions, process groups, and the terminal is central to how foreground and background tasks are managed.
- The controlling terminal is associated with a single foreground process group within a session. This foreground group has the permission to read from and write to the terminal, while other process groups in the same session may be backgrounded.
These concepts are defined and standardized in POSIX and related specifications, which describe the behavior of process groups in function calls, signal delivery, and terminal control. In practice, software developers interact with process groups via a small set of system interfaces and kernels manage the underlying bookkeeping.
Creation, management, and signaling
- Creating or changing a process group is done through system calls such as setpgid or setpgrp. These calls can establish a new group for a process or move an existing process into a different group.
- A process can report its own group using getpgid or getpgrp. The PGID can be examined to determine which group a running process belongs to.
- Signals can be directed at a single process or an entire process group. To send a signal to a group, a negative PGID is used with a call like kill; for example, a command might use kill -PGID to affect all members of that group. This capability is central to implementing job control, as a shell can stop, resume, or terminate an entire job with a single command.
- Foreground and background control from a terminal relies on the shell’s ability to manipulate the foreground process group of the terminal. The operation is typically performed with terminal-control interfaces such as tcsetpgrp to designate which group should receive terminal input and signals while that group is in the foreground, and with standard signal handling for background processes.
These mechanisms enable a variety of common workflows: a user can start a long-running task in the background, bring another task to the foreground, or suspend and resume work without inadvertently affecting unrelated processes.
Roles in shells, sessions, and terminals
- Shells use process groups to implement job control. When a user starts a new command, the shell may create a new process group for that command, and then place the group in the foreground or background as requested. This separation keeps interactive programs from interfering with each other and ensures that terminal signals are delivered in a controlled fashion.
- The relationship among processes, process groups, sessions, and the controlling terminal helps prevent background tasks from reading input meant for foreground programs and enables precise signaling and cleanup when a user logs out or a session ends.
- In complex environments, utilities such as tmux or screen orchestrate multiple processes and shells, creating and managing several process groups so that each pane or window can operate independently yet remain coordinated through the terminal interface.
Portability and related concepts
- Process groups are a core feature of POSIX-compliant systems. While many Unix-like systems share common semantics, there are differences in how terminals, sessions, and signaling may be exposed in specific environments.
- Windows and other non-POSIX systems use different mechanisms for grouping or isolating processes (such as job objects). While these mechanisms differ in design, the underlying goal—controlled collection and management of related processes—parallels the intent of process groups in POSIX systems.
- Understanding process groups helps in diagnosing issues related to terminal I/O, background processing, and the behavior of long-running daemons that must operate without direct user interaction.