Problem Statement
Explain job control in Linux shells. How do you manage foreground and background jobs, suspend and resume processes?
Explanation
Job control allows managing multiple processes from a single shell, switching between foreground and background execution. Start commands in background with &: command & returns control immediately. The shell assigns job number [1], [2], etc., and shows PID. Use jobs command to list jobs with status (Running, Stopped, Done) and job numbers.
Suspend foreground process with Ctrl+Z, which sends SIGTSTP (terminal stop). The process stops and control returns to shell, showing '[1]+ Stopped command'. Resume in foreground with fg %jobnumber or just fg for most recent job. Resume in background with bg %jobnumber, making the stopped job continue in background.
Job specifiers: %1 (job number 1), %% or %+ (current job), %- (previous job), %?str (job whose command contains str), %command (job whose command starts with command). Examples: fg %2 brings job 2 to foreground, kill %1 kills job 1. Without %, shell interprets numbers as PIDs not job numbers.
Practical workflow: start long compilation in foreground, press Ctrl+Z to suspend, run bg to continue in background, start another command in foreground. Use jobs to check status, fg %1 to bring back first job when ready to check it. This enables parallel work without multiple terminals. Understanding job control improves productivity on remote servers with limited terminal access.
Practice Sets
This question appears in the following practice sets:
