7.4 Compaction & Paging Architecture (Pages, Frames & Page Tables)
💡 Core Intuition
🍳 The Everyday Analogy: The Loose-Leaf Binder vs The Bound Journal
Imagine writing a comprehensive textbook across months of work:
The Textbook Binding Evolution Pipeline
From continuous bound pages to arbitrary loose-leaf distribution
The Bound Leather Journal
You must reserve Chapter 3 as 50 consecutive printed pages.
Tedious Page Shuffling
Physically re-copying every chapter to unite all blank sheets at the back.
Loose-Leaf Ring Binder & Index
Every sheet of paper is an identical standardized size. Pages can be placed in any ring slot.
- Compaction: A desperate attempt to salvage contiguous allocation by physically shifting data in RAM, burning massive CPU cycles.
- Paging: The foundational breakthrough of modern operating systems. By separating the logical view of contiguous memory from the physical reality of scattered placement, external fragmentation is eliminated.
💻 Bridging to Computer Science
Contiguous memory allocation imposes an artificial and costly restriction: an entire program must occupy consecutive physical memory addresses. When free RAM becomes fragmented, the OS is faced with a dilemma:
📚 Core Deep-Dive & Concepts
1. The Anatomy of Paging: Pages vs Frames
Paging breaks physical and logical memory into standardized, uniform blocks:
2. The Paging Architecture & Mapping Engine
When the CPU executes instructions, it generates a Logical Address split into two components:
- Page Number (): Serves as an index into the process's page table.
- Page Offset (): The displacement within the page, identifying the specific byte.
Hardware MMU Paging Address Translation Blueprint
Decomposing logical addresses into page index and concatenating physical frame offsets
Issue Logical Address (p, d)
Index Page Table via PTBR + (p × Entry Size)
Return Physical Frame Number (f)
Synthesize Physical Address (f, d) & Access DRAM
Step-by-Step Translation Mechanics
- Address Decomposition: The CPU issues a logical address. The hardware splits the bitstring into high-order bits () and low-order bits ().
- Page Table Indexing: The hardware uses the Page Table Base Register (PTBR) to locate the process's page table in RAM. It computes to read entry .
- Frame Resolution: From page table entry , the MMU extracts the physical Frame Number ().
- Physical Address Synthesis: The frame number replaces , while the offset is concatenated directly without modification:
- DRAM Access: The memory controller reads/writes the physical DRAM cell at the computed address.
3. The Page Table: Operating System Data Structure
A fundamental principle: The Page Table is a data structure, NOT a piece of hardware.
- Location: Page tables are maintained in Main Memory (RAM) by the OS kernel.
- Per-Process Basis: Every process running in the system possesses its own dedicated page table.
- Kernel Management: During a context switch, the operating system reloads the Page Table Base Register (PTBR) with the physical address of the newly scheduled process's page table.
Anatomy of a Page Table Entry (PTE)
A Page Table Entry does not merely store the Frame Number; it contains vital hardware status bits:
| Bitfield Component | Bit Width | Purpose & Operational Hardware Semantic |
|---|---|---|
| Valid / Invalid () | 1 = Resident in physical DRAM; 0 = Not in RAM (raises Page Fault trap). | |
| Dirty / Modified () | Hardware sets to 1 on write. If dirty, kernel must flush to disk on eviction. | |
| Referenced () | Hardware sets to 1 on read/write. Evaluated by Clock/LRU eviction heuristics. | |
| Protection Bits | Enforces Read, Write, and Execute (NX / DEP) access permissions. | |
| Cache Disable / PWT | Bypasses CPU L1/L2 caches for memory-mapped hardware I/O registers. | |
| Frame Number () | Base physical address of the DRAM frame backing this virtual page. |
- Valid/Invalid Bit ():
1(Valid): The page is legally part of the process's address space and currently resides in physical RAM.0(Invalid): The page is either unmapped or currently swapped out on disk. Referencing an invalid page triggers a Page Fault trap.
- Dirty / Modified Bit ():
- Set to
1by hardware whenever the CPU writes to any byte in that frame. - When the page is evicted, if , the kernel must write its contents back to disk; if , the frame can be silently overwritten without disk I/O.
- Set to
- Referenced / Access Bit ():
- Set to
1by hardware whenever the page is read or written. Used by page replacement algorithms (such as Second-Chance and Clock) to approximate LRU.
- Set to
- Protection Bits:
- Encodes access permissions:
Read-Only,Read-Write, orExecute-Disable (NX / DEP). Prevents malicious execution of injected code on the stack.
- Encodes access permissions:
4. Trade-Offs of Pure Paging
🏭 In The Real World: Production Case Study
Linux Page Allocation: Zero-Page Optimization & Shared Libraries
Modern operating systems leverage paging to achieve massive performance gains when launching programs:
- Shared Read-Only Code (
libc.so):- If 500 processes run on a server, each linking the standard C runtime (
libc.so), the OS does not load 500 copies into RAM. - Paging maps the identical physical frame (Frame 500) into all 500 process page tables, saving gigabytes of physical DRAM.
- If 500 processes run on a server, each linking the standard C runtime (
- The Zero-Page Optimization:
- When a process allocates a large uninitialized memory buffer (
callocor anonymousmmap), the kernel points all newly created virtual pages to a single global read-only physical frame called the Zero Page. - Physical RAM is not consumed until the process writes to a specific page, at which point the MMU triggers a Copy-on-Write (COW) fault to allocate a fresh private frame.
- When a process allocates a large uninitialized memory buffer (
🎯 Exam & Interview Pitfall Check
Question 1: Why is the page size in paging systems invariably chosen as a power of two ( bytes)? Answer:
- Zero-Latency Hardware Splitting: If the page size is a power of two ( bytes), the lower bits of the CPU-generated logical address automatically represent the page offset (), and the remaining higher bits represent the page number ().
- No Hardware Division or Modulo: If the page size were not a power of two (e.g. bytes), the MMU hardware would have to perform integer division () to compute the page number and modulo () to compute the offset. Division and modulo circuits are slow, bulky, and consume significant power.
- With powers of two, address splitting is performed purely by bit-masking and wire routing at the hardware clock cycle level, taking effectively .
Question 2: If a process of size is loaded into a paging system with page size , calculate:
- The total number of pages allocated to the process.
- The internal fragmentation generated.
- What if the process size were ?
Answer:
- For Process:
- Number of pages pages.
- Total allocated memory .
- Internal Fragmentation (Exact multiple).
- For Process:
- Number of pages .
- Total allocated memory .
- Internal Fragmentation .
- The "Page Table is Inside the CPU" Fallacy: The page table is NOT a register inside the CPU! Page tables can contain millions of entries and reside in Main Memory (DRAM). The CPU contains only the Page Table Base Register (PTBR) pointing to the page table's starting address in RAM.
- Confusing Page Offset with Frame Offset: In paging, Page Offset is identically equal to Frame Offset (). Because page size equals frame size, the relative byte offset within the page is identical to the relative byte offset within the physical frame.
- The "Two Memory Accesses" Trap: Never forget that pure paging doubles memory latency: for every single instruction or variable reference, the CPU must read the page table in RAM first, and then read the actual instruction/data in RAM second.