0

Process, process group, and (process group) session are concepts of Linux.

If I am correct, in a bash shell, the process groups started in it are called jobs.

Do all the jobs started in a bash shell exactly form a session? "Exactly" means no more and no less.

muru
  • 72,889
Tim
  • 101,790
  • Note that "jobs" is a bash concept, whereas processes, process groups and sessions are kernel concepts. Also see https://unix.stackexchange.com/q/18166/135943. – Wildcard Aug 10 '17 at 01:38

1 Answers1

4

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 execed - PID remains the same. A session is thus created inside a process which becomes the user's login shell.

VPfB
  • 801
  • 1
    sessions are not created by login shells. You terminal emulator, sshd/telnetd/rlogind or getty typically create sessions. Usually, those typically execute a shell in the process where they did a setsid() but they don't have to, nor are the shells they start always invoked as login shells. Graphical session managers, some init systems typically also create sessions, and generally don't make shells the session leaders. – Stéphane Chazelas Aug 14 '17 at 17:20
  • @StéphaneChazelas Thank you for adding these details. I have used the term login shell for the process (one PID) that becomes the login shell. This is an oversimplification. I will edit my answer later. – VPfB Aug 14 '17 at 21:52