The grand illusion behind process isolation
Three lessons in this course have leaned on a promise: each process gets private memory and cannot touch anyone else's (lessons 1-3, 3-1, 4-1). This lesson shows the machinery that keeps the promise, because you will meet its symptoms directly: a machine that crawls to a halt when RAM runs out, the word segfault in a crash log, and memory dashboards that show "virtual" sizes far bigger than the RAM installed.
The trick is called virtual memory. A process never sees real RAM addresses. Every address in a program — every variable, every stack frame, every heap object — is a virtual address: a made-up number in the process's own private numbering that starts at zero. The OS keeps a page table per process: a map from the process's virtual addresses to the physical RAM locations actually holding the data. The CPU translates every single memory access through that map, in hardware, automatically.
To keep the map small, memory is managed in fixed-size chunks called pages (typically 4 KB). The page table says "virtual page 0 lives in physical frame 7, virtual page 1 lives in physical frame 2", and so on. Two consequences fall out immediately:
- Isolation for free: process A's page table simply contains no entry pointing at process B's frames. A cannot even name B's memory. That is the enforcement mechanism behind everything unit 3 promised.
- Two processes can use the same virtual address for different data, which is why every process can believe it owns a clean, empty address space starting at zero.
Code exercise · python
Run this toy page table. Virtual addresses are split into a page number (which 100-byte chunk) and an offset (position inside the chunk); the table maps each page to where that chunk really sits in physical RAM. Real hardware does exactly this split, just with 4096-byte pages and in silicon.
Page faults and swapping: when the map has a hole
What if a process touches a virtual page with no entry in the table? The hardware stops mid-instruction and hands control to the kernel. That event is a page fault, and the kernel decides what it means:
- Legal but not loaded: the page exists but its data is currently on disk. The kernel loads it into a free frame, updates the table, and resumes the process as if nothing happened. The process never notices, beyond the time it took.
- Illegal: the process had no right to that address. The kernel kills it — this is the segmentation fault from lesson 4-2, finally with its full mechanism visible: a page-table lookup that came up empty-handed.
The "on disk" case enables swapping: when RAM fills up, the kernel evicts pages that have not been used recently to a reserved area of disk (swap space), freeing frames for whoever needs them now. This is why a machine that runs out of RAM does not instantly die but instead crawls: pages keep faulting back and forth between RAM and a disk that is a thousand times slower (lesson 1-1's ratios, now weaponized). Engineers call the worst version thrashing — the machine spends all its time moving pages and none doing work. When a dashboard shows a process's virtual size far above physical RAM, virtual memory is what makes that legal.
Code exercise · python
Your turn: teach the toy translator about page faults. If the page is missing from the table, return the string "page fault: page N is not in RAM" (with the page number) instead of crashing with a KeyError. Match the expected output exactly.
Quiz
Processes A and B both store a variable "at address 5000" and nothing collides. How?
Problem
When RAM is full, the kernel moves not-recently-used pages out to a reserved area of disk to free up frames. What is this mechanism called? (one word)