🖥️

Understanding Segmentation in Operating Systems

Nov 4, 2024

Segmentation in Operating Systems

Introduction to Segmentation

  • Segmentation is used to maintain a modular structure of a program.
  • Unlike paging, segmentation allows each module to reside in a contiguous memory space.

Drawbacks of Paging

  • In paging, a program's modules can be split across multiple pages.
  • Example: A C program with three functions (Deposit, Withdrawal, Cancel) can have these functions spread over different pages.
  • Modular structure is compromised as functions may occupy multiple pages.

Definition of Segment

  • A segment can be a method, function, object, or class.
  • It represents a collection of statements (typically a function).

Memory Allocation for Segments

  • Memory allocation for segments is similar to paging but depends on the size of the segment.
  • Each segment is allocated a partition that corresponds to its size (e.g., a 10 KB segment gets a 10 KB partition).
  • The operating system allocates free frames for each segment.

Logical Address Generation

  • The CPU generates a logical address comprising:
    • Segment Number: Identifies which segment is being accessed.
    • Offset: Specifies the location within the segment.

Segment Table

  • Each segment table contains:
    • Base Address: Starting address of the segment.
    • Limit Address: Length of the segment.

Example of Memory Access

  1. Segments in Process:

    • Segment 0: Deposit
    • Segment 1: Withdrawal
    • Segment 2: Cancel
  2. Accessing Segment 1:

    • Base Address: 200
    • Limit: 150 (from address 200 to 350)
  3. Offset Usage:

    • When accessing an instruction, the CPU generates a logical address (e.g., segment number 1 with offset 50).
    • Comparison: Check if offset < limit (50 < 150).
    • If valid, calculate physical address: Base Address + Offset = 200 + 50 = 250.
  4. Invalid Access Handling:

    • If offset is greater than the limit (e.g., offset 200 with limit 150), a trap (interrupt) is generated.
    • This indicates that the process is trying to access outside its segment.

Summary of Segmentation Process

  • The logical address is split into segment number and offset.
  • The segment number provides input to the segment table, retrieving base and limit addresses.
  • The offset is checked against the segment limit to ensure valid access.
  • For valid instructions, the physical address is calculated by adding the base address and offset.
  • In case of invalid access, a trap is generated, terminating the process.