The process: address space, registers and the PCB
About this lesson
A program on disk does nothing. A process is that program while it runs, and the kernel builds it from three parts: an address space (its own memory), the CPU's registers while it runs, and a record called the process control block (PCB). Watch the kernel turn three lines of C into process 42, pause it, run the same file again as process 43, and bring 42 back exactly where it stopped. The Linux lesson Processes and signals shows the commands such as ps and kill; this lesson shows what the kernel keeps for each process. User mode and kernel mode is a good warm-up.
A program is just a file
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.
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)Read the lesson step by step
A program is just a file
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.
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)The kernel opens a record
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.
PCB for PID 42 (xv6 calls it struct proc): pid 42 state new saved regs (filled in when it is paused) page table where its memory is open files 0 stdin, 1 stdout, 2 stderr a process can ask for its own PID (POSIX getpid): #define _POSIX_C_SOURCE 200809L #include <stdio.h> #include <unistd.h> int main(void) { printf("my pid is %ld\n", (long)getpid()); return 0; }Its own address space
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.
address space of PID 42: code the instructions, copied from the file data global and static variables heap memory asked for while running (malloc) stack local variables such as n on Linux, /proc/42/maps lists these regions, including [heap] and [stack]Registers: where it is
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.
register holds now PC the next line to run line 1 SP the top of the stack 42's stack PT which address space 42's PT is satp on RISC-V and CR3 on x86-64 PCB 42 state new -> runningLines 1 and 2 run
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.
line 1 int n = 0; stack: n = 0 PC -> line 2 line 2 n = n + 1; stack: n = 1 PC -> line 3 the kernel is not involved: plain work runs at full speed on the CPUPaused: registers saved
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.
PCB 42 saved PC = line 3, SP, PT state running -> ready memory untouched: n = 1 how and when the kernel takes the CPU back is the lesson "The context switch and the timer interrupt"Same program, new process
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.
PCB 43 new, saved PC = line 1 memory its own code, data, heap and stack PID 42 ready n = 1 PID 43 new n not set yet43 runs with its own n
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.
registers PC line 1, SP 43, PT 43 line 1 int n = 0; 43's stack: n = 0 42's stack n = 1 (not touched) 43's stack n = 042 picks up where it stopped
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.
save registers -> PCB 43 (43: ready, PC line 2) load PCB 42 -> registers (42: running, PC line 3) line 3 printf("%d\n", n); prints 1What a process is
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.
the bookkeeping as a Python simulation: table = {} # the kernel's process table: pid -> PCB def create(pid): memory = {"n": None} # its own address space table[pid] = {"state": "new", "pc": 1, "memory": memory} def run(pid, lines): # load registers, run, save them again pcb = table[pid] pc, mem = pcb["pc"], pcb["memory"] pcb["state"] = "running" for _ in range(lines): if pc == 1: mem["n"] = 0 if pc == 2: mem["n"] = mem["n"] + 1 if pc == 3: print(mem["n"]) pc += 1 pcb["pc"], pcb["state"] = pc, "ready" create(42); run(42, 2) # lines 1 and 2, then paused create(43); run(43, 1) # same program, its own n run(42, 1) # 42 resumes at line 3 print(table[42]["memory"], table[43]["memory"]) prints: 1 {'n': 1} {'n': 0}
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01What is the difference between a program and a process?
The program is lifeless bytes on disk. The kernel turns it into a process by giving it memory, registers and a PCB. One program can run as many processes at once.
02In the figure, where does the local variable n live?
C programs keep local variables such as n on the stack. The stack is part of the process's address space, so each process has its own n.
03What does the program counter (PC) hold?
The PC, also called the instruction pointer, says what runs next. Save it and you know exactly where to carry on later.
04The kernel pauses process 42 to run process 43. What does it do with 42's registers?
The PCB keeps the saved registers while the process waits. Loading them back puts the PC at line 3 again, so 42 carries on as if nothing happened.
05Process 43 runs the same program and sets its n to 0. What happens to process 42's n, which is 1?
Both processes use the name n, but each n lives in a different address space. The page-table register tells the CPU which address space is in use.
Glossary
- Process
- A program while it runs: its own address space, its registers and the kernel's record of it, the PCB.
- Address space
- The memory a process may use. It holds the code, global data, the heap and the stack.
- Stack
- The part of the address space for local variables, function arguments and return addresses. It grows and shrinks as functions are called and return.
- Heap
- The part of the address space for memory a program asks for while it runs, for example with malloc() in C.
- Register
- A tiny, very fast store inside the CPU. The program counter, the stack pointer and the page-table register are three special ones.
- Program counter (PC)
- The register that says which instruction runs next. Also called the instruction pointer.
- Process control block (PCB)
- The kernel's record of one process: its PID, state, saved registers, page table and open files. xv6 calls it struct proc.
- Process ID (PID)
- The number the kernel gives each process. A process can ask for its own with getpid().
Notes and sources
- The PIDs 42 and 43 are examples: the kernel hands out whatever number is free, and getpid() tells a process its own.
- 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.
- 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.
- Modern kernels load code lazily, page by page as it is first used, and two processes running the same file can share the same physical copy of the read-only code. Each still sees the code in its own address space.
- xv6 names the states UNUSED, USED, SLEEPING, RUNNABLE, RUNNING and ZOMBIE; in /proc/PID/status Linux shows letters such as R (running) and S (sleeping). The lesson "Process states: ready, running and blocked" covers them.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 4: The Abstraction: The Process
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 13: The Abstraction: Address Spaces
- Berkeley CS162, Lecture 2: Four Fundamental OS Concepts (threads, address spaces, processes)
- MIT 6.1810: xv6 kernel/proc.h (struct proc, the xv6 PCB)
- MIT 6.1810 (2024), lecture notes: Virtual memory (satp and per-process page tables)
- Linux kernel documentation: The /proc filesystem (/proc/PID/maps and status)
- Linux kernel documentation: Page Table Isolation (CR3 and page tables on x86)
- Linux man-pages: getpid(2)
Quiz Unlocks after the last step.