Unit 12.3A · Term 3

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

Lesson Presentation

12.3A-paging-segmentation.pdf · Slides for classroom use

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

Remember

Define: page, frame, segment, page table, segment table.

Understand

Explain the difference between internal fragmentation and external fragmentation, giving an example of each.

Apply

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?

Analyze

Compare paging and segmentation across 5 features. Which approach is better for sharing a library of code between programs? Justify your answer.

Self-Check Quiz

1. What is the difference between a page and a frame?
Click to reveal: A page is a fixed-size block of the program's logical memory. A frame is a fixed-size block of physical RAM. Pages are mapped into frames.
2. Which method suffers from external fragmentation?
Click to reveal: Segmentation — because variable-size segments leave gaps in memory when they are removed.
3. How does a page table work?
Click to reveal: It maps each page number to a frame number in physical RAM, allowing the OS to find where each page is stored.
4. What is a segment table?
Click to reveal: It maps each segment to its base address (starting position) and limit (size) in physical RAM.
5. Why is segmentation better for protection?
Click to reveal: Because segments correspond to logical units (code, data, stack), access permissions can be set per segment — e.g., code is read-only, data is read-write.