OS Notes
Understanding segmentation memory management — logical segments, segment table, address translation, comparison with paging, and segmentation with paging (hybrid approach).
Introduction
When you write a program, you think in terms of logical units — there is the main code, there are functions, there are global variables, there is the stack, and there is dynamically allocated memory. You do not think of your program as a flat sequence of bytes. Segmentation is a memory management scheme that reflects this programmer's view of memory.
Unlike paging (which divides memory into fixed-size pages without regard to program structure), segmentation divides a program into logical segments of varying sizes. Each segment corresponds to a logical unit: one segment for the code, one for global data, one for the stack, one for each function's local data. This makes protection and sharing more natural — you can share a code segment between processes without sharing their data.
Segments in a Typical Program
| │ Main Code Segment │ Segment 0 (size | 2400 bytes) |
| │ Function Library │ Segment 1 (size | 1500 bytes) |
| │ Global Variables │ Segment 2 (size | 800 bytes) |
| │ Stack │ Segment 3 (size | 4096 bytes) |
| │ Heap │ Segment 4 (size | variable) |
| Note | Each segment has a DIFFERENT size (unlike pages which are all equal) |
Logical Address in Segmentation
A logical address consists of two parts: a segment number and an offset within that segment.
Segment Table
Each process has a segment table. Each entry contains:
- Base: Starting physical address of the segment in memory
- Limit: Length of the segment (for protection)
| Segment | Base | Limit |
|---|---|---|
| 0 | 1400 | 2400 |
| 1 | 6300 | 1500 |
| 2 | 4300 | 800 |
| 3 | 3200 | 4096 |
| 4 | 8000 | 2048 |
Address Translation
| Logical | <segment s, offset d> |
| Step 1 | Look up segment s in segment table |
| Step 2 | Check: Is offset d < limit[s]? |
| - If NO | Segmentation Fault (illegal access!) |
| - If YES | Continue |
| Step 3 | Physical address = base[s] + d |
| Example | Translate <2, 400> |
| Segment 2 | base = 4300, limit = 800 |
| Check | 400 < 800? YES ✓ |
| Example | Translate <2, 900> |
| Segment 2 | base = 4300, limit = 800 |
| Check: 900 < 800? NO ✗ | SEGMENTATION FAULT! |
Advantages of Segmentation
- Matches program structure: Each logical unit is a segment (natural for programmers)
- Easy sharing: Share the code segment between processes without sharing data
- Protection: Set permissions per segment (code = execute-only, data = read-write)
- Dynamic growth: Segments can grow (stack grows, heap grows) independently
Disadvantages of Segmentation
- External fragmentation: Since segments have variable sizes, free memory becomes fragmented into small unusable gaps
- Compaction needed: Periodically move segments to consolidate free space (expensive)
- Complex allocation: Finding a hole large enough for a segment (first-fit, best-fit, worst-fit problems)
External Fragmentation Example
Memory: [Seg A: 2KB][FREE: 1KB][Seg B: 3KB][FREE: 2KB][Seg C: 1KB][FREE: 1.5KB]
Need to allocate Segment D (4KB)
Total free memory = 1KB + 2KB + 1.5KB = 4.5KB (enough!)
But no single contiguous hole is 4KB → Cannot allocate without compaction!
Segmentation vs Paging
| Feature | Segmentation | Paging |
|---|---|---|
| Unit size | Variable (logical units) | Fixed (e.g., 4 KB) |
| Fragmentation | External | Internal |
| Program view | Logical (matches code structure) | Physical (arbitrary chunks) |
| Sharing | Natural (share a segment) | Possible but less intuitive |
| Protection | Per-segment permissions | Per-page permissions |
| Implementation | More complex | Simpler (fixed sizes) |
Segmentation with Paging (Hybrid)
Modern systems like Intel x86 combine both: the logical address space is divided into segments, and each segment is further divided into pages. This gives the logical benefits of segmentation with the physical benefits of paging (no external fragmentation).
| Logical Address | <segment, page, offset> |
| Segment Table | Page Table → Physical Frame |
| Example | Intel x86 (legacy mode) |
| Segment selector | Segment descriptor (base + limit) |
| Linear address | Page table → Physical address |
Real-World Analogy
Segmentation is like organizing a library by subject sections (history, science, fiction) rather than by book size. Each section (segment) has a different number of shelves. A book's address is "Section 3, Position 42" rather than "Shelf 147." This is more natural for librarians (programmers) — you know that all science books are in one place. But sections of different sizes create gaps when sections are rearranged (external fragmentation), unlike fixed-size shelves (paging) where any book fits anywhere.
Key Takeaways
- Segmentation divides memory into variable-size logical units matching program structure
- Addresses are <segment, offset> pairs translated via a segment table
- The limit field provides automatic bounds checking (segmentation fault if exceeded)
- Main advantage: natural sharing and protection at the logical unit level
- Main disadvantage: external fragmentation requiring compaction
- Modern systems combine segmentation with paging for best of both worlds
- Paging has largely replaced pure segmentation in modern OS (but the concept persists in x86 architecture)
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Segmentation.
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, segmentation
Related Operating Systems Topics