Not every shell process, only a login shell creates a new session (more details below). At the start the session has obviously only one member, the shell itself. Each newly forked process belongs automatically to the same session. A process may remove itself from its session by creating a new one with setsid()
. There is no other way for a process to change its session.
Summary:
session members = login shell
AND all child processes (and their child processes, etc.)
EXCEPT child processes that have created its own sessions
A remaining question is if all processes spawned by the shell are always started as "jobs", i.e. one process group per command. While this is normally true, I do not know for sure for non-interactive shells.
UPDATE
This question was about bash
, but the same can be said about other similar command line oriented shells with job control.
As @StéphaneChazelas pointed out, a session is created by the program that manages logins at a terminal or pseudoterminal device. A session is strictly tied with its terminal.
The user must log in first, only then his or her shell program can be determined from the corresponding record in the passwd
file. That shell program gets exec
ed - PID remains the same. A session is thus created inside a process which becomes the user's login shell.