If not, then when does it diverge?
(I couldn't make them diverge.)
And what other traits do SID-sharing processes always share?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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  2902 2903 PID PGID SID TTY TIME CMD 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 (
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.