Unit 8: Process Management
I. Orientation: Processes in a Unix/Linux System
A process is a program in execution — the fundamental unit of work the operating system schedules and controls. The kernel creates a process whenever a program is loaded into memory and given CPU time, tracking each one through a data structure (the process control block) and a hierarchy rooted at the first process, init/systemd (PID 1). Process management is the set of commands and signals used to observe, prioritise, suspend, resume and terminate these processes.
Defining properties this unit relies on:
- State: every process is in one of a fixed set of states — running (
R), sleeping/interruptible (S), uninterruptible sleep (D), stopped (T), or zombie (Z) after exit but before the parent reaps it. - Ownership: each process has a real and effective UID/GID, so permission to signal a process depends on the user.
- Hierarchy: every process has a parent (PPID); killing a parent can orphan children, which are re-parented to PID 1.
- Foreground vs background: a shell runs one job in the foreground (holding the terminal) while others run in the background, detached from keyboard input.
- Signals: the kernel controls processes by delivering numbered signals (e.g.
SIGTERM15,SIGKILL9,SIGSTOP19,SIGCONT18).
II. Concept of Processes — Program Execution as a Managed Entity
A. Definition and life cycle
A process is an instance of a running program together with its execution context (memory, registers, open files, environment).
- Creation: a parent calls
fork()to duplicate itself, thenexec()to replace its image with a new program:bash → fork → exec("/bin/ls"). - Address space: each process gets its own virtual memory — text (code), data, heap and stack segments — isolated from others.
- Termination: a process ends via
exit()or an unhandled signal; its exit status is returned to the parent throughwait(). - Zombie state: a terminated child whose status has not yet been collected shows as
Z; it holds only a PID entry until reaped.
B. Foreground and background execution
Processes differ by their relationship to the controlling terminal.
- Foreground: occupies the terminal and receives keyboard input; the shell waits for it to finish —
sleep 30blocks the prompt. - Background: launched with a trailing
&, runs concurrently while the prompt returns —sleep 30 &prints[1] 4521(job number and PID).
C. Significance
- Multitasking: the scheduler time-slices the CPU so many processes appear to run at once.
- Isolation: memory separation prevents one process corrupting another, improving stability and security.
III. Process ID (PID) — Identifying Every Process
A. Definition and allocation
The PID is a unique positive integer the kernel assigns to each process at creation.
- Uniqueness: no two live processes share a PID; numbers are reused only after a process exits.
- Key values: PID 1 is
init/systemd; PID 0 is the kernel scheduler. - PPID: the Parent Process ID links a process to its creator, forming the process tree viewable with
pstree. - Discovery:
echo $$prints the current shell's PID;pgrep firefoxprints PIDs matching a name.
B. Why the PID matters
- Addressing target: every management command (
kill,renice) needs a PID to act on a specific process —kill 4521. - Files under
/proc: the kernel exposes each process at/proc/<PID>/, e.g./proc/4521/statuslists its state, memory and UID.
IV. ps — Snapshot of Current Processes
A. Purpose and principle
ps prints a one-time static listing of processes at the moment it runs, unlike the live view of top.
- BSD syntax:
ps aux—a(all users' processes),u(user-oriented format),x(include processes with no terminal). - Standard syntax:
ps -ef—-e(every process),-f(full format showing PPID and command). - Columns: typical output shows
USER PID %CPU %MEM VSZ RSS STAT START TIME COMMAND.
B. Common usage
- Filtering:
ps aux | grep nginxisolates a named service. - Custom fields:
ps -eo pid,ppid,stat,commprints only chosen columns. - Tree view:
ps -ejHorps axjfshows the parent-child hierarchy indented.
$ ps -ef | head -2
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 09:12 ? 00:00:03 /sbin/initV. top — Real-Time Process Monitor
A. Purpose and principle
top displays a continuously updating, interactive view of running processes ranked by resource use, refreshing every few seconds (default ~3 s).
- Header summary: shows uptime, load averages (1/5/15 min), task counts by state, CPU percentages (
ususer,sysystem,ididle,waI/O wait) and memory usage. - Process rows:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND, wherePRis priority andNIthe nice value.
B. Interactive keys
- Sorting:
Psorts by CPU,Mby memory usage. - Signalling:
kprompts for a PID and signal to send;rrenices a process. - Filtering:
ulimits the display to one user;qquits.
C. Related tool
htop: a colourised, scrollable alternative allowing mouse selection and per-core meters — not always installed by default.
VI. jobs — Listing Shell Jobs
A. Purpose and principle
jobs lists the processes started from the current shell that are running in the background or stopped, each tagged with a job number in brackets.
- Job number vs PID:
[1]is a shell-local job spec, distinct from the system-wide PID; commands likefgandbgaccept%1. - Status markers:
Running,Stopped, plus+(current job, default target) and-(previous job).
B. Usage
- List with PIDs:
jobs -ladds each job's PID alongside its number. - Reference forms:
%1(job 1),%+or%%(current),%-(previous),%string(job whose command starts with string).
$ sleep 100 &
[1] 5233
$ jobs
[1]+ Running sleep 100 &VII. bg — Resuming a Job in the Background
A. Purpose and principle
bg resumes a suspended job and continues its execution in the background, freeing the terminal.
- Workflow: press
Ctrl+Zto stop a foreground job (sendsSIGSTOP), thenbg %1restarts it detached. - Effect: equivalent to having launched the command with
&, but applied after the fact.
B. Usage notes
- Default target:
bgwith no argument acts on the current job (+). - Limitation: a backgrounded job needing terminal input will stop again until brought to the foreground.
VIII. fg — Bringing a Job to the Foreground
A. Purpose and principle
fg moves a background or stopped job back into the foreground, reattaching it to the terminal so it again receives keyboard input and blocks the prompt.
- Selection:
fg %2targets job 2;fgalone targets the current job. - Pairing with
bg: the two are complementary —bgdetaches,fgre-attaches, andCtrl+Zsuspends between them.
B. Typical cycle
- Suspend:
Ctrl+Zon a running editor →[1]+ Stopped vim. - Resume in foreground:
fg %1returns control to the editor.
IX. kill — Sending Signals by PID
A. Purpose and principle
kill sends a signal to a process identified by PID; despite its name, it delivers any signal, not only termination.
- Default signal:
kill 5233sendsSIGTERM(15), asking the process to shut down cleanly so it can save state. - Forced kill:
kill -9 5233orkill -SIGKILL 5233cannot be caught or ignored and terminates immediately — a last resort. - List signals:
kill -lprints all signal names and numbers.
B. Signal choices and job specs
- Common signals:
SIGTERM(15, graceful),SIGKILL(9, forced),SIGHUP(1, reload config),SIGSTOP/SIGCONT(pause/continue). - Job spec form:
kill %1signals a shell job by its job number rather than PID.
$ kill -15 5233 # polite request to stop
$ kill -9 5233 # force if it ignores SIGTERMX. pkill — Signalling Processes by Name and Attribute
A. Purpose and principle
pkill signals processes matched by name or other attributes, so no PID lookup is needed — a pattern-based counterpart to kill.
- By name:
pkill firefoxsendsSIGTERMto every process whose name matches. - Signal selection:
pkill -9 firefoxforces termination of all matches.
B. Matching options and cautions
- Attribute filters:
-u userrestricts to a user's processes,-t pts/1to a terminal,-fmatches against the full command line. - Companion
pgrep: runpgrep -l firefoxfirst to preview which PIDs will be hit. - Risk: a broad pattern can signal unintended processes —
pkill sshmay kill the session daemon; scope the match before firing.
C. kill versus pkill
kill: precise, acts on explicit PIDs, safe when the exact target is known.pkill: convenient for groups by name, but broad patterns risk collateral termination.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →