Knowledge

Linux process: sessions

Group commands that work together

Suppose you search for a word and then sort the matches. The terminal runs two programs as one job. Pressing Ctrl+C usually interrupts both together, while a long-running job can be sent to the background so you can type another command. Linux tracks which programs belong together and which terminal job is in front.

A process group is a set of related processes; its process group ID, or PGID, identifies it. A session groups one or more process groups and has a session ID, or SID. Here, “session” means these Linux relationships, not a user account or a website login. A new process normally inherits its PGID and SID from its parent. Those identifiers continue when a process replaces its program with exec, though programs can change groups or create sessions using other system calls. An interactive shell can put one command or pipeline into a process group and choose one group as the terminal's foreground job. Background jobs stay in the session without controlling the foreground terminal.

The terminal connection affects input and signals. A background process group that tries to read from the controlling terminal is normally stopped with SIGTTIN. If terminal settings enable stopping background writes, a background write can generate SIGTTOU. Pressing Ctrl+C normally sends SIGINT to the foreground process group, which is why one keystroke can interrupt every command in a pipeline.

Services, scheduled jobs, and commands with redirected input may have no controlling terminal. They still have process and session identifiers; a blank terminal field does not mean there is no SID. ps can show the relevant columns:

ps -o pid,ppid,pgid,sid,tpgid,tty,stat,cmd

PID identifies the process, PPID its parent, PGID the group, and SID the session. TTY names a connected terminal when there is one. TPGID is that terminal's foreground process group ID. - or -1 commonly appears when no controlling terminal is associated, depending on the ps implementation. Compare the output while a foreground command and a background job are running; their group and terminal fields show how job control connects them.

The setsid() system call creates a new session when the caller is not already a process-group leader. The caller becomes the session leader and leader of a new process group; the new session starts without a controlling terminal. The setsid utility can start a command this way, though its behavior may differ from calling the system call directly.

Starting a new session changes terminal and job-control relationships. It does not supervise a process or restart it after failure; those tasks need separate mechanisms.

Interactive exercises

Skillsets
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful