Address spaces
Two programs, A and B, are running. Each one sees its own private memory, numbered from address 0 upward. This is its virtual address space: the addresses are names the program uses, not real places in RAM.
Virtual memory is cut into pages of 4 KiB (4,096 bytes). Real RAM is cut into frames of the same size. Any page can sit in any frame, or wait on disk. Here A and B have three pages each; RAM has five frames.
Translation
A reads virtual address 0x1234. The MMU (memory management unit, part of the processor) splits it in two. The low 12 bits, 0x234, are the offset inside a page, since 2¹² = 4,096. The rest, 1, is the page number.
The MMU looks up page 1 in A's page table, a list the operating system keeps in RAM. Page 1 is in frame 3. The offset stays the same, so the real, physical address is 0x3234.
Now B reads the same address, 0x1234. B has its own page table, and there page 1 is in frame 2. So B reaches 0x2234. Same virtual address, different real memory: neither program touches the other's data.
Each table lookup costs an extra trip to RAM. So the MMU saves recent translations in its TLB (translation lookaside buffer), a tiny, fast cache. A reads page 1 again: a TLB hit, so no table lookup at all.
Faults and protection
A reads 0x2010, on page 2. The TLB misses, and A's table says page 2 is not in RAM: it is on disk. The MMU raises a page fault: the processor pauses A and hands control to the operating system.
The operating system copies page 2 from disk into free frame 4, writes frame 4 into A's table, then runs A's read again. Now it works: 0x4010. A never notices, except that the disk made it wait.
Each table entry also has protection bits: may the program read this page, or write it? B's page 0 holds its code and is read-only. B tries to write there: the MMU refuses, and the operating system stops B.
A's table points only at A's frames, and B's only at B's, so neither can reach the other's memory. And six pages share five frames: pages not in use wait on disk, so programs can use more memory than RAM has.
Numbers and code
;; step 1/10 · own-space ;; about: Program A, Program B virtual address: the address a program uses in its own code physical address: a real place in RAM A reads 0x1234 (its own 0x1234) B reads 0x1234 (its own 0x1234) 0x means hexadecimal (base 16): 0x1234 = 4,660
Each process has its own virtual address space and its own page table; memory is managed in fixed-size pages (virtual) and frames (physical), and the page offset is copied unchanged while the page number is translated (Patterson and Hennessy, Computer Organization and Design, section 5.7; Arpaci-Dusseau, Operating Systems: Three Easy Pieces, chapter 18). A TLB is a small cache of recent translations. Typical values: 16 to 512 entries, a hit in 0.5 to 1 clock cycle, a miss costing 10 to 100 cycles, and a miss rate of 0.01% to 1% (Patterson and Hennessy, section 5.7).