Skip to content
Tutorials Center
My learning

Topics

Lesson 41 of 66 ยท Memory Management

Segmentation and Segmented Paging

Learn how segmentation matches the programmer's view of memory, how segment tables translate and protect addresses, how it compares with paging, and how the two combine.

Paging divides memory into equal blocks that have nothing to do with how a program is organised: a page might hold half a function and the start of an array. A programmer, however, thinks of a program as a collection of logical units: the main program, functions, a stack, a heap, global variables, a symbol table. Segmentation makes the operating system respect that view.

The user's view of memory

A segment is a variable-size block of memory holding one logical unit. Each segment has a number and a length, and the address space of a process is a set of segments, each starting at its own offset 0.

A compiler typically creates segments such as:

  1. Code (the program instructions)
  2. Global data
  3. Heap
  4. Stack
  5. Standard library

A logical address is a pair (segment number s, offset d). The offset must stay within the segment.

Segment table and translation

The hardware keeps a segment table per process. Each entry has:

  • Base: the physical address where the segment starts.
  • Limit: the length of the segment.
  • Protection bits (read, write, execute) and a valid bit.

Translation of (s, d): if s is too large, the segment does not exist; if d >= limit[s], trap (segmentation fault); otherwise the physical address is base[s] + d.

flowchart LR
    A["(s, d)"] --> B["Segment table entry s"]
    B --> C{"d < limit?"}
    C -- no --> T["Trap: segmentation fault"]
    C -- yes --> D["physical = base + d"]

Segmentation is the base-and-limit idea, applied once per logical unit.

Python A segment table with protection

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two traps (0,10) w and (1,400) r show the benefits of segmentation: protection follows the logical meaning (code is read-only and executable, data is not executable) and an error is caught by size or permission. A write to code, or an array index past the end of a segment, becomes a segmentation fault. (That message is still what you see on Linux when a C program touches memory it should not, even though modern systems protect at page level.)

Sharing and protection

Because a segment is a logical unit, sharing is natural: two processes can have segment-table entries with the same base to share one code segment or one library, and each entry can carry its own permissions. Shared segments must have the same segment number in each process if they contain pointers to themselves.

The problem: external fragmentation again

Segments have different, variable sizes, so allocating them is the dynamic storage-allocation problem: first/best/worst fit, holes, external fragmentation and compaction. Segments are usually smaller than whole processes, which helps (the pieces are easier to fit), but the problem remains. Another drawback: the hardware must keep the whole segment contiguous, so a 100 MB heap segment needs 100 MB of contiguous RAM.

Paging versus segmentation

Paging Segmentation
Unit Fixed-size page Variable-size segment
Visible to programmer? No (invisible) Yes (logical units)
Address One number split by the hardware (segment, offset) pair
Fragmentation Internal (last page) External
Table Page table (one entry per page) Segment table (one entry per segment, with base and limit)
Sharing and protection Per page, no logical meaning Per logical unit, natural
Typical use Almost all modern CPUs Mostly historical; x86 legacy modes

Combining them: segmented paging

The two techniques can be combined to get logical units and no external fragmentation. The program uses (s, d); each segment is itself paged. The segment table entry points not to a physical base but to a page table for that segment; the offset d is split into a page number and a page offset.

flowchart LR
    L["(s, d)"] --> ST["Segment table: page table address and length for s"]
    ST --> PT["Page table of segment s"]
    L -- "d = (p, offset)" --> PT
    PT --> F["frame f"] --> PA["physical = (f, offset)"]

MULTICS used this design. Intel's 32-bit x86 chips keep segmentation hardware followed by paging; 64-bit mode essentially disables segments (treats them as one flat space) and relies on paging alone.

Python Segments made of pages

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both checks happen: the segment length is a protection check at the logical level, while the paging underneath places the pieces in any free frames. The wasted space is limited to the last page of each segment.

Where you meet segments today

  • Executable layout: Linux ELF and Windows PE files still have sections and segments (text, data, bss), and a process's memory map is a set of regions (code, heap, stack, memory-mapped files) with separate permissions, enforced by pages.
  • "Segmentation fault" is the name of the signal (SIGSEGV) sent when a process accesses memory it may not.
  • Some hardware features, such as thread-local storage on x86, still use segment registers.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A segment table entry has base 4300 and limit 400. What is the result for the logical address (segment, offset 53)?

    53 < 400, so the address is valid and equals base + offset = 4300 + 53 = 4353.
  2. 2. What kind of fragmentation does pure segmentation suffer from?

    Variable-size segments leave holes in memory, just like variable partitions.
  3. 3. Which is an advantage of segmentation over paging?

    Segments correspond to what the programmer sees, so they can be given meaningful permissions.
  4. 4. In segmented paging, what does a segment-table entry point to?

    Each segment is divided into pages, so its entry locates that segment's page table.
  5. 5. Which architecture mode mostly ignores segments and uses a flat paged address space?

    In 64-bit mode, segment bases are treated as zero (apart from the FS and GS registers) and paging does the work.

Summary

  • A segment is a variable-size logical unit (code, data, stack, heap); an address is (segment, offset).
  • The segment table holds base, limit and permissions; offsets beyond the limit trap.
  • Segmentation gives natural protection and sharing but suffers from external fragmentation.
  • Segmented paging pages each segment, combining logical units with frame-based allocation.
  • Modern systems mostly use flat paging; the idea of segments survives in memory maps and the word "segmentation fault".

Next: Virtual Memory and Demand Paging