G27
Self-checkedTier 1 10 min reados.segmentation

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 (s,d)(s, d): the segment number ss and the offset dd within the segment. In binary it is ss followed by dd; if the address has mm bits of which bb are the segment number, there are 2b2^b segments of at most 2m−b2^{m-b} 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 (s,d)(s, d).

  1. If s≥STLRs \ge \text{STLR} the segment does not exist: trap.
  2. Read the entry (bases,limits)(\text{base}_s, \text{limit}_s).
  3. If d≥limitsd \ge \text{limit}_s: trap (the offset lies beyond the end of the segment: a segmentation fault).
  4. Otherwise the physical address is bases+d\text{base}_s + d.

The legal offsets are 0,…,limits−10, \ldots, \text{limit}_s - 1.

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.

PagingSegmentation
Unitfixed-size pagevariable-size segment
Visible to the programmerno (the address is one number)yes (segment and offset)
Addresspage number, offsetsegment number, offset
Table entryframe numberbase and limit
Fragmentationinternal onlyexternal only
Protection and sharingby page (bits per page)by logical unit (natural)
Checkvalid bitoffset below limit

Segmentation with paging. Each segment is itself divided into pages. A logical address is (s,d)(s, d) where the offset dd 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 LL bytes in pages of PP bytes needs ⌈L/P⌉\lceil L/P \rceil pages and wastes (−L) mod P(-L) \bmod P 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 (s,d)(s, d): the logical address is a 3-bit segment number followed by an 11-bit offset. Look at the boundary case (1,350)(1, 350): 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 table (STLR = 4)
segmentbaselimit
02200800
16100350
24000500
390001200

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.

0 / 6
computed by script: the algorithm or the translation is executed, one event per step. python3 scripts/gen-os-mem-traces.py

Worked examples

EX 1Translating (segment, offset)

Easy

A 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 -> trap

Reading it. The pair (0,120)(0, 120) maps to 2200+120=23202200 + 120 = 2320. The pair (1,349)(1, 349) is the last byte of segment 1 (limit 350), at 6449; (1,350)(1, 350) 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?

Easy

A logical address has 32 bits, of which 4 are the segment number. Then there can be 24=162^4 = 16 segments, and the offset has 28 bits, so each segment can be up to 2282^{28} bytes = 256 MB, giving a logical address space of 16×25616 \times 256 MB =4= 4 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?

Medium

Which 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

Medium

A segment of 1200 bytes is paged with pages of 256 bytes. It needs ⌈1200/256⌉=5\lceil 1200/256 \rceil = 5 pages (4 full pages hold 1024 bytes, the fifth holds 176), so 256−176=80256 - 176 = 80 bytes are wasted in the last page. A logical address in this segment, offset 700, has page number ⌊700/256⌋=2\lfloor 700/256 \rfloor = 2 within the segment and offset 700 mod 256=188700 \bmod 256 = 188 in the page. Translation uses the segment table to find the segment's page table, checks 700<1200700 < 1200, 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 level

A segment table: segment 0 (base 1000, limit 400), segment 1 (base 3000, limit 200). Which of these causes a trap: (0,399)(0, 399), (0,400)(0, 400), (1,0)(1, 0), (2,5)(2, 5)? (0,399)(0, 399): 399<400399 < 400, legal, physical 13991399. (0,400)(0, 400): not below the limit, trap. (1,0)(1, 0): physical 30003000. (2,5)(2, 5): segment 2 is not in the table (STLR = 2), trap. The limit is the size, so the largest legal offset is limit −1- 1; offsets are compared with "less than", not "less than or equal".

Cheat box

Cheat box

  • Logical address =(s,d)= (s, d). Segment table entry == (base, limit); STBR, STLR.
  • Legal iff s<s < STLR and d<d < limits_s; physical == bases+d_s + d. Otherwise trap.
  • Segments: variable size, programmer-visible, external fragmentation, natural protection and sharing.
  • Pages: fixed size, invisible, internal fragmentation.
  • bb segment bits of an mm-bit address: 2b2^b segments of ≤2m−b\le 2^{m-b} bytes.
  • Segmentation with paging: segment table gives a page table; a segment of LL bytes needs ⌈L/P⌉\lceil L/P \rceil pages; 3 accesses per reference without a TLB.

Traps and GATE patterns

Connections

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 answer

(1,250)(1, 250): 250<300250 < 300, physical 900+250=1150900 + 250 = 1150. (0,100)(0, 100): 100100 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 answer

25=322^5 = 32 segments, each up to 211=20482^{11} = 2048 bytes.

Question 3

A segment of 3000 bytes is paged with 1 KB pages (1024). How many pages, and how much waste?

Show answer

⌈3000/1024⌉=3\lceil 3000/1024 \rceil = 3 pages; the last holds 3000−2048=9523000 - 2048 = 952 bytes, so the waste is 1024−952=721024 - 952 = 72 bytes.

Question 4

Which has no internal fragmentation: paging or segmentation?

Show answer

Segmentation alone (each segment takes exactly its size). Paging wastes part of the last page.

Revision card

One-screen revision

  • (s,d)(s, d): trap if s≥s \ge STLR or d≥d \ge limit; else base +d+ d.
  • Variable size, visible, external fragmentation, natural sharing and protection.
  • bb bits of segment number: 2b2^b segments of ≤2m−b\le 2^{m-b} bytes.
  • Segmentation with paging: segment table to page table to frame; ⌈L/P⌉\lceil L/P \rceil 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