Processes¶
Practical 5.5 processes-annex.pdf · PDF
Open the calculator on your computer. Now open it again. How many calculators are installed? One. How many are running? Two. Right now your computer is running more than a hundred programs, and you started maybe three of them. Who started the rest? And can you stop them?
For a week you have protected files that sit still on the disk. Today the files wake up. When your laptop fan goes crazy, when a game freezes and ignores every click, when the computer feels slow for no reason, the answer is always in the same place: the list of running processes. Today you learn to read it and to act on it.
What you will learn
- Tell a program from a process, and say what a PID and a parent process are
- List processes with
ps,pstree,topandhtop, and find the one eating the CPU - Run a command in the background, list jobs and move them between background and foreground
- Send signals with
kill, and whykill -9is the last thing you try, not the first - Lower the priority of a process with
niceandrenice - Do the same on Windows with Task Manager,
Get-ProcessandStop-Process
Recipe or dish? Program and process¶
Back to the kitchen of Unit 1: the CPU is the chef, RAM is the countertop, the disk is the pantry, and the operating system is the kitchen manager.
A program is a recipe in the book. A process is a dish being cooked: a program that is running.
The recipe sits in the pantry (the disk) and does nothing. When you start it, the recipe is copied to the countertop (RAM), together with its ingredients, the step it is on, and an order ticket with a number. The chef works on one dish per hand (each core of the CPU) and jumps between dishes many times per second, so they all seem to cook at once. The kitchen manager (the OS) hands out the tickets and decides who gets the chef next.
| Program | Process | |
|---|---|---|
| Kitchen | The recipe in the book | A dish being cooked |
| Lives in | The disk | RAM, while it runs |
| How many | One copy installed | As many as were started |
| Has | A path, like /usr/bin/sleep |
A PID, an owner, a state, a parent |
Careful: a process is not the same as an app
One app can be many processes: a web browser runs one per tab. And most processes have no window at all. They work in silence in the background.
PID and parents: the family tree¶
The PID (Process ID) is the ticket number of a process: a number that identifies it in the whole system.
Every process is started by another one, its parent. The parent's PID is the PPID (Parent Process ID). Ask for process number 1:
PID 1 is systemd, the first process the kernel starts when the machine boots (remember the relay race in Boot). Its parent is 0, the kernel itself. Every other process descends from it, so the processes form a family tree.
flowchart TB
K["<b>Kernel</b><br>shown as PPID 0"] --> S["<b>systemd</b><br>PID 1: the first ticket"]
S --> G["<b>GNOME session</b><br>your desktop"]
S --> D["<b>Background services</b><br>nobody started them by hand"]
G --> T["<b>Terminal</b><br>the window you type in"]
T --> B["<b>bash</b><br>your shell"]
B --> P["<b>ps, sleep, ...</b><br>what you run"]
A process also carries the identity of the user who started it. The permissions you studied last week are checked against that user: a process started by marta can only touch what marta can touch.
Look at what is running¶
Four commands, from a photo to a live video. On deb-lab, install the two that are missing first (this needs the network once):
ps: a photo¶
ps # only the processes of this terminal: bash and ps itself
ps aux | wc -l # count every process in the system (plus one header line)
ps -u $USER | wc -l # count only yours
A Debian with GNOME shows roughly 150 to 250 processes. Yours are a small part. ps aux is the full photo; its most useful columns are:
| Column | Meaning |
|---|---|
USER |
Who owns the process |
PID |
Its ticket number |
%CPU |
Share of the CPU it is using |
%MEM |
Share of the RAM it is using |
STAT |
Its state (see below) |
COMMAND |
The program and its arguments |
ps aux, not ps -aux
These options come from the old BSD Unix style, which takes no dash. With a dash, some systems print a warning.
pstree: the family tree¶
The root of the tree is systemd(1). Follow it with your finger down to your own bash: it hangs from the terminal, which hangs from your desktop session.
top and htop: the live video¶
top refreshes the list every 3 seconds. Inside it, press P to sort by CPU, M to sort by memory, and q to quit. htop does the same with colours, one bar per CPU core and the function keys at the bottom: F6 to sort, F9 to kill, F10 to quit.
The first line of top ends with load average:. Keep it in mind: you will read it in Monitoring performance.
A bit of history
top was written by William LeFebvre in 1984. Twenty years later, in 2004, Hisham Muhammad wanted to scroll the list with the arrow keys and wrote htop. Both are still installed on servers today.
Process states¶
The STAT column (or S in top) is one letter:
| Letter | State | In the kitchen |
|---|---|---|
R |
Running: using the CPU or ready to | The chef is cooking it now |
S |
Sleeping: waiting for something (time, a key, the network) | The dish is in the oven |
T |
Stopped: paused by a signal | The dish is put aside on the counter |
Signals: you can only send a message¶
You cannot reach into a running process and change it. You can only send it a signal: a numbered message delivered by the kernel. The command for that is badly named: kill sends signals, and most of them are polite.
| Signal | No. | Keyboard | Meaning |
|---|---|---|---|
SIGINT |
2 | Ctrl+C | Interrupt: "please stop", from the keyboard |
SIGTERM |
15 | Terminate: "please finish and leave". What kill PID sends |
|
SIGKILL |
9 | The kernel removes the process. It cannot be caught or ignored | |
SIGTSTP |
20 | Ctrl+Z | Pause, from the keyboard |
SIGSTOP |
19 | Pause. It cannot be ignored either | |
SIGCONT |
18 | Continue after a pause |
In the kitchen, SIGTERM is "last orders": the cook plates what is on the fire and cleans up. SIGKILL is switching off the gas: the dish is lost. A program that is saving a file when it gets kill -9 leaves half a file.
The rule
Always kill first. Count to five. Only then kill -9.
The proof is a small script, stubborn.sh, that ignores SIGINT and answers SIGTERM politely:
#!/bin/bash
trap 'echo "TERM received: saving and leaving"; exit 0' TERM
trap '' INT
echo "stubborn started with PID $$"
while true; do sleep 1; done
Ctrl+C does nothing: the script ignores it. From a second terminal, kill 4636 makes the first one print TERM received: saving and leaving, and the prompt comes back. Run it again and use kill -9 4636: the first terminal prints only Killed. The goodbye message never appears, because the process had no chance to save.
Closing the terminal is a signal too
If you close the window instead, the shell sends SIGHUP ("the terminal hung up") to what is running inside it.
Jobs: background and foreground¶
Normally a command takes over the terminal until it ends: it runs in the foreground. Add & at the end and it runs in the background: the terminal is free at once.
$ sleep 300 &
[1] 317
$ sleep 400 &
[2] 318
$ jobs -l
[1]- 317 Running sleep 300 &
[2]+ 318 Running sleep 400 &
[1] is the job number: it belongs to this shell only. 317 is the PID: it is valid in the whole system. With them:
| Command | What it does |
|---|---|
jobs -l |
List the jobs of this shell, with their PIDs |
fg %1 |
Bring job 1 to the foreground |
| Ctrl+Z | Pause the job in the foreground |
bg %1 |
Let a paused job continue in the background |
kill -STOP %1 / kill -CONT %1 |
Pause / continue job 1 with a signal |
kill %1 |
Send SIGTERM to job 1 |
$ kill -STOP %1
[1]+ 317 Stopped (signal) sleep 300
$ kill -CONT %1
$ kill %1
[1]+ 317 Terminated sleep 300
Careful: Ctrl+Z does not close anything
It pauses the program. It is still in memory: jobs shows it, and fg brings it back. And kill %1 only works in the shell that started the job; in another terminal, use the PID.
Priorities: who gets the chef first¶
When there are more hungry processes than cores, the manager has to share the chef.
The nice value of a process goes from −20 (first in line) to 19 (after everyone else). The default is 0. A "nice" process lets the others go first.
$ nice -n 10 sleep 500 &
$ renice -n 15 -p 326
326 (process ID) old priority 10, new priority 15
$ renice -n 5 -p 326
renice: failed to set priority for 326 (process ID): Permission denied
nice starts a process with a given value; renice changes a running one. A normal user can only make their own processes nicer (a higher number). Going back down, or below 0, needs sudo. Why? Otherwise everybody would put their own processes first.
Careful: nice 19 does not mean slow
A higher number means less priority, not less speed. If nobody else wants the CPU, a process with nice 19 still gets 100 % of a core. Priority only matters when processes compete.
The runaway process: a real case¶
Marta's laptop at Tramuntana Apps has the fan at full speed. Something is eating the CPU. You can rehearse it: yes is a program that writes "y" for ever.
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
5062 marta 20 0 2584 1528 1420 R 100.0 0.0 0:01.19 yes
State R, about 100 %. Then the three moves of a calm administrator:
renice -n 19 -p 5062 # 1. calm it down: last in line
kill 5062 # 2. stop it politely
pgrep -a yes # 3. check: prints nothing if it is gone
pkill yes # or end every process called yes, by name
Careful: 100 % is one core
In top, 100 % means one whole core, not the whole machine. On the 2-vCPU deb-lab, the summary line at the top shows about 50 % while yes shows 100 % in the list. A machine with two busy cores can show 200 %.
The same on Windows¶
Same ideas, other names. Ctrl+Shift+Esc opens Task Manager; its Details page shows the PID of each process.
| Linux | Windows |
|---|---|
ps aux |
Get-Process |
top, htop |
Task Manager → Processes / Details |
kill PID |
Stop-Process -Id PID |
pkill name |
Stop-Process -Name name |
renice |
Task Manager → Details → right-click → Set priority |
Start-Process notepad
Get-Process notepad # one line, with its Id
Stop-Process -Name notepad # -Name is needed: the first parameter is the Id
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5
The last line is the PowerShell pipeline at work: the five processes with the most CPU time.
One real difference: Windows has no family of polite signals. Stop-Process and End task simply end the process. A program may still ask "save changes?" when you close its window, but not when you stop the process.
Now do all of it yourself, step by step, on the Practical 5.5 sheet (button at the top of the page).
Key takeaways¶
- A program is a recipe on the disk; a process is that program running in RAM, with a PID, an owner, a state and a parent (PPID). Everything descends from
systemd, PID 1. ps auxis a photo,pstreethe family tree,top/htopthe live video. Intop, 100 % is one core.killsends signals:SIGTERM(15) asks politely and is the default;SIGKILL(9) cannot be caught, so the process cannot save.&runs a command in the background;jobs,fg,bgand Ctrl+Z move jobs around. Job numbers live in one shell, PIDs in the whole system.- The nice value goes from −20 (first) to 19 (last); a normal user can only make processes nicer. It only matters when processes compete.
- On Windows: Task Manager,
Get-Process,Stop-Process, and no polite signals.
Quick check¶
1. You open the calculator twice. What do you have?
- One program and two processes
- Two programs and one process
- Two programs and two processes
- One program and one process
The program is installed once on the disk. Each time you start it, the OS creates a new process in RAM, with its own PID. One recipe, two dishes.
2. Which signal does the command kill 4636 send?
- SIGTERM (15)
- SIGKILL (9)
- SIGINT (2)
- SIGSTOP (19)
Without a number, kill sends SIGTERM: "please finish and leave". The process can clean up first, or even ignore it. SIGKILL needs kill -9.
3. Why is kill -9 the last thing you should try?
- The process cannot catch it, so it gets no chance to save or clean up
- It needs sudo every single time you use it
- It also ends the parent process of the target
- It only works on jobs that run in the background
SIGKILL is removed by the kernel and cannot be caught or ignored. A program saving a file when it arrives leaves half a file. Polite kill first, then wait, then kill -9.
4. What does the & at the end of sleep 300 & do?
- It runs the command in the background, so the terminal is free at once
- It runs the command twice, one copy after the other
- It runs the command as root, without asking for a password
- It runs the command with the lowest possible priority
& starts the command as a background job. The shell prints the job number and the PID, and gives you the prompt back. fg would bring it to the foreground.
5. A process has nice value 19. What does that mean?
- It goes last in line when the CPU is busy
- It has the highest priority of all processes
- It may use only 19 % of the CPU
- It will end on its own after 19 seconds
The nice value goes from −20 (first in line) to 19 (last). A nice process lets the others go first, but if nobody else wants the CPU it still gets a whole core.
6. You pressed Ctrl+Z on a program in the terminal. Where is it now?
- Paused in memory, and jobs still lists it
- Closed, and its memory has been freed
- Running in the background, as with &
- Killed with SIGKILL, without saving
Ctrl+Z sends a pause signal: the process is stopped (state T) but still alive. fg brings it back to the foreground; bg lets it continue in the background.
7. Marta's laptop is slow. top shows yes at 100 % CPU on her 2-core machine. Select the two that are true.
- The process is using one whole core, not the whole machine
- The right first step to stop it is kill with its PID, without -9
- The machine has no CPU left at all for other processes
- renice -n 19 will make it drop well below 100 % straight away
In top, 100 % is one core, so the second core is still free. Stop it politely with kill PID (SIGTERM) first. renice only matters when processes compete: if nobody else wants the CPU, yes keeps its 100 %.
Next: the processes that nobody started by hand, and who brings them back when they die — Services and systemd.