Segmentation - aryanjoshi0823/5143-Operating-System GitHub Wiki
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.
-
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.
-
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.
-
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.
-
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.
-
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.
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.
- Base Address: The starting physical address of a segment in main memory.
- Limit: The size of the segment, which defines its boundary.
- 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.
- Segment Number (s):Identifies the segment and acts as an index in the segment table.
- Segment Offset (d): Specifies the byte position within the segment.
- The segment number (s) is used to find the base address of the segment in the segment table.
- The segment offset (d) is added to the base address to generate the physical address.
- 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.
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