KERNELPID 42PC L3PID 43PC L2CPUPCSPPTcodedataheapstackcodedataheapstack1int n = 0;2n = n + 1;3printf("%d\n", n);424301
Back: the kernel's process table (PCBs) and the CPU's registersFront: the program file and one address space per processTokens: a PID, copied code, values and a register snapshot movingAt the back: the kernel with its process table (one PCB per process) and the CPU with three registers: PC, SP and PT. In front: the program file and one address space per process. The kernel opens PCB 42, copies the code into its address space and loads its registers; n = 1 lands on its stack. The registers are saved into the PCB; a second process, 43, gets its own PCB and memory and its own n. Then 42's registers are loaded back and it carries on at line 3. The PIDs 42 and 43 are examples: the kernel hands out whatever number is free, and getpid() tells a process its own.

Program and process

  1. 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

Two 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.