Sub topic 7
Segmentation and Segmented Paging
This chapter assumes you know how paging translates an address (page number, offset, page table) and what external fragmentation is. You should also be comfortable with powers of two and with reading short Python.
Why segmentation exists
Paging splits memory into fixed-size blocks that the programmer never sees. A program, however, is a set of named pieces: code, a stack, a heap, global data, and perhaps a library. Segmentation is a memory-management scheme that matches this view. Each piece is a segment, a variable-length block with its own number and its own size.
A logical address under segmentation has two parts:
- s, the segment number.
- d, the offset inside that segment.
The address is written as the pair (s, d). The compiler or loader assigns segment numbers. The hardware does not carve a flat number into fields the way it does with paging.
The segment table
The OS keeps one segment table per process. Entry number s holds two values:
- base: the physical address where segment s starts.
- limit: the length of segment s.
Two hardware registers locate the table. The segment-table base register (STBR) holds its address. The segment-table length register (STLR) holds the number of segments the process uses.
Translating (s, d) takes three steps:
- If s is not less than STLR, trap. The segment does not exist.
- If d is not less than limit[s], trap. The offset lies outside the segment.
- Otherwise the physical address is base[s] + d.
The limit check is strict. A segment of length 400 has valid offsets 0 to 399. Offset 400 is a violation.
A worked example
Take a process with this segment table. The values are in bytes.
| Segment | Base | Limit |
|---|---|---|
| 0 | 1400 | 1000 |
| 1 | 6300 | 400 |
| 2 | 4300 | 400 |
| 3 | 3200 | 1100 |
| 4 | 4700 | 1000 |
Translate three addresses:
- (2, 53): 53 < 400, so the physical address is 4300 + 53 = 4353.
- (3, 852): 852 < 1100, so the physical address is 3200 + 852 = 4052.
- (0, 1222): 1222 ≥ 1000, so the hardware traps.
This code reproduces the same checks:
SEGMENT_TABLE = {
0: (1400, 1000),
1: (6300, 400),
2: (4300, 400),
3: (3200, 1100),
4: (4700, 1000),
}
def translate(seg, offset):
if seg not in SEGMENT_TABLE:
raise MemoryError(f"invalid segment {seg}")
base, limit = SEGMENT_TABLE[seg]
if offset >= limit:
raise MemoryError(f"offset {offset} >= limit {limit} in segment {seg}")
return base + offset
for s, d in [(2, 53), (3, 852), (0, 1222)]:
try:
print((s, d), "->", translate(s, d))
except MemoryError as err:
print((s, d), "-> trap:", err)The output is 4353, then 4052, then a trap for the third address.
Strengths and costs of segmentation
Strengths
- Natural protection. A segment holds one kind of thing, so permissions attach cleanly. Code can be read and execute only. Data can be read and write.
- Easy sharing. Two processes can map the same physical segment, such as a library's code. Each process has a table entry pointing to the same base.
- Growth by segment. A stack or heap segment can grow independently of the others.
- No internal fragmentation. A segment gets exactly the memory it needs.
Costs
- External fragmentation. Segments have different sizes, so memory is allocated like variable partitions. Holes appear as segments come and go. Placement strategies (first fit, best fit, worst fit) and compaction apply here as they do in contiguous allocation.
- Whole-segment allocation. A large segment needs one contiguous hole as large as itself. It can fail even when the total free memory is enough.
- Table-lookup cost. Every memory reference needs a segment-table access before the data access, unless a cache hides it.
Segmented paging
Segmented paging combines the two schemes to keep the benefits of segmentation while removing external fragmentation. The logical view stays segmented. Each segment, however, is divided into fixed-size pages, and the pages are placed in any free frames.
Each segment-table entry no longer holds a base address of the segment itself. It holds the address of that segment's page table, and the limit is usually counted in pages. A logical address now has three fields:
| Field | Meaning | Used for |
|---|---|---|
| s | Segment number | Index into the segment table |
| p | Page number inside the segment | Index into that segment's page table |
| d | Offset inside the page | Added to the frame start |
The translation steps are:
- Use s to find the segment's page-table address and length.
- Check p against the segment's page-table length. Trap if p is too large.
- Use p to find the frame number f in that page table.
- The physical address is f × (page size) + d.
A worked example
Suppose a 20-bit logical address is split as 2 bits for s, 8 bits for p and 10 bits for d. The page size is 2^10 = 1 KB.
- There are at most 2^2 = 4 segments.
- Each segment has at most 2^8 = 256 pages, so at most 256 KB.
- A page table has at most 256 entries.
Now translate s = 2, p = 5, d = 499. The page table of segment 2 maps page 5 to frame 40.
- Frame 40 starts at 40 × 1024 = 40960.
- The physical address is 40960 + 499 = 41459.
The three-field layout lets the OS tell the fields apart by bit position. A programmer who thinks only in segments never sees p.
Memory references and effective access time
Without a cache, segmented paging needs three memory accesses for one data access:
- Read the segment-table entry.
- Read the page-table entry.
- Read the data.
A TLB removes the first two. The TLB is keyed on the pair (s, p). On a hit, only one memory access remains. For a TLB hit ratio h, TLB time t and memory time m, the effective access time is:
EAT = h × (t + m) + (1 − h) × (t + 3m)
With h = 0.9, t = 10 ns and m = 100 ns:
- Hit term: 0.9 × 110 = 99.
- Miss term: 0.1 × 310 = 31.
- EAT = 130 ns.
Compare this with plain paging, where a miss costs only 2m and the EAT is 120 ns. The extra level costs 10 ns here.
Table size in segmented paging
The page tables are per segment, so total table memory depends on how many segments there are and how big each one is. Suppose a process has 4 segments, and each needs its full 256 pages with 4-byte page-table entries. Each page table uses 256 × 4 = 1024 bytes, which is exactly one page. Four tables use 4 KB, plus the small segment table.
A small segment has a short page table, because the limit field says how many entries are valid. This is an advantage over a single flat page table that must cover the whole address space.
Internal fragmentation returns
Segmented paging removes external fragmentation but brings back internal fragmentation. The last page of each segment is usually only partly used. On average, each segment wastes half a page.
For a process with n segments and page size P, the expected waste is about n × P / 2. Frames of any size are interchangeable, so a free frame is always usable.
Comparing the three schemes
| Property | Paging | Segmentation | Segmented paging |
|---|---|---|---|
| Unit size | Fixed | Variable | Fixed pages inside variable segments |
| Visible to programmer | No | Yes | Segments yes, pages no |
| External fragmentation | None | Yes | None |
| Internal fragmentation | Last page of the process | None | Last page of each segment |
| Sharing and protection | Per page, awkward | Per segment, natural | Per segment, natural |
| Accesses on TLB miss | 2 | 2 | 3 |
Common errors
- Adding the base before checking. The check compares d with the limit, not the physical address with the limit.
- Treating the limit as an end address. In the segment table, the limit is a length. The segment ends at base + limit − 1.
- Confusing segment number with page number. In segmented paging, only s indexes the segment table. Only p indexes a page table.
- Forgetting the invalid-segment check. A segment number at or above STLR traps before any limit is read.
- Assuming the physical memory for two segments cannot overlap. Shared segments deliberately overlap, so two table entries may have the same base.
Where this appears in GATE
Typical question patterns are:
- Translating a given (s, d) pair from a table and deciding whether it traps.
- Finding the number of bits in a segmented-paging address from the maximum number of segments and the maximum segment size.
- Computing the page-table size for each segment.
- Counting memory accesses or EAT with a TLB.
- Identifying which scheme suffers from external or internal fragmentation.
Exercise
A segment table has segment 1 with base 6300 and limit 400, and segment 3 with base 3200 and limit 1100. Give the physical address for (1, 400) and for (3, 1099), or say that the access traps.
Show answer
For (1, 400), the offset 400 is not below the limit 400, so it traps. For (3, 1099), the offset 1099 is below 1100, so the address is 3200 + 1099 = 4299.
Exercise
A system uses segmented paging with a 32-bit logical address: 4 bits for the segment, 12 bits for the page and 16 bits for the offset. What is the page size, the maximum size of a segment, and the maximum number of entries in one segment's page table?
Show answer
The offset has 16 bits, so the page size is 2^16 = 64 KB. A segment has up to 2^12 pages, so its maximum size is 2^12 × 2^16 = 2^28 bytes = 256 MB. Each page table has at most 2^12 = 4096 entries.
Exercise
In a segmented-paging system with a TLB lookup time of 20 ns, a memory access time of 100 ns and a TLB hit ratio of 80%, a TLB miss needs the segment table and the page table. What is the effective access time?
Show answer
A hit costs 20 + 100 = 120 ns. A miss costs 20 + 3 × 100 = 320 ns. EAT = 0.8 × 120 + 0.2 × 320 = 96 + 64 = 160 ns.