Your laptop has one CPU core running one instruction at a time, yet dozens of programs seem to be running. Some of them are not running at all: they are waiting for a key press, a network reply or a file. How does the kernel keep track of who is doing what?
It keeps a record for every process, and the most important thing in that record is a state.
The PCB: the kernel’s record of a process
The kernel stores everything it needs to know about a process in one data structure, the process control block (PCB). Linux calls it a task_struct. A PCB holds:
- the PID, the process’s number, and the PID of its parent;
- the state, which says what the process is doing right now;
- the program counter and the other saved registers: where the process was in its code, and its values, when it last left the CPU;
- memory information: where the process’s address space is (Lesson 2.1);
- the open files, a list starting with standard input, output and error;
- scheduling information, such as the process’s priority (Module 3).
The PCB is what lets the kernel take a process off the CPU and put it back later as if nothing happened. The registers are only in the CPU while the process runs; the rest of the time they are in the PCB.
Five states
A process is always in exactly one of five states:
- New: being created. The kernel is building its PCB and address space.
- Ready: able to run, waiting for a CPU. Many processes can be ready at once.
- Running: on a CPU right now. With one core, at most one process is running.
- Waiting (also called blocked): cannot continue until something happens, such as a disk read finishing. It does not want the CPU.
- Terminated: finished, but its record has not been cleaned up yet.
Watch one process live through all of them. The circle is the process, and the fields on the right are its PCB. When it is not running, another process has the CPU.
One process, from creation to exit
-
A process is being created
A program has been started. The kernel is building its process: giving it a PID and a PCB, an address space and the standard open files. It cannot run yet, so its state is new.
- PID
- 4100
- State
- new
- Program counter
- 0x401a00
- Registers
- none yet
- Open files
- none yet
- CPU
- idle
-
Admitted: new to ready
The process is complete and the kernel admits it. It goes into the ready queue (the list of processes that could run right now) and waits for its turn.
- PID
- 4100
- State
- ready
- Program counter
- 0x401a00
- Registers
- saved in the PCB
- Open files
- stdin, stdout, stderr
- CPU
- another process
-
Dispatched: ready to running
The scheduler picks this process. The kernel loads its saved registers from the PCB and the CPU starts running it. Only one process per core can be in this state at a time.
- PID
- 4100
- State
- running
- Program counter
- 0x401a00
- Registers
- in the CPU
- Open files
- stdin, stdout, stderr
- CPU
- this process
-
Timer interrupt: running to ready
The timer interrupt (Lesson 1.2) fires. The kernel saves the registers into the PCB and takes the CPU back. The process did nothing wrong and still wants to run, so it goes back to ready and another process gets the CPU.
- PID
- 4100
- State
- ready
- Program counter
- 0x401a04
- Registers
- saved in the PCB
- Open files
- stdin, stdout, stderr
- CPU
- another process
-
Dispatched: ready to running
The scheduler picks this process. The kernel loads its saved registers from the PCB and the CPU starts running it. Only one process per core can be in this state at a time.
- PID
- 4100
- State
- running
- Program counter
- 0x401a04
- Registers
- in the CPU
- Open files
- stdin, stdout, stderr
- CPU
- this process
-
Asks for a file: running to waiting
The process calls read on a file that is not in memory yet. The disk is slow, so the kernel does not let it burn CPU time waiting. It moves the process to waiting (also called blocked) and runs someone else.
- PID
- 4100
- State
- waiting
- Program counter
- 0x401a08
- Registers
- saved in the PCB
- Open files
- stdin, stdout, stderr
- CPU
- another process
-
Disk done: waiting to ready
The disk finishes and raises an interrupt. The kernel moves the process to ready, not to running: it has to queue for the CPU like everybody else.
- PID
- 4100
- State
- ready
- Program counter
- 0x401a08
- Registers
- saved in the PCB
- Open files
- stdin, stdout, stderr
- CPU
- another process
-
Dispatched: ready to running
The scheduler picks this process. The kernel loads its saved registers from the PCB and the CPU starts running it. Only one process per core can be in this state at a time.
- PID
- 4100
- State
- running
- Program counter
- 0x401a08
- Registers
- in the CPU
- Open files
- stdin, stdout, stderr
- CPU
- this process
-
Exit: running to terminated
The process calls exit. The kernel frees its memory and open files, and keeps a small record (the exit status) until the parent collects it.
- PID
- 4100
- State
- terminated
- Program counter
- 0x401a08
- Registers
- saved in the PCB
- Open files
- stdin, stdout, stderr
- CPU
- another process
The arrows are the only moves a process can make. That gives a few facts you can rely on:
- Only the scheduler moves a process from ready to running. This move is called dispatching.
- A running process leaves the CPU in one of three ways: its time slice ends (back to ready), it must wait for something (waiting), or it exits (terminated).
- Waiting never leads straight to running. When the thing it waited for happens, the process becomes ready, and then competes for the CPU.
The states on a real machine
Linux shows the state of every process as a letter. This C program starts two children, one that sleeps and one that exits straight away, waits a moment, and then reads each state from /proc/PID/stat, the file the kernel keeps for every process. It also reads its own state. You will meet the zombie in the next lesson.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
static char state_of(pid_t pid) {
char path[64], buf[512];
snprintf(path, sizeof path, "/proc/%d/stat", (int)pid);
FILE *f = fopen(path, "r");
if (!f) return '?';
char *got = fgets(buf, sizeof buf, f);
fclose(f);
if (!got) return '?';
char *close = strrchr(buf, ')'); /* the state letter follows the program name in brackets */
return close ? close[2] : '?';
}
int main(void) {
pid_t sleeper = fork();
if (sleeper == 0) { sleep(5); _exit(0); }
pid_t quitter = fork();
if (quitter == 0) _exit(0);
usleep(300000); /* give the children time to settle */
printf("me, running this code: %c\n", state_of(getpid()));
printf("child asleep in sleep(): %c\n", state_of(sleeper));
printf("child that quit, not waited for: %c\n", state_of(quitter));
kill(sleeper, SIGKILL);
waitpid(sleeper, NULL, 0);
waitpid(quitter, NULL, 0);
return 0;
}
Output (one run)
me, running this code: R child asleep in sleep(): S child that quit, not waited for: Z
The program asking is, of course, running: R. The sleeping child is waiting for a timer, so it shows S. The child that quit shows Z, for zombie: a terminated process nobody has collected yet. This is the state “terminated” from the picture.
In the real world
Run ps -o pid,stat,cmd on Linux and the STAT column holds one letter per process (sometimes followed by extra flags):
| Letter | Meaning | Closest state above |
|---|---|---|
R |
Running, or ready and waiting for a CPU. Linux uses one letter for both | running and ready |
S |
Sleeping: waiting for an event that can be interrupted, such as a timer or a key press | waiting |
D |
Sleeping and not interruptible: usually waiting for the disk or a device. You cannot even kill it until the device answers | waiting |
T |
Stopped, for example by Ctrl+Z | waiting |
Z |
Zombie: terminated, not yet collected | terminated |
Most processes on an idle machine are S, because most of them are waiting for something. A process stuck in D for a long time usually means a slow or broken disk or network drive, which is why you will see it in outage stories.
Quick check
Quick check
-
The saved program counter and registers are the key. The PCB holds what the CPU would otherwise lose when another process takes over.
-
The process cannot continue until the data arrives, so it would only waste CPU time. The kernel parks it as waiting.
-
Only the scheduler moves a process onto the CPU. The disk interrupt just says that the process is able to run again.
-
The PCB describes the process: identity, state, registers, memory and open files. Output goes to the terminal, where it belongs to the terminal, not to the process.
Recap
- The kernel keeps a PCB per process: PID, parent, state, saved registers, memory information, open files and scheduling details.
- A process is new, ready, running, waiting (blocked) or terminated.
- The scheduler dispatches a ready process onto the CPU. A timer interrupt sends it back to ready. Asking for slow I/O sends it to waiting.
- A waiting process becomes ready, never running, when its event happens.
- On Linux,
psshows the state asR,S,D,TorZ.
Next: where do new processes come from? On Unix, from one remarkable system call that copies the process that makes it.
Join the discussion