Process group


In a POSIX-conformant operating system, a process group denotes a collection of one or more processes. Among other things, a process group is used to control the distribution of a signal; when a signal is directed to a process group, the signal is delivered to each process that is a member of the group.
Similarly, a session denotes a collection of one or more process groups. A process may not create a process group that belongs to another session; furthermore, a process is not permitted to join a process group that is a member of another session—that is, a process is not permitted to migrate from one session to another.
When a process replaces its image with a new image, the new image is subjected to the same process group membership as the old image.

Applications

The distribution of signals to process groups forms the basis of job control employed by shell programs. The tty device driver incorporates a notion of a foreground process group, to which it sends signals generated by keyboard interrupts, notably SIGINT, SIGTSTP, and SIGQUIT. It also sends the SIGTTIN and SIGTTOU signals to any processes that attempt to read from or write to the terminal and that are not in the foreground process group. The shell, in turn, partitions the command pipelines that it creates into process groups, and controls what process group is the foreground process group of its controlling terminal, thus determining what processes may perform I/O to and from the terminal at any given time.
When the shell forks a new child process for a command pipeline, both the parent shell process and the child process immediately attempt to make the child process into the leader of the process group for the command pipeline.
Where a textual user interface is being used on a Unix-like system, sessions are used to implement login sessions. A single process, the session leader, interacts with the controlling terminal in order to ensure that all programs are terminated when a user "hangs up" the terminal connection.
Where a graphical user interface is being used, the session concept is largely lost, and the kernel's notion of sessions largely ignored. Graphical user interfaces, such as where the X display manager is employed, use a different mechanism for implementing login sessions.

Details

The system call setsid is used to create a new session containing a single process group, with the current process as both the session leader and the process group leader of that single process group. Process groups are identified by a positive integer, the process group ID, which is the process identifier of the process that is the process group leader. Process groups need not necessarily have leaders, although they always begin with one. Sessions are identified by the process group ID of the session leader. POSIX prohibits the change of the process group ID of a session leader.
The system call setpgid is used to set the process group ID of a process, thereby either joining the process to an existing process group, or creating a new process group within the session of the process with the process becoming the process group leader of the newly created group. POSIX prohibits the re-use of a process ID where a process group with that identifier still exists. It thereby guarantees that processes may not accidentally become process group leaders.
The system call kill is capable of directing signals either to individual processes or to process groups.