Process Management – Linux Teaching Plan
This 2‑hour module introduces students to the most commonly used tools for viewing, controlling and troubleshooting Linux processes. Students will gain hands‑on experience by launching, manipulating and profiling simple programs.
Learning Objectives
- Differentiate between foreground, background, and daemon processes.
- Identify running processes and their relationships.
- Send signals to control process execution.
- Use job control for shell‑based tasks.
- Profile CPU and memory usage.
- Manage process priority (nice/renice).
- Detect zombie processes and understand the kernel’s reaping mechanism.
- Write a simple process‑creation script for demo purposes.
Schedule & Topics
- Intro & PID concepts (5 min)
- Listing –
ps (10 min)
- Tree view –
pstree (5 min)
- Top‑like monitor –
top (10 min)
- Interactive monitor –
htop (10 min)
- Signal sending –
kill (10 min)
- Job control –
bg, fg, jobs (10 min)
- Process affinity –
taskset (10 min)
- Priority control –
nice/ renice (10 min)
- Tracing –
strace (10 min)
- Monitoring –
systemctl status & systemctl start/stop (10 min)
- Mini‑project: “Create & manage a sample service” (15 min)
- Q&A & wrap‑up (5 min)
Command Topics & Hands‑On Exercises
1. ps – View Current Processes
Shows the state of processes running in a shell.
ps aux
ps -ef
- List all processes owned by the current user.
- Find the PID of a running
sleep 60 command.
2. pstree – Process Hierarchy
Displays processes as a tree.
pstree -p
pstree -s 1234
- Launch a nested process: `sleep 60 &` → inside it run `sleep 120 &` → check tree.
- Show which process is the parent of the inner `sleep`.
3. top – Interactive CPU/Memory Monitor
Updates every 3 s by default.
top
top -p 1234
- Run
top and sort by memory usage (press M).
- Filter the view to show only
sleep processes (press u → user name).
4. htop – User‑friendly Top
Requires installation on some distros.
sudo apt install htop
htop
- Launch
htop and locate the process tree toggle (F5).
- Kill a selected process using F9 (choose SIGTERM).
5. kill – Send a Signal
Send signals to processes.
kill 1234 # SIGTERM
kill -KILL 1234 # SIGKILL
kill -USR1 1234
- Launch `sleep 1000 &` → note its PID → send SIGSTOP, then SIGCONT, finally SIGTERM.
- Send a custom signal to a program that prints the received signal number (e.g., a simple C script).
6. pkill / killall – Kill by Name
Terminates processes matching a pattern.
pkill -f "sleep 200"
killall -u bob # kill all bob's processes
- Start three `sleep 200` processes → kill them all with one command.
- Verify that no `sleep` processes remain with `ps -C sleep`.
7. bg, fg, jobs – Job Control
Manage jobs started from the interactive shell.
sleep 300 &
jobs
fg %1
bg %1
kill %1
- Start `sleep 300` in background → suspend it with Ctrl‑Z.
- List jobs, bring it to foreground, then background it again.
- Terminate the job with `kill` using its job number.
8. nice / renice – Process Priority
Adjusts scheduling priority.
nice -n 10 long_running_task &
renice -n -5 1234
- Launch a CPU‑intensive loop in the background with a low priority (nice +10).
- Using `top`, verify the priority of the process.
- Raise its priority to -5 with `renice` (requires root).
9. ps -o – Custom Output
Show only selected fields.
ps -e -o pid,ppid,cmd,stat
- Display PID, PPID, command name and state for all processes.
- Filter to show only processes with state “S” (sleeping).
10. top -d – Custom Refresh Interval
Set the refresh interval to 5 s.
top -d 5
- Run `top -d 5` and observe the updated refresh time.
- Start a background task that logs CPU usage to a file; watch its growth in `top` output.
11. pstree -A – ASCII Tree
Displays a compact tree with ASCII characters.
pstree -A -p
- Run the command after starting several background jobs.
- Identify the parent-child relationships from the output.
12. strace – System Call Tracing
Shows the system calls made by a program.
strace -p 1234
strace -e trace=read,write -o trace.log sleep 10
- Trace the `sleep` command and capture the output to a file.
- Filter `strace` to only show `open` system calls for a script that reads a file.
13. top -p – Monitor a Single PID
Focuses on a specific process.
top -p 1234
- Launch a CPU hog (`yes > /dev/null &`), note its PID, and then run `top -p` on it.
- Observe how CPU usage changes when you send SIGSTOP and SIGCONT.
14. systemctl status – Service Process Info
Shows the status of systemd units and their main PID.
sudo systemctl start ssh
systemctl status ssh
- Start a simple service (e.g., `apache2` or `ssh`) and verify its main PID.
- Stop the service and confirm the process has terminated.
15. ps -f – Full Format
Displays the full command line and process ancestry.
ps -f --forest
ps -f -C bash
- Launch a nested shell (`bash &` inside another `bash &`).
- Show the full tree with `ps -f --forest`.
Mini‑Project: “Process‑Based Service Lab”
Students write a small script that:
- Starts a long‑running background task (e.g., a simple Python HTTP server).
- Logs its PID to a file.
- Prints a friendly message when the process dies (by trapping SIGTERM).
- Provides commands to view its CPU and memory usage (`top -p`, `ps`, `htop`).
They will then:
- Start the service and verify it's listed in `ps`.
- Send SIGSTOP, check CPU usage, resume with SIGCONT.
- Terminate the service with SIGKILL and confirm it disappears.
Assessment
- Quiz (5 min) – 5 quick questions about signals and process states.
- Hands‑on lab (10 min) – create, suspend, resume, and kill a process; use `ps` to confirm.
- Script demonstration (10 min) – each student runs their process‑service script and explains the output.
Resources
Feel free to tweak the project or add optional tools such as systemd-run or ulimit for more advanced process‑management concepts.