Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use and meaning of session and process group in Unix?

Tags:

unix

process

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 ?

like image 606
Habalusa Avatar asked Jul 01 '11 13:07

Habalusa


People also ask

What is a process group in Linux?

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.

What process group means?

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.

What is a session in Linux?

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.

What is session leader?

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.


1 Answers

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.

like image 110
Matthew Slattery Avatar answered Oct 14 '22 21:10

Matthew Slattery