If not, then when does it diverge?

(I couldn't make them diverge.)

And what other traits do SID-sharing processes always share?


Sessions contain process groups which contain processes.

Those were created mostly for terminal job control. A session is typically associated to a login session in a terminal, or the whole shell session in a terminal emulator.

When you login or start xterm, a new process is created that starts a new session (which automatically starts a new process group) and that process (which is now both a session leader and process group leader) executes your login shell. By opening the terminal device without O_NOCTTY, that process also associates that terminal device to the session (and becomes the controlling process of that terminal). That terminal device becomes the controlling terminal of all the processes in that session.

That shell then runs jobs each in their own process group (by forking a process that starts a new process group and may end up forking several other processes in that group (for instance for pipelines like cmd1 | cmd2)) and does special ioctl()s to the terminal device to tell it which of those jobs is in foreground (so they can do I/O to the terminal and receive the SIGINT upon CTRL-C for instance).

So, yes, you typically have several process groups per session.

For instance in an interactive shell:

$ sleep 1 | sh -c 'sleep 2; :' & ps -j
[1] 2902 2903
 2902  2902  4648 pts/23   00:00:00 sleep
 2903  2902  4648 pts/23   00:00:00 sh
 2904  2904  4648 pts/23   00:00:00 ps
 2905  2902  4648 pts/23   00:00:00 sleep
 4648  4648  4648 pts/23   00:00:00 zsh

3 processe groups, one in foreground (ps), one in background with 3 processes (sleep, sleep, sh) and my interactive shell.

All processes of a process group are necessarily in the same session as creating a new session (which you can't do if you're a process group leader) automatically creates a new process group.

I recommend reading

info libc 'Job Control'

on a GNU system for more information.

| improve this answer | |
  • "you typically have several process groups per session" -- What things are invariant in a session then? What traits/attributes/anything will always be shared by processes with a common SID? – dziadek1990 Jun 13 '15 at 20:55
  • 1
    @dziadek1990. The controlling terminal if any. Sessions are also used for automatic signal delivery in some circumstancies. – Stéphane Chazelas Jun 13 '15 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.