OS Notes
Complete guide to paging in memory management — page tables, page number and offset, address translation, TLB, multi-level page tables, and internal fragmentation.
Introduction
Imagine a large book that does not fit on a single shelf. Instead of finding one contiguous shelf space, you could split the book into chapters and place each chapter wherever there is an empty spot. You maintain an index card that records which chapter is on which shelf. When someone wants to read the book, they check the index to find each chapter. This is exactly how paging works.
Paging is a memory management scheme that eliminates the need for contiguous memory allocation. It divides both physical memory and logical memory into fixed-size blocks. Physical memory blocks are called frames; logical memory blocks are called pages. When a process needs memory, the OS finds free frames anywhere in memory and maps the process pages to them through a page table.
How Paging Works
Basic Concept
| Logical Address Space (Process view) | Physical Memory (RAM): |
| │ Page 2 │──┐ │ └───────────────── | │ Page 0 │ ← |
| │ Page 3 │ │ └───────────────────────── | │ Page 1 │ ← |
| └───────────────────────────── | ├────────┤ Frame 5 |
Address Translation
A logical address in a paging system has two parts:
| Logical Address | | Page Number (p) | Offset (d) | |
| - Page offset | n bits (position within a page) |
| - Page number | m - n bits (which page) |
| Example | 16-bit address, page size = 4KB (2^12) |
| Address | 0011 | 0101 0110 1010 |
| Page Table lookup: Page 3 | Frame 6 |
| Physical Address | Frame 6 base + Offset 1386 |
Page Table
Each process has its own page table mapping its logical pages to physical frames:
| Page# | Frame# | Valid/Invalid |
|---|---|---|
| 0 | 2 | Valid |
| 1 | 3 | Valid |
| 2 | 1 | Valid |
| 3 | 6 | Valid |
Address Translation Hardware
Translation Lookaside Buffer (TLB)
Every memory access requires a page table lookup — but the page table itself is in memory. This means every memory access requires TWO memory accesses (one for the table, one for the data). This is unacceptable.
The solution is the TLB — a small, fast hardware cache that stores recent page-to-frame mappings.
| Use TLB entry | Check Page | |
|---|---|---|
| (no memory | Table in RAM | |
| access) | (slow, 100+ |
Effective Access Time (EAT)
| Example | TLB hit ratio = 98%, TLB lookup = 10ns, Memory access = 100ns |
| Without TLB | 200 ns (two memory accesses every time) |
| With TLB | 112 ns (44% improvement!) |
Multi-Level Page Tables
For a 64-bit address space with 4KB pages, a single-level page table would need 2^52 entries — impossibly large. Multi-level page tables solve this by only allocating table space for pages actually in use.
| Address | | 10 bits | 10 bits | 12 bits | |
| │ Table │──── | │Inner Page│ ┌───────────┐ |
| │(1024 ent)│ │ Table │──── | │ Physical │ |
Internal Fragmentation
Paging eliminates external fragmentation (no need for contiguous memory) but introduces internal fragmentation. Since pages are fixed-size, the last page of a process may not be completely filled.
| Pages needed | ceil(72766 / 4096) = 18 pages |
| Memory allocated | 18 × 4096 = 73,728 bytes |
| Internal fragmentation | 73,728 - 72,766 = 962 bytes wasted |
Smaller page sizes reduce internal fragmentation but require larger page tables (more entries). Typical modern page size: 4 KB (balance between table size and fragmentation).
Page Table Entry Structure
Each entry in the page table contains more than just the frame number:
| Valid | Dirty | Referenced | Protection | Cache | Frame Number |
|---|---|---|---|---|---|
| Bit | Bit | Bit | Bits | Control |
- Valid bit: Is this page currently in memory?
- Dirty bit: Has this page been modified since loading? (Needed for page replacement)
- Referenced bit: Has this page been accessed recently? (Used by replacement algorithms)
- Protection bits: Read/Write/Execute permissions
Real-World Analogy
Paging is like a library with numbered shelves (frames). When a new book (process) arrives, the librarian does not need a single long empty shelf. They split the book into chapters (pages) and put each chapter on any available shelf. The card catalog (page table) records where each chapter is. To read chapter 5, you check the catalog: "Chapter 5 is on Shelf 23." The shelf number is the frame, and the position within the shelf is the offset.
Key Takeaways
- Paging divides memory into fixed-size pages (logical) and frames (physical)
- Eliminates external fragmentation by allowing non-contiguous allocation
- Page table maps logical pages to physical frames for each process
- TLB caches recent translations to avoid double memory access penalty
- Multi-level page tables save space by only allocating tables for used regions
- Internal fragmentation averages half a page per process (typically ~2 KB)
- Every memory access goes through address translation (page number → frame number + offset)
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Paging.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, memory, management, paging
Related Operating Systems Topics