Use and meaning of session and process group in Unix?

UnixProcess

Unix Problem Overview


Unix processes have a session id and are part of a process group - which can be changed/queried with functions such as setsid()/getpgrp().

However the concept of a process group and session always eluded me, could anybody explain what significance having distinct sessions and process groups provide - why/when do one want to create a new session or place several processes in the same session and/or process group ?

Unix Solutions


Solution 1 - Unix

A process group is a collection of related processes which can all be signalled at once.

A session is a collection of process groups, which are either attached to a single terminal device (known as the controlling terminal) or not attached to any terminal.

Sessions are used for job control: one of the process groups in the session is the foreground process group, and can be sent signals by terminal control characters. You can think of a session with a controlling terminal as corresponding to a "login" on that terminal. (Daemons normally disassociate themselves from any controlling terminal by creating a new session without one.)

e.g. if you run some_app from the shell, the shell creates a new process group for it, and makes that the foreground process group of the session. (some_app might create some child processes; by default they will be part of the same process group.) If you then press ^Z, some_app's process group is signalled to stop it; and the shell's process group is switched to be the foreground process group again. Then e.g.bg %1 would start some_app's process group again, but keep it running in the background.


The POSIX.1-2008 standard is fairly readable (at least, I think so!) - take a look at the definitions and the relevant sections of the "General Terminal Interface" chapter.

Solution 2 - Unix

Job control shells manipulate sessions or process groups all the time. You can send the same signal to all the processes in a process group with a single call to the POSIX kill() function.

The POSIX standard says:

> If pid is greater than 0, sig shall be sent to the process whose process ID is equal to pid. > > If pid is 0, sig shall be sent to all processes (excluding an unspecified set of system processes) whose process group ID is equal to the process group ID of the sender, and for which the process has permission to send a signal. > > If pid is -1, sig shall be sent to all processes (excluding an unspecified set of system processes) for which the process has permission to send that signal. > > If pid is negative, but not -1, sig shall be sent to all processes (excluding an unspecified set of system processes) whose process group ID is equal to the absolute value of pid, and for which the process has permission to send a signal.

When a login shell exits, for example, a SIGHUP signal is sent to all programs in its process group.

When you manipulate programs into foreground or background, you are using process groups.

There are also controlling terminals to worry about; signals generated by a controlling terminal can be sent to all programs in a process group.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHabalusaView Question on Stackoverflow
Solution 1 - UnixMatthew SlatteryView Answer on Stackoverflow
Solution 2 - UnixJonathan LefflerView Answer on Stackoverflow