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 ?
Process Groups A process group in Linux, as the name suggests, is a way that Linux groups process together. They are a collection of related processes sharing the same PGID's (Process Group ID). A common misconception is that killing the parent process will kill the children's processes too.
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.
Before looking at the Linux implementation, first a general Unix description of threads, processes, process groups and sessions. A session contains a number of process groups, and a process group contains a number of processes, and a process contains a number of threads. A session can have a controlling tty.
A session leader is a process where session id == process id. This sounds contrived, but the session id is inherited by child processes. Some operations within UNIX/Linux operate on process sessions, for example, negating the process id when sending to the kill system call or command.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With