Processes and Job Control
Processes and Job Control#
Concepts#
What Is a Process?#
A process is a running instance of a program. When you type ls, the shell creates a process to execute the ls program. When the command finishes, the process dies.
Every process has:
| Attribute | Description |
|---|---|
| PID | Process ID — a unique number identifying the process |
| PPID | Parent Process ID — the PID of the process that started it |
| UID | User ID — which user owns the process |
| State | Running, sleeping, stopped, zombie, etc. |
| Memory | How much RAM the process is using |
| CPU | How much CPU time it is consuming |
When you open a terminal, a shell process (e.g., Bash, PID 1234) starts. Every command you run becomes a child process of that shell. The system’s first process (PID 1) is systemd (or init), which is the ancestor of all processes.
systemd (PID 1)
├── sshd (PID 500)
│ └── bash (PID 1234) ← your shell
│ ├── ls (PID 5678) ← commands you run
│ └── grep (PID 5679)
├── nginx (PID 600)
└── cron (PID 700)
Viewing Processes#
ps — Process Snapshot#
ps # show processes in current terminal
ps aux # show ALL processes on the system
ps -ef # show ALL processes (alternative format)
ps -u kmiguel # show processes owned by user
ps -p 1234 # show info about PID 1234
ps aux --sort=-%mem # sort by memory usage (descending)
ps aux --sort=-%cpu # sort by CPU usage (descending)
ps aux output columns:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 168836 12944 ? Ss 08:00 0:02 /sbin/init
kmiguel 1234 0.0 0.1 11436 5324 pts/0 Ss 08:01 0:00 bash
| Column | Meaning |
|---|---|
USER |
Process owner |
PID |
Process ID |
%CPU |
CPU usage percentage |
%MEM |
Memory usage percentage |
VSZ |
Virtual memory size (KB) |
RSS |
Resident Set Size — actual physical memory used (KB) |
TTY |
Terminal associated (? = no terminal, daemon) |
STAT |
State code |
TIME |
Total CPU time used |
COMMAND |
The command that started the process |
Process states (STAT column):
| Code | State |
|---|---|
R |
Running |
S |
Sleeping (waiting for an event) |
D |
Uninterruptible sleep (usually I/O) |
T |
Stopped (suspended) |
Z |
Zombie (finished but parent hasn’t collected its exit status) |
s |
Session leader |
+ |
Foreground process |
l |
Multi-threaded |
< |
High priority |
N |
Low priority (nice) |
top — Real-Time Process Monitor#
top
top shows a continuously updating view of processes. Key commands inside top:
| Key | Action |
|---|---|
q |
Quit |
M |
Sort by memory |
P |
Sort by CPU (default) |
k |
Kill a process (enter PID) |
u |
Filter by user |
1 |
Show individual CPU cores |
h |
Help |
htop — Better Interactive Process Viewer#
sudo apt install -y htop
htop
htop is a friendlier version of top with colors, mouse support, horizontal scrolling, and tree view. Press F5 for tree view, F6 to change sort column, F9 to kill, F10 or q to quit.
pgrep — Find Processes by Name#
pgrep bash # PIDs of all bash processes
pgrep -u kmiguel # PIDs of all processes by user
pgrep -a nginx # PIDs and full command lines
pgrep -c bash # count bash processes
Foreground and Background#
By default, commands run in the foreground — your terminal waits for the command to finish before accepting new input.
Running in the Background#
# Add & to run a command in the background
sleep 60 &
# [1] 12345
# Shell says: job 1, PID 12345, and gives you your prompt back
# List background jobs
jobs
# [1]+ Running sleep 60 &
Job Control#
# Start a command normally (foreground)
sleep 120
# Suspend it: press Ctrl+Z
# [1]+ Stopped sleep 120
# Send it to background (resume in background)
bg
# or: bg %1
# Bring it back to foreground
fg
# or: fg %1
# List all jobs
jobs
| Command | Action |
|---|---|
command & |
Start in background |
Ctrl + Z |
Suspend (stop) the foreground process |
bg or bg %N |
Resume a stopped job in the background |
fg or fg %N |
Bring a job to the foreground |
jobs |
List all shell jobs |
nohup and disown — Keep Running After Logout#
When you close your terminal, all its child processes receive a hangup signal (SIGHUP) and typically die.
# nohup: immune to hangup, output goes to nohup.out
nohup long_command &
# disown: detach a running job from the shell
long_command &
disown %1
# The process continues even if you close the terminal
Process Priority: nice and renice#
Every process has a niceness value from -20 (highest priority) to 19 (lowest priority). Default is 0. Higher niceness = lower priority = “nicer” to other processes. I call it the “good boy index”.
# Start a process with lower priority
nice -n 10 make -j4
# Change priority of a running process
renice 10 -p 12345 # set niceness to 10
sudo renice -5 -p 12345 # set to -5 (only root can increase priority)
# Check niceness
ps -o pid,ni,comm -p 12345
Lab#
Exercise 1: View Processes#
# See your current shell's processes
ps
# See all system processes
ps aux | head -20
# Find bash processes
ps aux | grep "[b]ash"
# Your processes
ps -u $(whoami)
# Sort by memory usage
ps aux --sort=-%mem | head -10
Exercise 2: Use top and htop#
# Run top
top
# Watch the display for 30 seconds
# Press P to sort by CPU, M to sort by memory
# Press 1 to see individual CPU cores
# Press q to quit
# Run htop (if installed)
htop
# Use arrow keys to scroll, F5 for tree view
# Press q to quit
Exercise 3: Background Jobs#
# Start a process in the background
sleep 300 &
echo "Shell is free! PID of sleep: $!"
# Start another
sleep 200 &
# List jobs
jobs
# Start a foreground process and suspend it
sleep 100
# Press Ctrl+Z
# [3]+ Stopped sleep 100
# See all jobs now
jobs
# Send the stopped one to background
bg %3
# Bring job 1 to foreground
fg %1
# Press Ctrl+C to kill it
# Kill remaining jobs
kill %2 %3
jobs
Exercise 4: Process Tree#
# View the process tree
ps auxf | head -40
# Or use pstree (may need to install)
pstree | head -30
# See your own process tree
pstree -p $(whoami)
Exercise 5: pgrep#
# Find all bash processes
pgrep bash
# With full command line
pgrep -a bash
# Count
pgrep -c bash
# Find processes by your user
pgrep -u $(whoami) -a | head -10
Exercise 6: nice#
# Start a process with low priority
nice -n 15 sleep 60 &
# Check its niceness
ps -o pid,ni,comm -p $!
# Try to renice it
renice 19 -p $!
ps -o pid,ni,comm -p $!
# Clean up
kill $!
Review#
1. What is a process and what uniquely identifies it?
A process is a running instance of a program. It is uniquely identified by its PID (Process ID), a number assigned by the kernel. Each process also has a PPID (Parent Process ID) identifying who started it.
2. What is the difference between `ps` and `top`?
ps takes a snapshot of processes at a single moment and exits. top shows a continuously updating, real-time view of processes. Use ps for scripting and one-off checks; use top/htop for interactive monitoring.
3. How do you run a command in the background?
Append & to the command: command &. The shell starts the process in the background and immediately returns to the prompt.
4. What does Ctrl+Z do?
It suspends (stops) the foreground process and returns you to the shell. The process is paused, not killed. You can resume it in the background with bg or in the foreground with fg.
5. What is the difference between `nohup` and `disown`?
nohup is used before starting a command — it makes the process immune to SIGHUP from the start and redirects output to nohup.out. disown is used after starting a background job — it removes the job from the shell’s job table so closing the terminal does not send SIGHUP.
6. What does the niceness value control?
Process scheduling priority. Niceness ranges from -20 (highest priority, runs more) to 19 (lowest priority, runs less). Default is 0. Only root can set negative niceness (higher priority).
7. What is a zombie process?
A process that has finished executing but whose parent has not yet collected its exit status (via wait()). It appears with state Z in ps. Zombies use no resources except a PID table entry. They are cleaned up when the parent reads the exit status or when the parent dies.
Previous: Sorting, Cutting, and Counting | Next: Signals