Paging & Segmentation
When multiple programs run simultaneously, the OS must manage how memory is allocated to each program. Paging and segmentation are two methods of organizing memory. Both allow programs to use more memory than physically available (virtual memory) and prevent programs from interfering with each other.
Learning Objectives
- 12.3.4.2 Explain the principle of memory organization by segment and page
Conceptual Anchor
The Storage Unit Analogy
Paging is like renting storage lockers that are all the same size — you split your belongings into equal boxes regardless of what they contain. Segmentation is like renting rooms of different sizes based on what you're storing — a big room for furniture, a small room for books, a medium room for clothes.
Paging
Paging divides both physical memory (RAM) and logical memory (program) into fixed-size blocks. Physical blocks are called frames; program blocks are called pages. Each page maps to a frame.
| Term | Definition |
|---|---|
| Page | A fixed-size block of a program's logical memory |
| Frame | A fixed-size block of physical RAM (same size as a page) |
| Page table | A mapping table that records which page is in which frame |
| Page size | Typically 4 KB (but can vary) |
✓ How Paging Works
Program (10 KB) split into pages: Physical RAM frames:
┌────────────┐ ┌────────────┐ Frame 0
│ Page 0 │ 4 KB ──────────────────→ │ Page 0 │
├────────────┤ ├────────────┤ Frame 1
│ Page 1 │ 4 KB ──────┐ │ Page 2 │ ← from another program
├────────────┤ │ ├────────────┤ Frame 2
│ Page 2 │ 2 KB ──┐ │ │ Page 1 │ ← Pages don't need
└────────────┘ │ │ ├────────────┤ Frame 3 contiguous frames!
└───│──────────→ │ Page 2 │
└──────────→ │ │
└────────────┘
Page Table:
┌────────┬─────────┐
│ Page # │ Frame # │
├────────┼─────────┤
│ 0 │ 0 │
│ 1 │ 2 │
│ 2 │ 3 │
└────────┴─────────┘Advantages & Disadvantages of Paging
| ✅ Advantages | ❌ Disadvantages |
|---|---|
| No external fragmentation — fixed-size blocks fit exactly | Internal fragmentation — last page may not be full (wasted space) |
| Pages can be stored in non-contiguous frames | Page table overhead — requires extra memory |
| Efficient memory utilization | Page faults cause slow disk access (swapping) |
| Easy to implement and manage | No logical grouping — code and data may be split across pages |
Segmentation
Segmentation divides a program into variable-size logical units called segments. Each segment represents a meaningful part of the program (e.g., code segment, data segment, stack segment).
| Term | Definition |
|---|---|
| Segment | A variable-size block corresponding to a logical unit of the program |
| Segment table | Maps each segment to its base address and size (limit) in RAM |
| Base address | Starting physical address of the segment in RAM |
| Limit | Length/size of the segment |
✓ How Segmentation Works
Program divided into logical segments:
┌──────────────────┐
│ Code Segment │ 6 KB
├──────────────────┤
│ Data Segment │ 3 KB
├──────────────────┤
│ Stack Segment │ 2 KB
└──────────────────┘
Segment Table:
┌─────────┬──────────────┬─────────┐
│ Segment │ Base Address │ Limit │
├─────────┼──────────────┼─────────┤
│ Code │ 0x1000 │ 6 KB │
│ Data │ 0x3000 │ 3 KB │
│ Stack │ 0x5000 │ 2 KB │
└─────────┴──────────────┴─────────┘
Each segment is placed in RAM at its base address.
Segments can be different sizes — each holds a complete logical unit.Advantages & Disadvantages of Segmentation
| ✅ Advantages | ❌ Disadvantages |
|---|---|
| No internal fragmentation — segments fit exactly | External fragmentation — gaps form as segments are loaded/removed |
| Logical grouping — code, data, stack are separate | More complex memory management |
| Easy to share/protect individual segments | Variable sizes make allocation harder |
| Segments can grow dynamically | Compaction may be needed to reclaim fragmented space |
Paging vs Segmentation
| Feature | Paging | Segmentation |
|---|---|---|
| Block size | Fixed (all pages same size) | Variable (segments differ) |
| Division basis | Physical — arbitrary blocks | Logical — meaningful program parts |
| Internal fragmentation | Yes (last page may be partly empty) | No |
| External fragmentation | No | Yes (gaps between segments) |
| Mapping table | Page table (page → frame) | Segment table (base + limit) |
| Sharing | Harder (pages don't align to logical units) | Easier (share a whole segment) |
| Protection | Per-page (less meaningful) | Per-segment (code, data, stack) |
Pitfalls & Common Errors
Mixing Up Internal and External Fragmentation
Internal = wasted space INSIDE a block (paging — last page partly empty). External = wasted gaps BETWEEN blocks (segmentation — holes in memory). Get these right — they are commonly tested.
Saying "Paging Has No Fragmentation"
Paging eliminates external fragmentation but still has internal fragmentation. Always specify which type.
Pro-Tips for Exams
Memory Management Questions
- For "explain" questions — define the method, describe how it divides memory, mention the mapping table
- For "compare" questions — use the comparison table headings as your structure
- Always distinguish between internal and external fragmentation
- Know that modern OSes often use both — segmented paging (segments divided into pages)
Graded Tasks
Define: page, frame, segment, page table, segment table.
Explain the difference between internal fragmentation and external fragmentation, giving an example of each.
A program is 18 KB and the page size is 4 KB. How many pages will it be divided into? Will there be internal fragmentation? How much?
Compare paging and segmentation across 5 features. Which approach is better for sharing a library of code between programs? Justify your answer.