Contents

Segmentation in OS

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?

  1. Matches program’s logical structure (code vs data separation)
  2. Enables fine-grained memory protection (e.g., read-only code)
  3. Facilitates code/data sharing between processes
  4. Allows independent growth of different program components
  5. 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 location
    • Limit: Maximum length of segment
    • Access 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

FeatureSegmentationPaging
SizeVariableFixed
Division BasisLogical unitsPhysical convenience
FragmentationExternalInternal
ProtectionPer-segmentPer-page

Address Translation

Logical Address Structure

  • Split into two parts:
    1. Segment Number (s): Index in segment table
    2. Offset (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

  1. Extract segment number from logical address
  2. Look up base address and limit in segment table
  3. Check if offset < limit (else segmentation fault)
  4. 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:

  1. Compaction: Rearrange memory to create large free blocks
  2. Memory Allocator Algorithms (Best-fit, Worst-fit)
  3. 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:

  1. Code (Read-Only): Base=0x0000, Limit=8KB
  2. Data (Read-Write): Base=0x2000, Limit=12KB
  3. 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

AspectPagingSegmentation
Memory DivisionFixed-size pagesVariable-size segments
VisibilityHardware/OS managedProgrammer visible
FragmentationInternalExternal
Address SpaceSingle linear spaceMultiple logical spaces
ProtectionPage-levelSegment-level
Typical Use CaseGeneral-purpose OSSpecialized systems