Sub topic 7
Segmentation and Segmented Paging
Assume you can convert between binary and hexadecimal, understand processes and page tables, and read short formulas. This chapter explains how segmentation represents a process and how segmented paging combines segments with pages.
Segmentation
Segmentation divides a process into logical parts called segments. A segment might contain code, data, a stack, or a shared library. Unlike fixed-size pages, segments can have different lengths.
A logical address has two parts:
- A segment number, which selects an entry in the segment table.
- An offset, which identifies a byte within that segment.
Each segment-table entry usually stores:
- A base, the starting physical address of the segment.
- A limit, the segment's length.
- Protection information, such as read, write, or execute permission.
The hardware translates a logical address in these steps:
- Use the segment number to find the segment-table entry.
- Check whether the offset is less than the limit.
- Check whether the requested operation is allowed.
- Compute the physical address as
base + offset.
If the offset is at least the limit, the address is outside the segment. The hardware raises a protection fault instead of producing a physical address.
For example, suppose segment 2 has base 12000 and limit 4000. Logical address (2, 350) translates to physical address 12350. Logical address (2, 4000) is invalid because valid offsets range from 0 through 3999.
| Logical address | Base | Limit | Result |
|---|---|---|---|
| `(2, 350)` | 12000 | 4000 | Physical address 12350 |
| `(2, 4000)` | 12000 | 4000 | Protection fault |
Segmented Paging
Pure segmentation can suffer from external fragmentation because variable-sized segments need contiguous physical memory. Segmented paging avoids this requirement.
In segmented paging, each segment has its own page table. A logical address contains three fields:
- Segment number
s - Page number
p - Page offset
d
The segment table entry points to the page table for segment s and records that segment's size or page limit. The selected page-table entry maps page p to a physical frame. The physical address is formed from that frame number and offset d.
A translation therefore performs two conceptual table lookups:
- The segment number selects a segment-table entry.
- The page number selects an entry in that segment's page table.
The offset is unchanged during translation. If the page size is 2^k bytes, the lowest k bits are the page offset. The remaining part of the segment-relative address identifies the page. The system must also reject a page number beyond the segment's declared size.
Worked Example
Suppose pages are 1 KiB, so each page offset uses 10 bits. A logical address refers to segment 3, page 5, offset 100. The segment table points to segment 3's page table. If page 5 maps to frame 21, the physical address is:
21 × 1024 + 100 = 21604
The segment table can also enforce that the requested page belongs to segment 3. A missing page-table entry causes a page fault, while an invalid segment or out-of-range page causes a protection fault.
Exercise
A segment uses 4 KiB pages. Its page 6 maps to frame 10. What physical address corresponds to offset 300 in that page?
Show answer
The physical address is 10 × 4096 + 300 = 41260.