Issue
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 ?
Solution
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.
Answered By - Matthew Slattery Answer Checked By - Timothy Miller (WPSolving Admin)