Course contentsShow
← Linux
Lesson 238 of 1,8192. Deep Dive into Bash ScriptingFree lesson

Job Control Implementation

Analyze how shells manage process groups and terminal control.

In this lesson, you will learn how your shell acts as a traffic controller to manage multiple processes sharing a single terminal.

The Problem of Shared Input

When you run a command in your terminal, the shell gives that process "foreground" access. This means the process receives input from your keyboard and writes output to your screen. But what happens when you want to run a long task—like a sleep 1000—while still using your shell? If the process stays in the foreground, your prompt disappears, and you lose control. Job Control is the mechanism that solves this by juggling process groups.

Process Groups and Terminal Control

Every command you execute belongs to a process group. When you launch a command, the shell designates its process group as the "foreground group" for the terminal. The terminal driver is smart: it knows which process group is currently allowed to receive stdin (keyboard input) and stdout (screen output).

When you press Ctrl+Z, the shell sends a SIGTSTP signal to that process group. This pauses the process. The shell then updates its internal job table, moves that group to the "background," and resumes control of the terminal input for itself.

You can observe this "job table" in action using the jobs command:

# Start a process
sleep 1000 &
# List the background jobs
jobs
# Bring the job back to the foreground
fg %1

How the Shell Manages Transitions

The shell maintains a list of these jobs. When you type fg, the shell uses a system call to set that specific process group as the new Foreground Process Group ID (PGRP) for the terminal device. Once this is set, the kernel directs all subsequent terminal signals (like Ctrl+C for SIGINT) exclusively to that group, not to the shell itself. When the process finishes or is moved to the background, the shell re-claims the terminal, re-instating its own process group as the master of your input.

Key Takeaway: Job control is the shell’s way of mediating which process group "owns" the terminal device, allowing you to swap processes between the foreground and background by updating the terminal's active process group ID.