Segmentation: Segment Table & Address Translation
Segmentation is a memory‐management technique that divides a program’s logical address space into variable‑sized segments, each representing a logical unit—such as a function, data array, or stack. Unlike paging, which treats all blocks uniformly, segmentation preserves the program’s structure and supports protection and sharing at a finer granularity.
1. What Is a Segment?
- A segment is a contiguous block of program code or data with a logical meaning (e.g., main function, global variables, heap, stack).
- Each segment has its own length and base address in physical memory.
- Programs view memory as a collection of segments rather than a flat array of bytes.
2. The Segment Table
Each process has an associated segment table, which records where each of its segments resides in physical memory:
Field | Description |
---|---|
Segment # | The index or name identifying each segment (e.g., code, data, stack) |
Base | The starting physical address where this segment is loaded |
Limit (Length) | The size (in bytes or words) of the segment |
Access Rights | Permissions (read, write, execute) for the segment |
When a process generates a logical address, it specifies two parts:
- Segment Number (s) – which segment it wants
- Offset (d) – the distance from the segment’s start
3. Logical‑to‑Physical Address Translation
To access a memory location, the CPU and MMU perform these steps:
- Fetch segment number and offset from the instruction’s logical address.
- Check bounds: Verify that
0 ≤ d < Limit[s]
.-
If
d
exceeds the segment’s limit, the OS raises a segmentation fault.
-
- Compute physical address:
PhysicalAddress=Base[s]+dPhysicalAddress = Base[s] + d
- Access memory at the computed physical address.
4. Example Walk‑Through
Imagine a process with three segments:
Segment # | Base | Limit | Description |
---|---|---|---|
0 | 4,000 | 1,000 | Code segment |
1 | 8,000 | 500 | Data segment |
2 | 9,000 | 200 | Stack segment |
- Logical address (1, 200) refers to segment 1, offset 200:
- Check 200 < 500 → OK
- Physical = 8,000 + 200 = 8,200
- Logical address (2, 250) → 250 ≥ 200 → Segmentation fault
5. Advantages & Drawbacks
Aspect | Advantage | Drawback |
---|---|---|
Logical Structuring | Maps to program modules (easier to understand/share) | Requires careful segment management |
Protection & Sharing | Can set per‑segment permissions | Fragmentation: unused “holes” between segments |
Variable Sizes | No internal fragmentation within segments | External fragmentation across the address space |
Why Segmentation Matters
- Reflects program structure: Easier debugging, sharing code/data segments.
- Fine‑grained protection: Different rights per segment (e.g., code read-only).
- Combines with paging in some systems (purely segmented or paged‑segmented).
Understanding segmentation lays the groundwork for advanced memory schemes—such as paged segmentation and modern GPU/OS hybrid models.