Segmentation
Segmentation lets a program be divided by meaning: code, data, stack, and heap. Each segment has a base and a limit, and the hardware checks every access.
In plain English
Segmentation cuts memory into logical pieces, like chapters in a book.
By the end you will be able to
- Explain a segment table and the base/limit pair.
- Translate a logical segment:offset address.
- Know how segmentation supports protection and sharing.
- Explain why segmentation still fragments memory externally.
Logical segments
A segment is a variable-length logical unit such as code, data, stack, or heap. The programmer or compiler sees a segment number and an offset. The hardware uses a segment table to find the segment's base address and limit. The offset must be less than the limit; otherwise the CPU raises a trap. The physical address is base + offset.
segment_table[code] = { base: 0x4000, limit: 0x1200 };
segment_table[data] = { base: 0x6000, limit: 0x0800 };
segment_table[stack] = { base: 0x9000, limit: 0x1000 };
logical address (1, 0x0100) -> data segment
check 0x0100 < 0x0800
physical = 0x6000 + 0x0100 = 0x6100Protection and sharing
A segment can carry permission bits: read, write, execute. The hardware checks them on every access, so code can be shared read-only between processes while data stays private. Segmentation matches the programmer's mental model better than a flat address space, which is why some architectures still expose segments.
The fragmentation problem
Because segments are variable-length, allocating and freeing them creates external fragmentation. The OS may compact memory or use a more sophisticated allocator. Paging solves this by using fixed-size units, at the cost of a page table and a different mental model.
Watch it happen
Step through the worked example. Play it, pause it, or move one step at a time.
step-by-step
Step 1 of 4
Logical address
The program refers to segment 1 (data) with offset 0x100.
Key takeaways
- A segment is a variable-length logical unit.
- The segment table stores a base and a limit.
- The hardware checks offset < limit before adding the base.
- Segments support protection and sharing.
- Variable-length segments cause external fragmentation.
Common pitfalls
- Adding the base before checking the limit.
- Treating a segment offset as an absolute address.
- Assuming segmentation eliminates external fragmentation.
Kernel notes
- Translate (segment, offset) to a physical address.
- Explain the limit check.
- Compare segmentation with paging.
Quick check
1. What does the segment limit check prevent?
2. What kind of fragmentation does segmentation suffer from?