Paging and virtual memory
A virtual address is a name whose physical location can change. Paging gives the kernel fixed-size units for protection, sharing and replacement, while the TLB keeps common translations close to the CPU. We will compute Finch’s translation, table size, page faults, replacement traces, thrashing and segmentation.
#Section 26Memory management goals
If programs used physical addresses directly, one process could overwrite another and relocation would be difficult. A virtual address belongs to a process’s address-space view. A physical address names a location in RAM. The memory manager maps one to the other and checks permissions before the hardware performs the access.
The goals are isolation, relocation, protection, allocation and sharing. A private page can be mapped to different frames in different processes. A read-only code page can be shared. A missing page can be brought in only when used. These choices let the 32-bit model expose 1,048,576 virtual pages without requiring every page to occupy RAM at once.
Contiguous allocation makes placement simple but produces holes. Paging removes external fragmentation from physical placement, though the final page can still contain unused bytes.
Contiguous allocation and fragmentation
Fixed partitions reserve predefined regions; variable partitions find a free hole large enough for each request. First fit takes an adequate hole encountered early, best fit selects a small adequate hole, and worst fit selects a large one. Searching and splitting have costs, and none eliminates the need to coalesce adjacent free regions when allocations end.
Internal fragmentation wastes space within an allocation; external fragmentation separates usable holes. Paging allows discontiguous physical frames but does not remove unused bytes inside the final page.
#Section 27Paging and address translation
With 4,096-byte pages, a 32-bit virtual address has 12 offset bits and 20 virtual-page-number bits. Finch’s address 13,396 gives VPN 3 and offset 1,108. The page table maps VPN 3 to frame 9. The physical address is frame times page size plus offset, or .
The offset does not change during translation. The page table entry adds the frame and permission state. A present bit says whether the frame is resident; protection bits say which access is allowed. Dirty and referenced bits help writeback and replacement decisions.
A page is the fixed-size unit in virtual memory, while a frame is the corresponding fixed-size physical unit. Compute the split explicitly: . The quotient selects the virtual page and the remainder selects a byte within it. Translating to frame 9 changes only that quotient, giving before adding the unchanged remainder. Read both equations as base-plus-offset calculations.
Check permission and residency separately. A present read-only frame can reject a write, while a valid permitted mapping can fault because its backing page is absent.
#Section 28Page tables and TLBs
A linear table needs one 4-byte entry for each of 1,048,576 virtual pages, which is 4,194,304 bytes per address space. Multi-level tables allocate lower branches only for populated regions. Inverted designs index by physical frame and store which virtual page occupies it, changing lookup trade-offs.
A translation lookaside buffer, or TLB, caches recent virtual-to-physical translations. With a 90 percent hit rate, a hit costs 10 ns of TLB lookup plus 100 ns of memory access. A miss costs 10 ns plus two 100 ns memory accesses. The weighted effective time is 120 ns.
Read this as hit probability h times hit cost plus miss probability times miss cost. E is the expected access time, t the 10 ns TLB lookup and m the 100 ns memory access. The example assumes one page-table memory lookup on a miss and excludes page faults.
Context switches can flush or partition TLB state. ASIDs or PCIDs let supported hardware retain entries while tagging ownership.
Multi-level tables save space for sparse mappings by omitting lower branches. A translation miss then needs more navigation. Inverted tables instead organize around physical frames and require another lookup strategy.
Virtual memory and translation tags
Private views, protection and shared mappings are the purpose of virtual memory; backing pages with storage is only one mechanism. Translation tags distinguish entries from different address spaces so a switch can retain correctly identified cached translations. A retained TLB entry must still follow mapping changes and invalidation rules. A larger hit rate cannot repair an outdated entry that grants the wrong permission.
#Section 29Demand paging and faults
Demand paging leaves a valid virtual mapping without a resident frame until the CPU touches it. A page fault transfers control to the kernel. The kernel checks whether the address is legal, locates file-backed bytes or creates a zero page, obtains a frame, updates the PTE and retries the faulting instruction.
A minor fault can be satisfied without storage I/O, while a major fault needs slower backing storage. A dirty evicted page must be written before its frame can be reused. The process is blocked during the wait, so page faults affect both memory latency and scheduler state.
Classify the permitted backing first. Demand-zero and already-cached file pages can avoid storage I/O; a major backing read may block the faulting task.
After establishing the mapping, retry the faulting instruction. An address outside the allowed region cannot be repaired merely by catching its failure signal.
#Section 30Page replacement
With three frames and reference string 1,2,3,4,1,2,5,1,2,3,4,5, FIFO produces 9 faults. With four frames it produces 10, the exact Belady anomaly in the supplied trace. LRU with three frames produces 10, while OPT produces 7 because it knows future references and is a benchmark rather than a deployable predictor.
FIFO evicts the oldest resident page, LRU the least recently used, and Clock approximates recency with a referenced bit. Replacement must also consider dirty state, sharing and working-set locality.
The first references 1, 2 and 3 fill the three frames. Reference 4 evicts the oldest loaded page, 1. References 1 and 2 then fault because FIFO’s earlier decisions removed them. After loading 5, the next references to 1 and 2 hit. The remaining references distinguish the policies because FIFO remembers admission order, LRU remembers recent use and OPT compares the next future use.
FIFO, optimal, LRU and Clock
Clock grants referenced frames a second chance by clearing their bit. Reclaiming a candidate still respects dirty and ownership rules. It approximates recency without a fully ordered access history.
#Section 31Thrashing and segmentation
When a task’s working set does not fit, it faults, loads a page, evicts a useful page and faults again. Thrashing means the system spends most of its time paging instead of executing. Locality, working-set size and page-fault frequency provide signals for reducing active work or adding frames.
Segmentation names logical regions such as code, heap and stack. Paging beneath segmentation supplies fixed physical placement and avoids external holes. A segment plus offset still needs bounds and permissions, while a page mapping handles frame placement.
Working sets and page-fault frequency
A working set depends on an observation interval. Admitting more tasks can worsen repeated eviction when their combined active pages exceed available frames. Distinguish that pressure from compulsory first access.
Segmentation and paging
Segmentation adds logical region identity and bounds; paging can allocate its pieces independently. Logical organization and physical placement differ, and neither permits out-of-bounds pointers.