Virtual Memory & Paging
01The address translation mechanism
When a process accesses memory at virtual address V, the CPU splits V into a page number and an offset within the page. The page number indexes into the process's page table to find which physical frame (block of RAM) holds that page. The offset within the page is unchanged. The Memory Management Unit (MMU) performs this translation in hardware on every memory access. The TLB (Translation Lookaside Buffer) caches recent translations so most lookups take just one cycle instead of a full page table walk.
Virtual Address: [ page number (20 bits) | offset (12 bits) ]
↓ page table lookup
Physical Address: [ frame number (20 bits) | offset (12 bits) ]02Process isolation via separate page tables
Each process has its own page table, stored in kernel memory. Virtual address 0x400000 in process A maps to physical frame 2000, while the same virtual address in process B maps to frame 5000. They cannot access each other's memory because their page tables point to entirely different physical frames. The OS switches the page table base register (CR3 on x86) during a context switch. This is the hardware enforcement behind process isolation.
03Page faults and demand paging
When a process accesses a page whose page table entry is marked "not present," the CPU raises a page fault exception. The OS handles it: if the page exists on disk (in the swap partition or a memory-mapped file), the OS finds a free physical frame, reads the page from disk into that frame, updates the page table, and resumes the process. Demand paging means the OS doesn't load a program's entire address space at startup — only pages actually accessed are ever loaded into RAM.
04Page replacement algorithms
When RAM is full and a page fault occurs, the OS must evict an existing page to make room. The optimal algorithm (OPT) evicts the page that will be accessed furthest in the future — impossible to implement online but useful as a benchmark. LRU (Least Recently Used) evicts the page not accessed for the longest time. In practice, OSes approximate LRU using the hardware-set "accessed" bit in page table entries — the clock algorithm or its variants scan pages in a circular buffer and evict the first one whose accessed bit is clear.