Program and process
A program is a file on disk: instructions and some starting data, doing nothing. Here it is three lines of C. A process is that program while it runs, and the kernel builds one from the file.
Building a process
First the kernel gives the new process a number, its process ID (PID), here 42. It writes a record for it in its process table: the process control block, or PCB. The PCB will hold the state, saved registers and open files.
Next the kernel builds the process's address space: the memory it may use. It copies the code in from the file and sets up a stack for local variables such as n. There is room for global data and a heap too.
Registers are tiny stores inside the CPU. The kernel loads them from the PCB: the program counter (PC) holds the next line, the stack pointer (SP) points into 42's stack, and a page-table register picks 42's memory. Now 42 runs.
The CPU runs line 1, so n is 0 on the stack, then line 2, so n becomes 1. After each line the PC moves on to the next. Everything the process knows is here: in its memory and its registers.
Two processes
The kernel pauses 42 before line 3 so something else can run. It copies the CPU's registers, PC = line 3 included, into 42's PCB and marks it ready. Its memory stays where it is, with n still 1.
Run the same file again and the kernel makes a second process: PID 43, with its own PCB and its own address space. The code is copied in again. It is one program, but now two processes.
The kernel loads 43's registers: PC at line 1, SP and the page-table register on 43's memory. Line 1 sets 43's n to 0. 42's n is still 1: the same name, in a different address space.
The kernel saves 43's registers into PCB 43, then loads 42's saved registers back into the CPU. The PC says line 3, so 42 carries on exactly there, ready to print 1. It never noticed the pause.
A process is three things: an address space (its memory), its registers (where it is in the program), and its PCB, the kernel's record that keeps the registers safe while it waits. One program can run as many processes.
What happens
;; step 1/10 · program
the whole program (C99):
#include <stdio.h>
int main(void) {
int n = 0;
n = n + 1;
printf("%d\n", n);
return 0;
}
program a file on disk: lifeless, just bytes
process the program while it runs (OSTEP ch. 4)
Real CPUs have many more registers, for example the general-purpose ones that hold values being worked on. The figure shows three: the PC, the stack pointer and the page-table register (satp on RISC-V, CR3 on x86-64), which tells the CPU which address space to use. The code card stands for the instructions. The CPU really runs machine code compiled from these lines, and the PC holds the address of the next instruction; the figure shows line numbers instead.