Segmentation - aryanjoshi0823/5143-Operating-System GitHub Wiki

Segmentation in Operating Systems

Segmentation is a memory management technique that divides a process into variable-sized segments, reflecting the user's logical view of the program. Unlike paging, which splits memory into equal-sized blocks, segmentation allows processes to be divided into segments of varying sizes based on the logical structure of the program (e.g., code, data, stack). These segments are then mapped to physical memory for execution.


Key Concepts of Segmentation

  1. Logical View:

    • Segmentation provides the user's perspective of memory as logical units, such as functions, arrays, or objects. These logical units correspond to segments.
  2. Segments:

    • A segment is a logical block of a process that may vary in size. Each segment represents a specific module or part of the process.
    • The size of a segment is determined by the requirements of the program module it represents.
  3. Comparison with Paging:

    • Paging: Divides memory into fixed-size blocks (pages), irrespective of the program's logical structure.
    • Segmentation: Divides memory into variable-size segments based on the logical organization of the program.

Types of Segmentation

  1. Simple Segmentation:

    • Each process is divided into multiple segments.
    • All segments are loaded into physical memory at runtime.
    • Segments can occupy non-contiguous locations in memory.
  2. Virtual Memory Segmentation:

    • Segments are divided, but not all segments need to be loaded into memory simultaneously.
    • This technique allows for dynamic loading and swapping of segments, enabling larger processes to execute on limited physical memory.

Segment Table

The segment table is a data structure that stores metadata about each segment of a process. It is essential for mapping logical addresses to physical addresses.

Components of the Segment Table:

  1. Base Address: The starting physical address of a segment in main memory.
  2. Limit: The size of the segment, which defines its boundary.

Role of the Segment Table:

  • Maps two-dimensional logical addresses (Segment Number and Segment Offset) to one-dimensional physical addresses.
  • Stored in the main memory and accessed during address translation.

Translation of Logical Address to Physical Address

Structure of a Logical Address:

  1. Segment Number (s):Identifies the segment and acts as an index in the segment table.
  2. Segment Offset (d): Specifies the byte position within the segment.

Translation Process:

  1. The segment number (s) is used to find the base address of the segment in the segment table.
  2. The segment offset (d) is added to the base address to generate the physical address.
  3. Validation:
    • If Offset (d) < Limit, the segment is fetched from memory
    • If, Offset (d) >= Limit, an error is raised as it exceeds the segment boundary.

Example of Segmentation

Assumptions:

  • A process has three segments: Code (size 400 KB), Data (size 300 KB), and Stack (size 100 KB).
  • The segment table is as follows:
Segment Number Base Address Limit
0 (Code) 1000 KB 400 KB
1 (Data) 2000 KB 300 KB
2 (Stack) 2500 KB 100 KB

Logical Address: Segment Number = 1, Offset = 150 KB
Translation:

  • Base Address of Segment 1 = 2000 KB
  • Physical Address = 2000+ 150 = 2150 KB
⚠️ **GitHub.com Fallback** ⚠️