🔖

Segmentation in Operating Systems

Jul 7, 2024

Segmentation in Operating Systems

Definition

  • Segmentation: A memory management technique where a process is divided into segments and loaded into the main memory.
  • Comparison with Paging:
    • Paging divides a process into fixed-sized pages, while segmentation divides a process into segments based on logical divisions in the program.

Key Concepts

Paging vs. Segmentation

  • Paging:
    • Divides processes into fixed-sized pages without considering the logical structure.
    • Can divide functions/modules into parts placed in different memory locations (e.g., parts of a function can be on different frames, causing inefficiency).
  • Segmentation:
    • Divides processes based on logical segments like functions or modules.
    • Segments vary in size based on the length of the code.

Problems with Paging

  • Divides functions arbitrarily: Can lead to inefficiency as parts of a function may be scattered in different frames.
  • Page Faults: If parts of required code are in different pages, additional overhead is needed to access them.

Advantages of Segmentation

  • Logical Grouping: Keeps logically related code together in one segment (e.g., main program, functions).
  • Varied Segment Sizes: Different segments can have different sizes based on the needs of the code.
  • User Perspective: More aligned with how users view and write programs.

Implementation

  • Base Address: Starting address of a segment in main memory.
  • Segment Size (Limit): The length or size of the segment.
  • Logical Address Generation: CPU generates logical addresses which need to be translated into physical addresses for memory access.

Segment Table

  • Structure:
    • Contains base address and size for each segment.
    • Segment numbers are used to reference the segments.
  • Address Conversion:
    • Logical Address: Composed of segment number and offset (size within segment).
    • Translation Process:
      • Identify segment number and base address.
      • Determine segment size and check if the offset is within bounds.
      • Calculate physical address by adding the offset to the base address if within bounds.

Example

  1. Logical Address: 000/1000
  2. Segment Number: 000
  3. Offset: 1000
  4. Check Base and Size: E.g., Segment 1 (S1) has base 1800 and size 400. Access is checked against these values.

Error Handling

  • Bounds Check: Ensure the offset (d) is within the segment size.
  • Trap Generation: Errors like trying to access data beyond segment size result in traps or interrupts.

Advantages over Paging

  • Logical Grouping: Segmentation keeps related code together, avoiding inefficiencies in paging.
  • Varied Sizes: Reflects the actual logical structure and requirements of the program.