Segmentation in OS
Contents
Segmentation
What is Segmentation?
- A memory management technique that divides a program into logical units (code, data, stack, etc.)
- Uses variable-sized memory blocks called
segments
- Example: Like splitting a recipe book into chapters (Appetizers, Main Course, Desserts) where each chapter can have different lengths.
Why Do We Need Segmentation?
- Matches program’s logical structure (code vs data separation)
- Enables fine-grained memory protection (e.g., read-only code)
- Facilitates code/data sharing between processes
- Allows independent growth of different program components
- In paging sometimes we don’t load all the important code to run another page properly, then the code doesn’t execute properly. But segmentation solves it by making the relative code into single segment.
Key Components
Segment Table
- Per-process table mapping logical segments to physical memory
- Contains:
Base Address
: Physical memory start locationLimit
: Maximum length of segmentAccess Flags
(R/W/X permissions)
Example Segment Table:
| Segment | Base | Limit | Flags |
|---------|--------|--------|-------|
| 0 (Code)| 0x4000 | 0x2000 | R-X |
| 1 (Data)| 0x8000 | 0x3000 | RW- |
| 2 (Stack)| 0xC000 | 0x1000 | RW- |
Segment vs Page
Feature | Segmentation | Paging |
---|---|---|
Size | Variable | Fixed |
Division Basis | Logical units | Physical convenience |
Fragmentation | External | Internal |
Protection | Per-segment | Per-page |
Address Translation
Logical Address Structure
- Split into two parts:
Segment Number (s)
: Index in segment tableOffset (d)
: Position within segment
Example: 16-bit address with 4 segments
| Segment (2 bits) | Offset (14 bits) |
| s=2 bits | d=14 bits |
Conversion Process
- Extract
segment number
from logical address - Look up
base address
andlimit
in segment table - Check if offset < limit (else segmentation fault)
- Physical address = base + offset
Example Conversion:
- Logical address: (Segment 1, Offset 0x500)
- Segment Table Entry 1: Base=0x8000, Limit=0x3000
- Check: 0x500 < 0x3000 → Valid
- Physical address = 0x8000 + 0x500 = 0x8500
External Fragmentation
What is it?
- Free memory becomes divided into small, non-contiguous blocks
- Caused by variable-sized segment allocation/deallocation
- Example: Like parking lot with scattered small spaces - total space exists but unusable for large vehicles
Solutions:
Compaction
: Rearrange memory to create large free blocksMemory Allocator Algorithms
(Best-fit, Worst-fit)Segmentation with Paging
(Hybrid approach)
Protection & Sharing
- Code segments can be marked
read-only
- Shared library segments can be mapped to multiple processes
- Stack segment grows automatically (with limit checks)
- Example: Two programs sharing a common math library segment
Example Scenario
Program with three segments:
- Code (Read-Only): Base=0x0000, Limit=8KB
- Data (Read-Write): Base=0x2000, Limit=12KB
- Stack (Read-Write): Base=0x5000, Limit=4KB
Logical address request:
- Accessing Data segment at offset 0x1234:
- Check Data segment limit (12KB = 0x3000)
- 0x1234 < 0x3000 → Valid
- Physical address = 0x2000 + 0x1234 = 0x3234
Advantages
- Natural program organization
- Efficient sharing between processes
- Flexible memory growth per component
- Strong memory protection
Disadvantages
- External fragmentation
- Complex memory allocation
- Costly compaction operations
- Fixed maximum segment sizes
Modern Usage
- Often combined with paging (Segmented Paging)
- x86 architecture uses segmentation in protected mode
- Most modern OS (Linux/Windows) use flat memory model with minimal segmentation
Paging vs Segmentation
Aspect | Paging | Segmentation |
---|---|---|
Memory Division | Fixed-size pages | Variable-size segments |
Visibility | Hardware/OS managed | Programmer visible |
Fragmentation | Internal | External |
Address Space | Single linear space | Multiple logical spaces |
Protection | Page-level | Segment-level |
Typical Use Case | General-purpose OS | Specialized systems |