Process Groups, Sessions & Job Control

 

Chapter 34: Process Groups, Sessions & Job Control
Linux System Programming — The Linux Programming Interface (TLPI)
Master how Linux organises processes into groups, sessions, and how the shell controls jobs

13

Sections Covered

13

Tutorial Pages

26+

Code Examples

50+

Interview Q&A

What You Will Learn

When you run commands in a Linux terminal, the shell organises them into process groups and sessions. This two-level hierarchy is the backbone of shell job control — the feature that lets you run commands in the foreground and background, suspend them with Ctrl+Z, and resume them. This chapter explains every piece of that system: how groups and sessions are created, how the controlling terminal works, what happens when you close a terminal window, and how orphaned processes are handled by the kernel.

Key Concepts in This Chapter

Process Group PGID Session SID Session Leader Controlling Terminal Foreground Job Background Job SIGHUP SIGTSTP SIGCONT SIGTTIN SIGTTOU Job Control Orphaned Process Group setsid() setpgid() getpgrp() tcsetpgrp() /dev/tty

The Two-Level Process Hierarchy

SESSION (SID = shell PID)
Process Group
(Shell)
Process Group
(BG Job 1)
Process Group
(BG Job 2)
Process Group
(FG Job) ★
bash find | wc sleep 60 sort | uniq

★ Only the foreground process group can read from the controlling terminal

All Tutorial Pages

34.1 — Overview: Process Groups & Sessions
Introduction to the two-level hierarchy. What a process group is, what a session is, and how they relate to job control.
Process Group ID (PGID) Session ID (SID) Controlling Terminal Foreground/Background
34.2 — Process Groups: getpgrp() and setpgid()
How to read and change process group IDs. Rules for setpgid(), race conditions in job-control shells, and BSD vs POSIX interfaces.
getpgrp() setpgid() EACCES / EPERM errors Race conditions
34.3 — Sessions: getsid() and setsid()
How sessions are created with setsid(), why a process group leader cannot call setsid(), and the daemon creation pattern.
getsid() setsid() Session Leader Daemon Pattern
34.4 — Controlling Terminals and Controlling Processes
How a session acquires a controlling terminal, /dev/tty, O_NOCTTY, TIOCNOTTY, TIOCSCTTY, and the ctermid() function.
/dev/tty O_NOCTTY TIOCNOTTY ctermid()
34.5 — Foreground and Background Process Groups
How the terminal tracks the foreground group, tcgetpgrp(), tcsetpgrp(), and why only foreground processes can read from the terminal.
tcgetpgrp() tcsetpgrp() Terminal I/O arbitration
34.6 — The SIGHUP Signal
When and why SIGHUP is sent, terminal disconnects, chain reactions, and using SIGHUP to reinitialise daemons.
SIGHUP Terminal disconnect Chain reactions nohup
34.6.1 — Handling of SIGHUP by the Shell
How bash forwards SIGHUP to all jobs it created when the shell exits. Demo with catch_SIGHUP program. nohup and disown commands.
Shell SIGHUP handler nohup(1) disown catch_SIGHUP
34.6.2 — SIGHUP and Termination of the Controlling Process
When the controlling process dies, SIGHUP propagates to the foreground process group. Demo with disc_SIGHUP program.
Controlling process death Foreground SIGHUP SIGCONT follow-up
34.7.1 — Using Job Control Within the Shell
Practical tutorial on fg, bg, jobs, Ctrl+Z, Ctrl+C, kill, SIGTSTP, SIGTTIN, SIGTTOU, and the TOSTOP terminal flag.
fg / bg / jobs Ctrl+Z → SIGTSTP SIGTTIN / SIGTTOU TOSTOP flag
34.7.2 — Implementing Job Control
What the kernel, terminal driver, and shell each need to do to support job control. The job_mon program explained in detail.
Kernel requirements Terminal driver SIGCONT credentials job_mon demo
34.7.3 — Handling Job-Control Signals
How screen-handling programs (vi, less) correctly handle SIGTSTP: the 5-step protocol for stopping and resuming cleanly.
SIGTSTP handler pattern SIG_DFL reset Terminal restore handling_SIGTSTP
34.7.4 — Orphaned Process Groups and SIGHUP Revisited
What an orphaned process group is, why stopped members are dangerous, and how SUSv3 mandates SIGHUP+SIGCONT to rescue them.
Orphaned process group SIGHUP + SIGCONT EIO on terminal read orphaned_pgrp demo
34.8 & 34.9 — Summary and Exercises
Full chapter summary with a consolidated diagram, all exercises from the book, and a comprehensive interview Q&A bank.
Chapter summary TLPI Exercises Interview Q&A

Free Linux & Embedded Systems Tutorials

All tutorials on EmbeddedPathashala are completely free. Designed for students and freshers breaking into embedded Linux.

Start with 34.1 Overview →

Leave a Reply

Your email address will not be published. Required fields are marked *