{{Short description|Collection of processes within a computer's operating system}} {{for|process groups in data replication|Virtual synchrony}}
In a [[POSIX]]-conformant [[operating system]], a '''process group''' denotes a collection of one or more [[process (computing)|processes]].<ref name="definition"/> Among other things, a process group is used to control the distribution of a [[signal (computing)|signal]]; when a signal is directed to a process group, the signal is delivered to each process that is a member of the group.<ref name="kill"/>
Similarly, a '''session''' denotes a collection of one or more process groups.<ref name="session"/> 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 (by calling one of the [[Exec (operating system)|<syntaxhighlight lang="c" inline>exec</syntaxhighlight> functions]]), the new image is subjected to the same process group (and thus session) membership as the old image.
== Applications == The distribution of signals to process groups forms the basis of [[job control (Unix)|job control]] employed by [[shell (computing)|shell programs]]. The [[Computer terminal|TTY device]] driver incorporates a notion of a '''foreground process group''', to which it sends signals generated by [[keyboard controller (computing)|keyboard interrupts]], notably [[SIGINT (POSIX)|SIGINT]] ("interrupt", {{keypress|Control|C}}), [[SIGTSTP]] ("terminal stop", {{keypress|Control|Z}}), and [[SIGQUIT]] ("quit", {{keypress|Control|\}}). 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 [[Pipeline (Unix)|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 (and thus what command pipelines) may perform I/O to and from the terminal at any given time.
When the shell [[fork (operating system)|<syntaxhighlight lang="c" inline>fork</syntaxhighlight>s]] a new child process for a command pipeline, both the parent shell process and the [[child process]] immediately make the child process the leader of the process group for the command pipeline. This ensures that the child is the leader of the process group before either the parent or child relies on this being the case.
Where a [[text terminal|textual user interface]] is being used on a Unix-like system, sessions are used to implement '''[[login session]]s'''. 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 session leader is absent, the processes in the terminal's foreground process group are expected to handle hangups.)
Where a [[graphical user interface]] is being used, the session concept is largely lost, and the [[kernel (computer science)|kernel]]'s notion of sessions largely ignored. Graphical user interfaces, such as where the [[X display manager (program type)|X display manager]] is employed, use a different mechanism for implementing login sessions.
== Details == The [[system call]] <syntaxhighlight lang="c" inline>setsid</syntaxhighlight> is used to create a new session containing a single (new) process group, with the current process as both the session leader and the '''process group leader''' of that single process group.<ref name="setsid"/> Process groups are identified by a positive integer, the '''process group ID''', which is the [[process identifier]] of the process that is (or was) 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 <syntaxhighlight lang="c" inline>setpgid</syntaxhighlight> 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.<ref name="setpgid"/> POSIX prohibits the re-use of a process ID where a process group with that identifier still exists (i.e. where the leader of a process group has exited, but other processes in the group still exist). It thereby guarantees that processes may not accidentally become process group leaders.
The [[kill (Unix)|system call <syntaxhighlight lang="c" inline>kill</syntaxhighlight>]] is capable of directing signals either to individual processes or to process groups.<ref name="kill"/>
== See also == * [[cgroups]] * Windows [[Object Manager (Windows)|Object Manager]], specifically [[Job objects]] and associated resource limits<ref>{{Cite web |last=Karl-Bridge-Microsoft |title=Job Objects - Win32 apps |url=https://docs.microsoft.com/en-us/windows/win32/procthread/job-objects |access-date=2022-08-28 |website=docs.microsoft.com |date=7 January 2021 |language=en-us}}</ref>
== References == * [[Single UNIX Specification]], Issue 6 <references> <ref name="definition"> {{cite book | title = The Open Group Base Specifications Issue 7 | author = IEEE and The Open Group | date = 2018 | chapter = 3. Definitions | at = § 296 | chapter-url = https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_296 | access-date = 2020-08-30 | quote = A collection of processes that permits the signaling of related processes. }} </ref> <ref name="session"> {{cite book | title = The Open Group Base Specifications Issue 7 | author = IEEE and The Open Group | date = 2018 | chapter = 3. Definitions | at = § 343 | chapter-url = https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_343 | access-date = 2020-08-30 | quote = A collection of process groups [...]. Each process group is a member of a session. }} </ref> <ref name="setsid">{{man|sh|setsid|SUS}}</ref> <ref name="setpgid">{{man|sh|setpgid|SUS}}</ref> <ref name="kill">{{man|sh|kill|SUS}}</ref> </references>
== Further reading == * {{cite book | title = The Design and Implementation of the FreeBSD Operating System | first1 = Marshall Kirk | last1 = McKusick | first2 = George V. | last2 = Neville-Neil | url = http://www.informit.com/title/0201702452 | date = 2004-08-02 | chapter = FreeBSD Process Management: Process Groups and Sessions | chapter-url = https://www.informit.com/articles/article.aspx?p=2249436 | publisher = Addison Wesley | isbn = 0-201-70245-2 }} * [http://www.cs.ucsb.edu/~almeroth/classes/W99.276/assignment1/signals.html UNIX Signals and Process Groups]
[[Category: Process (computing)]] [[Category: POSIX]]