Segmentation and segmented paging
Memory as a set of variable-size segments that follow the program's structure: logical addresses as (segment, offset), the segment table with base and limit, translation and its traps, protection and sharing, external fragmentation, the comparison with paging, and segmentation combined with paging.
Why it exists
Paging splits a program into equal pieces that mean nothing to the programmer. But a program is naturally made of parts: the main code, a library, a stack, a heap, a table of data. Each part has its own size, its own protection (code is read-only, data is writable) and may be shared. Segmentation makes these parts visible to the hardware: a logical address names a segment and an offset within it, and each segment has its own base and limit. It matches the programmer's view, makes protection and sharing natural, and brings back the problem paging solved: segments have different sizes, so memory fragments externally. Segmentation combined with paging uses the strengths of both. GATE asks for translations with a segment table, for the trap conditions, and for comparisons with paging.
Prerequisites
You need the translation by table and the page-table calculations from paging.
Precise definitions
Segment. A logical unit of a program (the main program, a procedure, a stack, the symbol table, an array), of variable size. The user view of memory is a collection of segments. The compiler or loader numbers them.
Logical address. A pair : the segment number and the offset within the segment. In binary it is followed by ; if the address has bits of which are the segment number, there are segments of at most bytes each.
Segment table. One entry per segment, holding the segment's base (the physical address where it starts) and its limit (its length). The segment-table base register (STBR) points to the table in memory and the segment-table length register (STLR) gives the number of segments the process uses.
Translation of .
- If the segment does not exist: trap.
- Read the entry .
- If : trap (the offset lies beyond the end of the segment: a segmentation fault).
- Otherwise the physical address is .
The legal offsets are .
Protection and sharing. Each entry can carry protection bits (read, write, execute) so that the code segment is read-only; a segment can be shared by giving the same base in several processes' segment tables (the code of an editor, a library); sharing is at the level of whole logical units.
Fragmentation. Segments are variable in size and each needs contiguous physical memory, so segmentation suffers from external fragmentation (the same problem as variable partitions, memory management basics), with the same placement rules and compaction; it has no internal fragmentation (a segment takes just what it needs).
Segmentation compared with paging.
| Paging | Segmentation | |
|---|---|---|
| Unit | fixed-size page | variable-size segment |
| Visible to the programmer | no (the address is one number) | yes (segment and offset) |
| Address | page number, offset | segment number, offset |
| Table entry | frame number | base and limit |
| Fragmentation | internal only | external only |
| Protection and sharing | by page (bits per page) | by logical unit (natural) |
| Check | valid bit | offset below limit |
Segmentation with paging. Each segment is itself divided into pages. A logical address is where the offset is split into a page number within the segment and an offset in the page. The segment table entry holds, instead of a base, the address of that segment's page table (and its limit); then the page table gives the frame. This removes external fragmentation (frames are all the same size) and keeps the logical structure. It costs more memory accesses per reference (segment table, page table, data: three, without a TLB) and some internal fragmentation in the last page of each segment. A segment of bytes in pages of bytes needs pages and wastes bytes. This is the scheme of the MULTICS system, and in a modified form of the Intel IA-32 architecture, where the segmentation unit turns a logical address into a linear address that the paging unit translates.
How it works
The segment table has four segments with bases 2200, 6100, 4000 and 9000 and limits 800, 350, 500 and 1200. The animation translates six addresses : the logical address is a 3-bit segment number followed by an 11-bit offset. Look at the boundary case : the limit of segment 1 is 350, so the legal offsets are 0 to 349; offset 350 traps. The last address names segment 4, which does not exist (STLR is 4).
Segmentation: (segment, offset) to physical address: start
Translation computed, not typed| segment | base | limit |
|---|---|---|
| 0 | 2200 | 800 |
| 1 | 6100 | 350 |
| 2 | 4000 | 500 |
| 3 | 9000 | 1200 |
No address yet.
Step 0
A logical address is a pair (segment number s, offset d). The segment table holds, for each segment, its BASE in physical memory and its LIMIT (its length). The offset must be less than the limit; the physical address is base + d. Unlike pages, segments have different sizes, so each needs its own limit. Here the logical address has a 3-bit segment number and an 11-bit offset (14 bits); the segment-table length register (STLR) says there are 4 segments.
Worked examples
EX 1Translating (segment, offset)
EasyA program computes the six translations of the animation:
#include <stdio.h>
struct seg { unsigned base, limit; };
int main(void) {
struct seg table[4] = {{2200, 800}, {6100, 350}, {4000, 500}, {9000, 1200}};
unsigned n = 4;
unsigned pairs[6][2] = {{0, 120}, {1, 349}, {1, 350}, {2, 0}, {3, 1199}, {4, 10}};
for (int i = 0; i < 6; i++) {
unsigned s = pairs[i][0], d = pairs[i][1];
if (s >= n) { printf("(%u, %4u): no such segment -> trap\n", s, d); continue; }
if (d >= table[s].limit) { printf("(%u, %4u): offset beyond the limit %u -> trap\n", s, d, table[s].limit); continue; }
printf("(%u, %4u): base %u + %u = %u\n", s, d, table[s].base, d, table[s].base + d);
}
return 0;
}(0, 120): base 2200 + 120 = 2320
(1, 349): base 6100 + 349 = 6449
(1, 350): offset beyond the limit 350 -> trap
(2, 0): base 4000 + 0 = 4000
(3, 1199): base 9000 + 1199 = 10199
(4, 10): no such segment -> trapReading it. The pair maps to . The pair is the last byte of segment 1 (limit 350), at 6449; is one byte beyond and traps. Each check comes before the addition: first the segment number against the table length, then the offset against the segment's limit.
EX 2How many segments, how large?
EasyA logical address has 32 bits, of which 4 are the segment number. Then there can be segments, and the offset has 28 bits, so each segment can be up to bytes = 256 MB, giving a logical address space of MB GB. With a 3-bit segment number and an 11-bit offset (the animation) there are 8 segments of up to 2048 bytes. The segment table needs one entry (base, limit) for each segment that is used: if the base needs 32 bits and the limit 28 bits, an entry takes 60 bits, rounded up to 8 bytes.
EX 3Segmentation or paging?
MediumWhich scheme has external fragmentation? Segmentation. Which has internal fragmentation? Paging (the last page). In which does the programmer's address have two parts that he can see? Segmentation. Which needs a limit check on the offset? Segmentation (a page offset is always less than the page size, so no check is needed). Which scheme makes it easy to share a library and to mark the code read-only as a unit? Segmentation (a single segment table entry). Which allows a program's memory to be allocated as fixed-size blocks, in any free frames? Paging. In an exam these are the statements to separate: size fixed or variable, visible or invisible, internal or external fragmentation.
EX 4A segment in pages
MediumA segment of 1200 bytes is paged with pages of 256 bytes. It needs pages (4 full pages hold 1024 bytes, the fifth holds 176), so bytes are wasted in the last page. A logical address in this segment, offset 700, has page number within the segment and offset in the page. Translation uses the segment table to find the segment's page table, checks , indexes the page table with 2 to get a frame, and adds 188: three memory accesses (segment table, page table, data) without a TLB.
EX 5Segment table traps
GATE levelA segment table: segment 0 (base 1000, limit 400), segment 1 (base 3000, limit 200). Which of these causes a trap: , , , ? : , legal, physical . : not below the limit, trap. : physical . : segment 2 is not in the table (STLR = 2), trap. The limit is the size, so the largest legal offset is limit ; offsets are compared with "less than", not "less than or equal".
Cheat box
Cheat box
- Logical address . Segment table entry (base, limit); STBR, STLR.
- Legal iff STLR and limit; physical base. Otherwise trap.
- Segments: variable size, programmer-visible, external fragmentation, natural protection and sharing.
- Pages: fixed size, invisible, internal fragmentation.
- segment bits of an -bit address: segments of bytes.
- Segmentation with paging: segment table gives a page table; a segment of bytes needs pages; 3 accesses per reference without a TLB.
Traps and GATE patterns
Connections
Builds on
Across subjects
Segmentation is the logical-unit view where paging is the fixed-size one, and it inherits the external fragmentation of contiguous allocation. The program's text, data, heap and stack of a C program (C memory layout) are natural segments. Combining the two uses a page table per segment (page table structures). The base-limit hardware of process address space is a segment table of one entry.
PYQs
No PYQs attached to this concept yet
Previous year questions are being added section by section from your PYQ book, each with my own re-checked answer. Nothing is shown from memory.
Self-check
Question 1
Segment table: (base 500, limit 100), (base 900, limit 300). Translate (1, 250) and (0, 100).
Show answerHide
: , physical . : is not below the limit 100: trap.
Question 2
A 16-bit logical address has a 5-bit segment number. How many segments and what is the largest segment size?
Show answerHide
segments, each up to bytes.
Question 3
A segment of 3000 bytes is paged with 1 KB pages (1024). How many pages, and how much waste?
Show answerHide
pages; the last holds bytes, so the waste is bytes.
Question 4
Which has no internal fragmentation: paging or segmentation?
Show answerHide
Segmentation alone (each segment takes exactly its size). Paging wastes part of the last page.
Revision card
One-screen revision
- : trap if STLR or limit; else base .
- Variable size, visible, external fragmentation, natural sharing and protection.
- bits of segment number: segments of bytes.
- Segmentation with paging: segment table to page table to frame; pages; 3 accesses.
References
- ·Silberschatz, Galvin, Gagne, Operating System Concepts, Chapter 9 Main Memory (segmentation; example: the Intel architecture)
- ·Tanenbaum and Bos, Modern Operating Systems, Chapter 3 Memory Management (segmentation, segmentation with paging: MULTICS and the Intel x86)
- ·Stallings, Operating Systems: Internals and Design Principles, Chapter 7 Memory Management (segmentation)
Verification
The translations in the animation are performed by a script (scripts/gen-os-mem-traces.py) and rechecked by tests/os-mem-traces.test.ts (physical address = base + offset exactly when the segment exists and the offset is below the limit); the same six translations are performed by the C program on this page (npm run verify:c) and by scripts/enum-checks/os_mem.py, which also checks legality on 1000 random segment tables and the page-count arithmetic of a segment of 1200 bytes in 256-byte pages. The bit widths (a 3-bit segment number and an 11-bit offset) are the toy example's; real systems differ (the Intel architecture, for instance, selects a segment through a selector and a descriptor table). Textbook chapter titles are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand. No defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)