Chapter 8: Memory Management

Operating Systems
Memory hierarchy and cachesBase and limit registersAddress binding and the MMU Dynamic loading and linkingSwappingContiguous allocation and fit algorithms Fragmentation and bitmapsPagingTLB and access time Hierarchical, hashed, inverted tablesx86-64 and huge pagesSegmentation, x86 and ARMv8
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Abstraction

Explain the memory hierarchy, cache lines and average access time, the address space, and the problems of protection and relocation.

Addresses

Explain binding times, logical vs. physical addresses, and the MMU's base–limit check.

Linking

Discuss dynamic loading, static and dynamic linking, shared libraries via PLT/GOT, and swapping.

Contiguous allocation

Apply the fit algorithms; analyze internal/external fragmentation, bitmaps, and linked lists.

Paging

Split an address into (p, d), translate it with a page table and TLB, and compute the effective access time.

Real systems

Compare hierarchical, hashed, and inverted tables; x86-64 four/five-level paging, huge pages, segmentation, and ARMv8.

Introduction

The Memory Hierarchy and the Memory Manager

  • The only storage the CPU can access directly is registers and main memory; disk only through I/O.
  • The memory unit sees only a stream: address + read or address + data + write. It doesn't know where the address came from.
  • A register is read in one clock cycle; a DRAM access takes hundreds of cycles and the CPU stalls. A cache bridges the gap.
  • Programmers want memory that is unlimited, fast, nonvolatile, and protected; the real answer is a hierarchy.
  • The memory manager tracks free and used space, allocates it to processes, reclaims it, and protects processes from each other.
Registers< 1 ns~1 KBL1 / L2 cache1–5 nsKB–MBL3 cache10–20 ns10–100+ MBMain memory (DRAM)80–120 ns8–512 GBSSD (NVMe)10–100 µsTBHDD / tape5–10 msTB+fast, small, expensiveslow, large, cheap, nonvolatilelatency

Typical orders of magnitude as of 2026; they vary by core and model. Caches are managed by hardware, main memory by the OS.

Memory Hardware

Kinds of Main Memory

TypePropertiesUsed for
RAM (random access)Read/write, fast, volatile. DRAM needs refreshing; SRAM is faster and costlier.Main memory (DRAM: DDR5, LPDDR5X), caches (SRAM)
ROM (read-only)Written at manufacture, unchangeable, nonvolatile, cheap.Simple devices, fixed code in microcontrollers
EEPROM / FlashElectrically erasable and rewritable, nonvolatile; writes are slow and write endurance is limited.UEFI/BIOS firmware (SPI flash), SSDs, USB sticks, phone and music-player storage (NAND flash)
CMOS / NVRAMA small area kept alive by a battery or flash.Clock, boot settings
In today's computers the BIOS/UEFI code lives in flash memory, not ROM, which is why it can be updated. Portable music players also use NAND flash, not disks.

Memory Hardware

Cache Lines

  • Memory is divided into cache lines (typically 64 bytes): line 0 = 0–63, line 1 = 64–127, …
  • For address a, the line number is ⌊a / 64⌋ and the offset within the line is a mod 64.
  • When the CPU reads a word, hardware checks the cache first: a hit → ~4–5 cycles in L1; a miss → a lower level or DRAM, hundreds of cycles.
  • Caches are expensive, so they are small; that's why they are built in levels (L1, L2, L3).
  • Why does it work? Temporal locality (what was just used will be used again) and spatial locality (nearby addresses are used together).
Address 200 (64 B lines):11001000line no = 3offset within line = 8 (6 bits)line 00–63line 164–127line 2128–191line 3192–255

Memory Hardware · Animation

How Does a Read Travel Through the Hierarchy?

CPU(register)L1~1 nsL2~4 nsL3~15 nsDRAM~80 nstotal latency: 0 ns

The CPU wants to read variable x (LOAD R1, [x]). The closest, fastest level is checked first.

CPU(register)L1~1 nsmissL2~4 nsL3~15 nsDRAM~80 nstotal latency: ~1 ns

Not in L1: a miss. ~1 ns lost; the search continues one level down.

CPU(register)L1~1 nsmissL2~4 nsmissL3~15 nsmissDRAM~80 nstotal latency: ~20 ns

Misses in L2 and L3 too. Each level is a little larger but slower; ~20 ns in total.

CPU(register)L1~1 nsmisslineL2~4 nsmisslineL3~15 nsmisslineDRAM~80 nstotal latency: ~100 ns

The data comes from DRAM (~80 ns). Not just one word: the whole 64-byte line is placed in L3, L2, and L1.

CPU(register)L1~1 nslineL2~4 nslineL3~15 nslineDRAM~80 nshit!total latency: ~1 ns

A second access to data in the same line is now an L1 hit: ~1 ns. Caches work thanks to locality.

Memory Hardware · Step through the code

Code Trace: A Direct-Mapped Cache

Access sequence0864256072Cache (4 sets)setblock (tag)0—1—2—3—

The cache starts empty (−1). 64 B lines, 4 sets, direct mapping: each block can go to only one set.

Access sequence0864256072Cache (4 sets)setblock (tag)0—1—2—3—

addr[0] = 0 → block = 0 / 64 = 0, set = 0 mod 4 = 0.

Access sequence0M864256072Cache (4 sets)setblock (tag)0block 01—2—3—

Set 0 is empty → miss; block 0 is loaded.

Access sequence0M864256072Cache (4 sets)setblock (tag)0block 01—2—3—

addr[1] = 8 → block = 8 / 64 = 0, set = 0 mod 4 = 0.

Access sequence0M8H64256072Cache (4 sets)setblock (tag)0block 01—2—3—

tag[0] = 0 = block → hit. 8 is in the same 64-byte line as 0: spatial locality.

Access sequence0M8H64256072Cache (4 sets)setblock (tag)0block 01—2—3—

addr[2] = 64 → block = 64 / 64 = 1, set = 1 mod 4 = 1.

Access sequence0M8H64M256072Cache (4 sets)setblock (tag)0block 01block 12—3—

Set 1 is empty → miss; block 1 is loaded.

Access sequence0M8H64M256072Cache (4 sets)setblock (tag)0block 01block 12—3—

addr[3] = 256 → block = 256 / 64 = 4, set = 4 mod 4 = 0.

Access sequence0M8H64M256M072Cache (4 sets)setblock (tag)0block 41block 12—3—

Set 0 holds block 0 → miss; block 4 evicts it (a conflict miss).

Access sequence0M8H64M256M072Cache (4 sets)setblock (tag)0block 41block 12—3—

addr[4] = 0 → block = 0 / 64 = 0, set = 0 mod 4 = 0.

Access sequence0M8H64M256M0M72Cache (4 sets)setblock (tag)0block 01block 12—3—

Set 0 holds block 4 → miss; block 0 evicts it (a conflict miss).

Access sequence0M8H64M256M0M72Cache (4 sets)setblock (tag)0block 01block 12—3—

addr[5] = 72 → block = 72 / 64 = 1, set = 1 mod 4 = 1.

Access sequence0M8H64M256M0M72HCache (4 sets)setblock (tag)0block 01block 12—3—

tag[1] = 1 = block → hit. 72 is in the same line as 64.

Access sequence0M8H64M256M0M72HCache (4 sets)setblock (tag)0block 01block 12—3—

The loop ends: 2 hits, 4 misses (33% hit ratio). 0 and 256 map to the same set and evict each other; a 2-way set-associative cache would avoid this. (Here the whole block number is stored instead of a tag.)

int addr[6] = {0, 8, 64, 256, 0, 72};
int tag[4] = {-1, -1, -1, -1};
int hits = 0, misses = 0;
for (int i = 0; i < 6; i++) {
  int block = addr[i] / 64;
  int set = block % 4;
  if (tag[set] == block) hits++;
  else { tag[set] = block; misses++; }
}

Memory Hardware · Step through the code

Code Trace: Average Memory Access Time

/* L1, L2, L3, DRAM access time (ns) */
double lat[4] = {1, 4, 20, 80};
double miss[3] = {0.10, 0.40, 0.50};
double t = lat[3];          /* DRAM */
for (int k = 2; k >= 0; k--)
  t = lat[k] + miss[k] * t;
printf("average = %.1f ns\n", t);
L11 nsmiss 10%t(L1) = ?L24 nsmiss 40%t(L2) = ?L320 nsmiss 50%t(L3) = ?DRAM80 nst(DRAM) = ?

Four levels: access times and each cache level's miss rate. On a miss at one level, the request goes down to the next.

L11 nsmiss 10%t(L1) = ?L24 nsmiss 40%t(L2) = ?L320 nsmiss 50%t(L3) = ?DRAM80 nst(DRAM) = 80.0 nst = 80

The bottom level, DRAM, satisfies every request: starting there, t = 80 ns.

L11 nsmiss 10%t(L1) = ?L24 nsmiss 40%t(L2) = ?L320 nsmiss 50%t(L3) = ?DRAM80 nst(DRAM) = 80.0 ns

k = 2 ≥ 0: move up one level (L3).

L11 nsmiss 10%t(L1) = ?L24 nsmiss 40%t(L2) = ?L320 nsmiss 50%t(L3) = 60.0 nsDRAM80 nst(DRAM) = 80.0 nst = 20 + 0.50 × 80.0 = 60.0

L3 spends 20 ns on every request; for 50% of requests the 80 ns of the level below is added: t = 60 ns.

L11 nsmiss 10%t(L1) = ?L24 nsmiss 40%t(L2) = ?L320 nsmiss 50%t(L3) = 60.0 nsDRAM80 nst(DRAM) = 80.0 ns

k = 1 ≥ 0: move up one level (L2).

L11 nsmiss 10%t(L1) = ?L24 nsmiss 40%t(L2) = 28.0 nsL320 nsmiss 50%t(L3) = 60.0 nsDRAM80 nst(DRAM) = 80.0 nst = 4 + 0.40 × 60.0 = 28.0

L2 spends 4 ns on every request; for 40% of requests the 60 ns of the level below is added: t = 28 ns.

L11 nsmiss 10%t(L1) = ?L24 nsmiss 40%t(L2) = 28.0 nsL320 nsmiss 50%t(L3) = 60.0 nsDRAM80 nst(DRAM) = 80.0 ns

k = 0 ≥ 0: move up one level (L1).

L11 nsmiss 10%t(L1) = 3.8 nsL24 nsmiss 40%t(L2) = 28.0 nsL320 nsmiss 50%t(L3) = 60.0 nsDRAM80 nst(DRAM) = 80.0 nst = 1 + 0.10 × 28.0 = 3.8

L1 spends 1 ns on every request; for 10% of requests the 28 ns of the level below is added: t = 3.8 ns.

L11 nsmiss 10%t(L1) = 3.8 nsL24 nsmiss 40%t(L2) = 28.0 nsL320 nsmiss 50%t(L3) = 60.0 nsDRAM80 nst(DRAM) = 80.0 ns

k = −1: the loop ends.

L11 nsmiss 10%t(L1) = 3.8 nsL24 nsmiss 40%t(L2) = 28.0 nsL320 nsmiss 50%t(L3) = 60.0 nsDRAM80 nst(DRAM) = 80.0 nsaverage = 3.8 ns

Even though 10% of accesses miss L1, the average is 3.8 ns: about 21 times faster than DRAM's 80 ns. A high hit ratio is what makes the hierarchy work.

Memory Hardware

Cache Design Questions

1. When to place?Usually on a miss; hardware also prefetches the following lines.
2. Which line?Mapping: direct-mapped, k-way set-associative, or fully associative.
3. Which one leaves when space is needed?Replacement policy: LRU approximations, random, FIFO.
4. Where does the evicted line go?If modified ("dirty") it is written to the level below (write-back); or every write goes down at once (write-through).
SizeA larger cache gets more hits but is slower and costlier.
ReplacementA balance between performance and hardware complexity.
CoherenceOn multicore, copies of the same line must stay up to date (protocols such as MESI).

The same four questions come up in the "caches" the OS manages: the page cache, virtual memory, the TLB.

Introduction

Memory Without Abstraction

  • On early computers (mainframes before the 1960s, PCs before the 1980s) every program saw physical memory directly: MOV REGISTER1, 1000 reads physical address 1000.
  • Two programs cannot be in memory at once: both write to the same physical addresses and one clobbers the other.
  • In (a) and (c), a bug in a user program can crash the OS.
  • (b) was used in some embedded systems, (c) in early PCs with MS-DOS (BIOS drivers in ROM).
  • MMU-less microcontrollers still work this way today; most protect regions with at least an MPU (memory protection unit).
User programOS (RAM)(a)0OS (ROM)User program(b)0Drivers (ROM)User programOS (RAM)(c)00xFFF…

Introduction · Step by step

The Relocation Problem

(a) Program AJMP 240ADD4MOV8⋮MOV24ADD28⋮…16380(b) Program BJMP 280MOV REG1, 284ADD8⋮MOV24CMP28⋮…16380both compiled to startat address 0 (16 KB)

A and B are two 16 KB programs, both compiled assuming they start at address 0.

(a) Program AJMP 240ADD4MOV8⋮MOV24ADD28⋮…16380(b) Program BJMP 280MOV REG1, 284ADD8⋮MOV24CMP28⋮…16380(c) both togetherJMP 240ADD4MOV8⋮MOV24ADD28⋮JMP 2816384MOV REG1, 2816388ADD16392⋮MOV16408CMP16412⋮

The OS loads A at 0, then B right above it, at 16384.

(a) Program AJMP 240ADD4MOV8⋮MOV24ADD28⋮…16380(b) Program BJMP 280MOV REG1, 284ADD8⋮MOV24CMP28⋮…16380(c) both togetherJMP 240ADD4MOV8⋮MOV24ADD28⋮JMP 2816384MOV REG1, 2816388ADD16392⋮MOV16408CMP16412⋮jumpsinto A!

B's first instruction, JMP 28, uses an absolute address: it jumps to physical 28, i.e., into A's ADD instruction, and the program crashes.

(a) Program AJMP 240ADD4MOV8⋮MOV24ADD28⋮…16380(b) Program BJMP 280MOV REG1, 284ADD8⋮MOV24CMP28⋮…16380(c) both togetherJMP 240ADD4MOV8⋮MOV24ADD28⋮JMP 1641216384MOV REG1, 2816388ADD16392⋮MOV16408CMP16412⋮+16384

Static relocation (IBM 360): the loader adds the start address to every address word: JMP 16412. Loading slows down.

(a) Program AJMP 240ADD4MOV8⋮MOV24ADD28⋮…16380(b) Program BJMP 280MOV REG1, 284ADD8⋮MOV24CMP28⋮…16380(c) both togetherJMP 240ADD4MOV8⋮MOV24ADD28⋮JMP 1641216384MOV REG1, 2816388ADD16392⋮MOV16408CMP16412⋮+16384constant 28 must not change!

But the 28 in MOV REG1, 28 is a constant and must not change. To know which words are addresses, the loader needs extra relocation information.

Address Spaces

Address Spaces: Base and Limit Registers

  • Address space: the set of addresses a process can use. Every process has its own address space, independent of the others.
  • Two problems to solve: protection (no access to another's space) and relocation (loadable anywhere).
  • The simplest hardware: base (starting physical address) and limit (length) registers. The program is loaded as is into a contiguous area.
  • The registers are loaded only in kernel mode by privileged instructions; on a context switch the new process's values are installed.
  • The CDC 6600 worked this way. The Intel 8088's segment registers were base only; with no limit there was no protection.
Operating system0process256000process (active)300040process4209408800001024000300040base120900limit

Address Spaces · Step by step

Hardware Address Protection

CPUaddress300,040≥ base ?yes< base+limit ?MemoryTrap to the OS: addressing errornonobase = 300,040limit = 120,900valid: 300,040 … 420,939

300,040 = base: the first valid address; both comparisons pass and the access proceeds.

CPUaddress356,000≥ base ?yes< base+limit ?MemoryTrap to the OS: addressing errornonobase = 300,040limit = 120,900valid: 300,040 … 420,939

356,000 is inside the range; the access proceeds. The check happens on every access, in parallel hardware.

CPUaddress420,939≥ base ?yes< base+limit ?MemoryTrap to the OS: addressing errornonobase = 300,040limit = 120,900valid: 300,040 … 420,939

420,939 = base + limit − 1: the last valid address.

CPUaddress420,940≥ base ?yes< base+limit ?MemoryTrap to the OS: addressing errornonobase = 300,040limit = 120,900valid: 300,040 … 420,939

420,940 = base + limit: the second comparison fails → trap. The kernel usually terminates the process (SIGSEGV on Unix).

CPUaddress299,999≥ base ?yes< base+limit ?MemoryTrap to the OS: addressing errornonobase = 300,040limit = 120,900valid: 300,040 … 420,939

299,999 is below the base: the first comparison fails → the other process's space is protected.

Every user-mode access is checked. In kernel mode there is no check; the OS must be able to access all of memory (Silberschatz Figure 9.2).

Address Spaces

Address Binding Times

Sourcemain.cObjectmain.oExecutablea.outIn memoryprocesscompilerlinkerloader+ other .o, static .a+ dinamik .so (ld.so)MMUon every accesscompile timeload timeexecution time
Binding timeGenerated codeIf location changesHardware
CompileAbsolute code (e.g., MS-DOS .COM)recompilenone
LoadRelocatable code; the loader fixes up addressesreloadnone
ExecutionLogical addresses; the process can move while runningnothingMMU (base–limit, paging)

In source code addresses are symbolic (count); the compiler binds them to relocatable addresses ("14 bytes from the start of this module"), the linker/loader to absolute addresses (74014). All modern general-purpose systems use execution-time binding.

Address Spaces

Logical and Physical Addresses, the MMU

  • Logical (virtual) address: the address the CPU generates. Physical address: the one the memory unit sees, loaded into the memory-address register (MAR).
  • With compile- and load-time binding the two are the same; with execution-time binding they differ.
  • Logical address space: the set of addresses a program generates; physical address space: the corresponding physical addresses.
  • The run-time mapping is done by hardware, the MMU (memory-management unit). The program never sees physical addresses.
  • In today's processors the MMU, with its TLBs and page-table walker, sits inside every core.
CPU chipCPUMMUvirtualphysical addressbusMemoryDisk controller

Address Spaces · Step by step

Dynamic Relocation with an MMU

CPUlogical346< limit ?3,000limit registeryes+14,000base (relocation)physical14,346Memorynotrap: addressing error

Logical 346 < 3,000 → physical 346 + 14,000 = 14,346. The program sees 0 through 2,999.

CPUlogical2,999< limit ?3,000limit registeryes+14,000base (relocation)physical16,999Memorynotrap: addressing error

2,999, the last valid logical address → 16,999.

CPUlogical3,500< limit ?3,000limit registeryes+14,000base (relocation)physical—Memorynotrap: addressing error

3,500 ≥ limit → trap without adding. The comparison is on the logical address, so it is the same whatever the base.

CPUlogical28< limit ?16,384limit registeryes+16,384base (relocation)physical16,412Memorynotrap: addressing error

Program B from the earlier example: base = limit = 16,384. JMP 28 stays unchanged; the MMU produces 28 + 16,384 = 16,412. No static relocation is needed.

Silberschatz's relocation register is Tanenbaum's base register. Every access needs an addition and a comparison; without special circuitry, the addition is slow because of carry propagation.

Linking and Loading · Step through the code

Dynamic Loading

#include <dlfcn.h>
#include <stdio.h>

int main(void) {
  void *h = dlopen("libm.so.6", RTLD_LAZY);
  if (!h) { puts(dlerror()); return 1; }
  double (*root)(double) =
      (double (*)(double)) dlsym(h, "sqrt");
  printf("%.3f\n", root(2.0));
  dlclose(h);
  return 0;
}
mainheaplibcstackprocess address space0high

The program has started: the address space holds only main, libc, the heap, and the stack; libm is not loaded.

mainheaplibmlibcstackprocess address space0high

dlopen: the dynamic linker (ld.so) finds the file and maps it into the address space with mmap. Loading happens now, at the program's request.

mainheaplibmlibcstackprocess address space0high

dlsym looks up sqrt in the symbol table and returns its address in this process's address space.

mainheaplibmlibcstackprocess address space0high

The call goes through the function pointer; the output is 1.414.

mainheaplibcstackprocess address space0high

dlclose: when the reference count reaches 0, the library is removed from the address space.

A routine isn't loaded until it is called; unused code is never loaded. No hardware support is needed; the program is designed this way and the OS provides a library (dlopen; LoadLibrary/GetProcAddress on Windows). Since glibc 2.34, -ldl is unnecessary.

Linking and Loading

Static and Dynamic Linking

Static linkingDynamic linking
Who, and when?The linker (ld), after compilationThe dynamic linker (ld.so), at load or run time
File.a, .lib.so, .dll, .dylib
MemoryA separate copy in every programCode pages are in memory once, shared
UpdatesMust relinkReplacing the library suffices (versioning: soname)
RiskLarge files; security patches are delayedVersion mismatches ("DLL hell")
  • Static: library routines are copied into the binary (ld *.o -lc -lm).
  • Dynamic: only a stub remains in the file; the address is resolved at execution time.
Process 1codedatalibc.sostackProcess 2codedatalibc.sostacklibc codeone copyphysical memory

Linking and Loading · Step by step

Lazy Binding: PLT and GOT

maincall printf@pltPLT entryjmp *GOT[printf]GOT[printf]→ resolverld.so resolver_dl_runtime_resolvelibc: printfreal code1st call

The compiler doesn't know printf's address; the call goes to a small PLT (procedure linkage table) stub. This is the textbooks' stub.

maincall printf@pltPLT entryjmp *GOT[printf]GOT[printf]→ resolverld.so resolver_dl_runtime_resolvelibc: printfreal code1st call

The PLT jumps indirectly through the address in the GOT (global offset table). Initially the GOT entry points to the resolver.

maincall printf@pltPLT entryjmp *GOT[printf]GOT[printf]→ resolverld.so resolver_dl_runtime_resolvelibc: printfreal codewrite address1st call

The ld.so resolver looks the symbol up in the loaded libraries and writes the address it finds into the GOT.

maincall printf@pltPLT entryjmp *GOT[printf]GOT[printf]→ printfld.so resolver_dl_runtime_resolvelibc: printfreal code1st call

The resolver jumps to printf. The code pages didn't change; only the per-process GOT page did, so the code stays shareable.

maincall printf@pltPLT entryjmp *GOT[printf]GOT[printf]→ printfld.so resolver_dl_runtime_resolvelibc: printfreal code2nd and later calls

Later calls go PLT → GOT → printf: just one indirect jump. For security, many distributions use -z now (full RELRO) to resolve all symbols at load time and make the GOT read-only.

The textbook account, "the stub replaces itself with the routine's address", is conceptual; on ELF systems what changes is the GOT entry, not code. And it's done by the dynamic linker in user space, not by the OS.

Linking and Loading

Building Libraries: Static and Shared

Static library

$ gcc -c func.c -o func.o
$ ar rcs libfunc.a func.o
$ gcc main.c -o main -static -L. -lfunc
$ ./main
  • ar archives object files; the linker copies only the objects it needs.
  • -static also requires the static version of libc (libc.a).

Shared library

$ gcc -fPIC -c func.c -o func.o
$ gcc -shared -o libfunc.so func.o
$ gcc main.c -o main -L. -lfunc
$ export LD_LIBRARY_PATH=$(pwd)
$ ./main
$ ldd ./main   (libfunc.so => ./libfunc.so)
  • -fPIC: position-independent code: PC-relative addressing, data via the GOT.
  • So one physical page can map to a different virtual address in each process (ASLR).
  • Permanent alternative to LD_LIBRARY_PATH: -Wl,-rpath,'$ORIGIN' or ldconfig.
static: a copy in every programmain1funcmain2funcshared: one copy, two mappingsmain1main2libfunc.so

Distributions now build programs as PIE too, so ASLR loads the program itself at a random address.

Swapping

Swapping

  • Standard swapping: an entire process is temporarily written to a backing store (disk), then brought back.
  • Total memory demand can exceed physical memory; the degree of multiprogramming rises.
  • A returning process may land at another address → execution-time binding is required.
  • The cost: a 100 MB process, a 50 MB/s disk → 2 s out + 2 s in = 4 s (Silberschatz).
  • A process with pending I/O is not swapped (DMA would write to the old address), or its I/O goes through kernel buffers.
Operating systemuser spaceP1(room for P2)main memoryP1P2backing store① swap out② swap in

Swapping

Page Swapping and Modern Systems

  • Moving a whole process is unacceptable today: a 1 GiB process, 3 GB/s NVMe → round trip ≈ 0.72 s.
  • Linux, Windows, and macOS swap pages: only long-unused (cold) pages go out, and a 4 KiB page comes back in tens of µs.
  • Compression before going to disk: Linux zswap and zram, Windows memory compression, the macOS compressor.
  • Mobile: traditional swapping is absent or limited because of flash wear and energy; Android uses zram + lmkd, iOS compression + app termination (jetsam).
main memoryP1 p0P2 p3P1 p2P3 p1swap spaceP1 p1P1 p4P2 p0P3 p0P2 p1page outpage in

Contiguous Allocation

Contiguous Memory Allocation

  • Memory is split into two regions: the operating system and user processes. Linux and Windows put the kernel in high memory.
  • Each process sits in a single contiguous region, protected by base–limit.
  • Fixed partitions (IBM OS/360 MFT): number of partitions = degree of multiprogramming; a small process in a large partition → internal fragmentation.
  • Variable partitions (MVT): each partition is as large as its process; free blocks are called holes.
  • Processes grow: room is left for the data segment (malloc) and the stack. A process returning from swap is brought in at the size it uses, not with its spare room.
(a)B (room to grow)BA (room to grow)AOS(b)B stack ↓room for growthB data + codeA stack ↓room for growthA data + code

Contiguous Allocation · Step by step

Variable Partitions

01024 KOS128 K896 Kholes: 896 K @ 128

Only the OS is in memory.

01024 KOS128 KA320 K576 Kholes: 576 K @ 448

A (320 K) arrives and goes into the first hole that fits.

01024 KOS128 KA320 KB224 K352 Kholes: 352 K @ 672

B (224 K) arrives and goes into the first hole that fits.

01024 KOS128 KA320 KB224 KC288 K64 Kholes: 64 K @ 960

C (288 K) arrives and goes into the first hole that fits.

01024 KOS128 K320 KB224 KC288 K64 Kholes: 320 K @ 128, 64 K @ 960

A terminates: 128–448 becomes a 320 K hole.

01024 KOS128 KD256 K64 KB224 KC288 K64 Kholes: 64 K @ 384, 64 K @ 960

D (256 K) goes into A's hole, leaving a small 64 K hole.

01024 KOS128 KD256 K288 KC288 K64 Kholes: 288 K @ 384, 64 K @ 960

B terminates: its space merges with the 64 K hole to its left → 288 K. Adjacent holes are coalesced at once.

01024 KOS128 KD256 KE192 K96 KC288 K64 Kholes: 96 K @ 576, 64 K @ 960

E (192 K) goes into the 288 K hole. 160 K is free in total but split into two pieces: external fragmentation is growing.

  • The OS keeps allocated partitions and holes in a table (or a list).
  • When a process arrives, a large enough hole is found and the rest stays a hole; when it ends, its space returns as a hole and merges with its neighbors.

Contiguous Allocation

Placement Algorithms

First fit

Scan from the start of the list and stop at the first hole that is big enough. Fast; small holes pile up near the front.

Next fit

The search resumes where it left off; slightly worse than first fit (Bays, 1977).

Best fit

Scan the whole list and pick the hole closest to the request. Slow; leaves tiny, useless holes.

Worst fit

Pick the largest hole so the leftover is still useful. Does poorly in simulations.

Quick fit

Separate lists for common sizes (4 K, 8 K, 12 K …). Finding is fast, merging is expensive.

A common improvement

Separate lists for processes and holes. Today's malloc implementations (jemalloc, tcmalloc) use size classes.

Simulations (Silberschatz, Knuth) show that first fit and best fit beat worst fit in both time and storage utilization; first fit is generally faster.

Contiguous Allocation · Step through the code

Best Fit: Code Trace

int best_fit(int hole[], int n, int req) {
  int best = -1;
  for (int i = 0; i < n; i++)
    if (hole[i] >= req &&
        (best == -1 || hole[i] < hole[best]))
      best = i;
  if (best != -1) hole[best] -= req;
  return best;
}
/* hole = 120, 450, 230, 300, 180 (K) */
/* best_fit(hole, 5, 210); best_fit(hole, 5, 160); */
req = 210 K120[0]450[1]230[2]300[3]180[4]

The hole list is 120, 450, 230, 300, 180 K. The first request is 210 K; best = −1 (no candidate).

req = 210 K120[0]450[1]230[2]300[3]180[4]

[0] = 120 < 210: doesn't fit, skip.

req = 210 K120[0]450[1]230[2]300[3]180[4]

[1] = 450 ≥ 210 and the first candidate → best = 1.

req = 210 K120[0]450[1]230[2]300[3]180[4]

[2] = 230 ≥ 210 and smaller than 450 → new best best = 2.

req = 210 K120[0]450[1]230[2]300[3]180[4]

[3] = 300 fits, but the current best, 230, is smaller: no change.

req = 210 K120[0]450[1]230[2]300[3]180[4]

[4] = 180 < 210: doesn't fit, skip.

req = 210 K120[0]450[1]20[2]300[3]180[4]

210 K is carved from hole [2], leaving a small 20 K hole. Returns 2. First fit would have split the 450 for the same request.

req = 160 K120[0]450[1]20[2]300[3]180[4]

The second request is 160 K; hole [2] is now 20 K. The scan starts over and walks the whole list.

req = 160 K120[0]450[1]20[2]300[3]180[4]

[0] = 120 doesn't fit; [1] = 450 ≥ 160 and the first candidate → best = 1.

req = 160 K120[0]450[1]20[2]300[3]180[4]

[2] = 20 doesn't fit; [3] = 300 ≥ 160 and smaller than 450 → new best best = 3.

req = 160 K120[0]450[1]20[2]300[3]180[4]

[4] = 180 ≥ 160 and smaller than 300 → new best best = 4.

req = 160 K120[0]450[1]20[2]300[3]20[4]

160 K is carved from hole [4], leaving a small 20 K hole. Returns 4. Best fit has now left two useless 20 K holes.

Contiguous Allocation · Step by step

Placement: An Example

100 K500 K200 K300 K600 KFirst fitNext fitBest fitWorst fit

The holes, in order, are 100, 500, 200, 300, 600 K. The requests, in order: 212, 417, 112, 426 K.

100 K500 K200 K300 K600 KFirst fit212288212 K → 500 K hole (288 K left)Next fit212288212 K → 500 K hole (288 K left)Best fit21288212 K → 300 K hole (88 K left)Worst fit212388212 K → 600 K hole (388 K left)

212 K: first/next fit put it in 500, best fit in 300 (88 left), worst fit in 600.

100 K500 K200 K300 K600 KFirst fit212288417183417 K → 600 K hole (183 K left)Next fit212288417183417 K → 600 K hole (183 K left)Best fit4178321288417 K → 500 K hole (83 K left)Worst fit41783212388417 K → 500 K hole (83 K left)

417 K: first and next fit use 600; best and worst fit use 500 (83 left).

100 K500 K200 K300 K600 KFirst fit212112176417183112 K → 288 K hole (176 K left)Next fit21228841711271112 K → 183 K hole (71 K left)Best fit417831128821288112 K → 200 K hole (88 K left)Worst fit41783212112276112 K → 388 K hole (276 K left)

112 K: first fit uses the 288 leftover; next fit the 183 where it last stopped (71 left); best fit 200; worst fit 388.

100 K500 K200 K300 K600 KFirst fit212112176417183426 K waitsNext fit21228841711271426 K waitsBest fit417831128821288426174426 K → 600 K hole (174 K left)Worst fit41783212112276426 K waits

426 K: only best fit can place it (600 K is still whole). In the others there is enough free space in total, but not in one piece: external fragmentation.

Contiguous Allocation

Internal and External Fragmentation

  • External fragmentation: there is enough total free memory for a request, but it is not contiguous. A problem of variable partitions.
  • The 50-percent rule (Knuth): with first fit and N allocated blocks, about 0.5 N blocks are lost to fragmentation → one third of memory is unusable.
  • Internal fragmentation: the block given is larger than requested; the difference is wasted inside the block. Seen with fixed partitions, paging, and the buddy system.
  • Solutions: compaction (external), or letting a process's address space be noncontiguous (paging, segmentation).
External fragmentationP1P2P3403040110 K free, but the largest piece is 40 Krequest 60 K ✗Internal fragmentationprocess 18,462 B2 B18,464 B block given; 2 B wasted (tracking the remaindercosts more than the hole)

Contiguous Allocation · Step by step

Compaction

01024 KOS128 KP1160 K96 KP2192 K64 KP3128 K160 KP464 K

Initially there is 352 K free among four processes (96 + 64 + 160 + 32), but the largest hole is 160 K.

01024 KOS128 KP1160 KP2192 KP3128 KP464 K

P2 slides down: 384 → 288. The moved process's base register is updated.

01024 KOS128 KP1160 KP2192 KP3128 KP464 K

P3: 640 → 480.

01024 KOS128 KP1160 KP2192 KP3128 KP464 K

P4: 928 → 608. Now there is a single 352 K hole. 384 K were copied in total.

  • Compaction: shuffle processes to one end and gather the holes into one block. Possible only with execution-time binding (a base register).
  • Expensive: per Tanenbaum, 16 GB of memory at 8 bytes per 8 ns takes ≈ 16 s; even at today's ≈20 GB/s copy rate it's ≈ 0.9 s, and processes stop meanwhile.
  • Not to be confused with data compression: the goal isn't to shrink data but to make blocks contiguous.
  • Today: Linux performs page migration to create contiguous physical frames for huge pages (kcompactd).

Free-Space Management

Tracking Free Memory: Bitmaps and Linked Lists

(a) memory, 32 unitsABCDE08162432(b) bitmap11111000111111111100111111111000(c) linked list (P = process, H = hole, start, length)P 0 5H 5 3P 8 6P 14 4H 18 2P 20 6P 26 3H 29 3
BitmapLinked list
Structure1 bit per allocation unit (0 free, 1 used)A node per process/hole (P/H, start, length)
SizeFixed: with 4 B units, 1/33 of memory; 16 GiB, 4 KiB units → 512 KiBProportional to the number of processes + holes
SearchFinding k consecutive 0s is slowWalk the hole list; kept sorted by address, merging is easy

Free-Space Management · Step through the code

Searching a Bitmap for Free Space: Code Trace

/* find k consecutive free (0) units */
int find(const char bit[], int n, int k) {
  int start = 0, len = 0;
  for (int i = 0; i < n; i++) {
    if (bit[i] == 0) {
      if (len == 0) start = i; /* new run */
      if (++len == k) return start;
    } else
      len = 0;               /* run broken */
  }
  return -1;
}
10110203140506071819orange: current free run · green: area found

Memory has 10 units; used units are 1. We look for three consecutive 0s for a process of k = 3 units.

10110203140506071819iorange: current free run · green: area found

bit[0] = 1: a used unit, len stays 0.

10110203140506071819iorange: current free run · green: area found

bit[1] = 1: a used unit, len stays 0.

10110203140506071819iorange: current free run · green: area found

bit[2] = 0 with no run in progress → a new run, start = 2, len = 1.

10110203140506071819iorange: current free run · green: area found

bit[3] = 0 → the run grows: len = 2; not yet k.

10110203140506071819iorange: current free run · green: area found

bit[4] = 1: the 2-unit run broke too soon → len = 0. The hole at 2–3 is too small for this process.

10110203140506071819iorange: current free run · green: area found

bit[5] = 0 with no run in progress → a new run, start = 5, len = 1.

10110203140506071819iorange: current free run · green: area found

bit[6] = 0 → the run grows: len = 2; not yet k.

10110203140506071819iorange: current free run · green: area found

bit[7] = 0 → len = 3 = k: the run is long enough; the start, 5, is returned.

10110203140506071819orange: current free run · green: area found

The allocator sets bits 5, 6, and 7 to 1. The search moves linearly from the start of the array, which is why it is slow for a large bitmap.

Free-Space Management · Step by step

Freeing in a Linked List

before X terminatesAXBafterAholeB

Both neighbors are processes: X's node becomes an H; the list length stays the same.

before X terminatesAXholeafterAhole

Hole on the right: X and the hole on its right merge, and one node is deleted.

before X terminatesholeXBafterholeB

Hole on the left: the left hole grows by X's size, and X's node is deleted.

before X terminatesholeXholeafterhole

Holes on both sides: three nodes collapse into one hole, and two nodes are deleted. A doubly linked list makes finding the previous node easy.

  • If the list is sorted by address, merging takes constant time by looking at the neighbors.
  • Keeping holes in a separate list sorted by size speeds up best fit (first fit becomes best fit) but slows down freeing.
  • The hole list can be stored inside the holes themselves: the first words of each hole hold its size and a pointer to the next hole.

Free-Space Management · Step by step

Inside malloc: Headers and an Embedded Free List

heap: 4,096 BBfree: size 4,080H = 16 B header: in use {size, magic number}, free {size, next}free list:head → [4,080] → NULLthe library gets the heap via mmap/sbrk and makes it one free chunk: 4,096 − 16 = 4,080

Initially: one free chunk. The free-list node lives inside the chunk; the allocator can't call malloc for a separate data structure.

heap: 4,096 BBp1: 200Bfree: size 3,864H = 16 B header: in use {size, magic number}, free {size, next}free list:head → [3,864] → NULLmalloc(200) uses 216 B: 4,080 − 216 = 3,864; p1 points just past the header

p1 = malloc(200): the first-fit chunk is split. The size and magic number go in the header.

heap: 4,096 BBp1: 200Bp2: 100Bp3: 300Bfree: 3,432H = 16 B header: in use {size, magic number}, free {size, next}free list:head → [3,432] → NULL3,864 − (100 + 16) − (300 + 16) = 3,432

p2 = malloc(100), p3 = malloc(300): the same splitting repeats.

heap: 4,096 BBp1: 200Bfree: 100Bp3: 300Bfree: 3,432H = 16 B header: in use {size, magic number}, free {size, next}free list:head → [100] → [3,432] → NULLfree(p2): header = (char *)p2 − 16 → reads size 100, puts the chunk at the head of the list

free(p2) takes no size: it is read from the header. Now a 150 B request won't fit the 100 chunk: external fragmentation.

heap: 4,096 BBfree: size 316Bp3: 300Bfree: 3,432H = 16 B header: in use {size, magic number}, free {size, next}free list:head → [316] → [3,432] → NULLfree(p1): coalesces with the free neighbor: 216 + 116 = 332 B → size 332 − 16 = 316

free(p1): two free chunks adjacent by address are coalesced; the list gets shorter.

Magic numberIf the fixed pattern in the header is corrupted (overflow, double free, a bad pointer), the library can notice and abort the program.
The real requestmalloc(n) consumes at least n + header bytes, rounded up for alignment (glibc: 16 B).
A system call?No, a library function. When space runs out it grows the heap with brk/sbrk; large requests (≥ 128 KiB in glibc) come straight from mmap.

Paging

Paging: The Basic Idea

  • Physical memory is divided into fixed-size frames, logical memory into pages of the same size.
  • A process's physical space need not be contiguous → no external fragmentation; only internal fragmentation in the last page.
  • A process of N pages needs N free frames; the OS keeps track of free frames.
  • The page table maps page numbers to frame numbers; each process has its own table.
  • The backing store is divided into blocks of the same size.
logical memorypage 0page 1page 2page 3page table01142337physical memory01page 023page 24page 1567page 3

Paging

Splitting the Address: (p, d)

p: page numberm − n bitd: page offsetn bitlogical address space 2^m, page size 2^n32 bit, 4 KiB:p = 20 bitd = 12 bit
ExampleComputationResult
32-bit address, 4 KiB pages4096 = 212 → d is 12 bits, p = 32 − 12 = 20 bits220 = 1,048,576 pages
0x12345678p = 0x12345678 >> 12, d = 0x12345678 & 0xFFFp = 0x12345 (74,565), d = 0x678 (1656)
16-bit address, 4 KiB (Tanenbaum)8196 = 2·4096 + 4p = 2, d = 4
48-bit (x86-64), 4 KiBp = 48 − 1236-bit page number

Page sizes are powers of 2, so division and remainder are just a shift and a mask; hardware does them for free. Physical address = frame × page size + d.

Paging · Step by step

Translation with a Page Table

logicala0b1c2d3e4f5g6h7i8j9k10l11m12n13o14p15page table05162132physical memory (32 bytes)0frame 04ijklframe 18mnopframe 212frame 316frame 420abcdframe 524efghframe 628frame 7

Page size 4 bytes, physical memory 32 bytes (8 frames). 16 bytes of logical memory, a…p; page table 0→5, 1→6, 2→1, 3→2.

logicala0b1c2d3e4f5g6h7i8j9k10l11m12n13o14p15page table05162132physical memory (32 bytes)0frame 04ijklframe 18mnopframe 212frame 316frame 420abcdframe 524efghframe 628frame 7address 0 → p = 0, d = 05 × 4 + 0 = 20

Logical 0 (a): page 0, offset 0 → frame 5 → physical 20.

logicala0b1c2d3e4f5g6h7i8j9k10l11m12n13o14p15page table05162132physical memory (32 bytes)0frame 04ijklframe 18mnopframe 212frame 316frame 420abcdframe 524efghframe 628frame 7address 3 → p = 0, d = 35 × 4 + 3 = 23

Logical 3 (d): page 0, offset 3 → frame 5 → physical 23.

logicala0b1c2d3e4f5g6h7i8j9k10l11m12n13o14p15page table05162132physical memory (32 bytes)0frame 04ijklframe 18mnopframe 212frame 316frame 420abcdframe 524efghframe 628frame 7address 4 → p = 1, d = 06 × 4 + 0 = 24

Logical 4 (e): page 1, offset 0 → frame 6 → physical 24.

logicala0b1c2d3e4f5g6h7i8j9k10l11m12n13o14p15page table05162132physical memory (32 bytes)0frame 04ijklframe 18mnopframe 212frame 316frame 420abcdframe 524efghframe 628frame 7address 13 → p = 3, d = 12 × 4 + 1 = 9

Logical 13 (n): page 3, offset 1 → frame 2 → physical 9. The process's order isn't preserved in physical memory, yet the program sees it as contiguous.

The example from Silberschatz Figure 9.10. The page-table entry's index is the page number; the page number itself isn't stored in the table.

Paging · Step through the code

Translation Inside the MMU

/* 16-bit virtual address, 4 KiB pages */
MOV REG, 0
MOV REG, 8192
MOV REG, 20500
MOV REG, 8196
MOV REG, 32780

/* page table (page → frame) */
/* 0→2 1→1 2→6 3→0 4→4 5→3 */
/* 9→5 11→7, others: absent */
virtual address 00000000000000000p = 0d = 0frame 2 + d → physical 8192010000000000000

Page 0 → frame 2: 8192. The upper 4 bits are the page number; the lower 12 bits are copied unchanged.

virtual address 81920010000000000000p = 2d = 0frame 6 + d → physical 24576110000000000000

8192 = page 2, offset 0 → frame 6: 24576.

virtual address 205000101000000010100p = 5d = 20frame 3 + d → physical 12308011000000010100

20500 = 5·4096 + 20 → frame 3: 12288 + 20 = 12308.

virtual address 81960010000000000100p = 2d = 4frame 6 + d → physical 24580110000000000100

8196 = 2·4096 + 4 → 6·4096 + 4 = 24580 (Tanenbaum Figure 3-10).

virtual address 327801000000000001100p = 8d = 12page 8 absent → page fault (trap)

32780 = 8·4096 + 12; page 8 is unmapped (present/absent bit 0) → page fault; the OS takes over.

64 KiB virtual address space (16 pages), 32 KiB physical memory (8 frames → 3-bit frame number).

Paging

Page Size and Internal Fragmentation

  • Example: page 2,048 B, process 72,766 B → 35 pages + 1,086 B → 36 frames, 962 B of internal fragmentation in the last page.
  • Worst-case internal fragmentation is 1 page − 1 byte; on average, half a page.
  • Small pages: little waste, but large page tables and many TLB misses. Large pages: efficient disk I/O, fewer table entries.
  • Overhead = s·e/p + p/2 (s process size, e entry size). Setting the derivative to zero: p = √(2se); s = 1 MiB, e = 8 B → p = 4096.
  • Today: x86-64 4 KiB (+ 2 MiB, 1 GiB huge pages), ARM64 4/16/64 KiB, Apple Silicon 16 KiB.
10 K20 K30 K5121 K2 K4 K8 K16 K32 K64 Kminimum: 4 KiBpage size p (log scale)

Paging

Free Frames

beforefree-frame list:14, 13, 18, 20, 15new process: 4 pagespage 0page 1page 2page 3131415161718192021afterfree list: 15page table01411321832013page 114page 015161718page 21920page 321
  • The OS keeps a frame table: for each physical frame, whether it is free and, if not, which page of which process it holds.
  • When a user process passes an address (e.g., a read buffer), the kernel translates it to a physical address through the process's page table.
  • On a context switch the dispatcher activates the new process's page table (CR3 on x86): context-switch time increases.

Paging

The Page-Table Entry

NX63PK62–59—58–52frame number51–12—11–9G8PAT7D6A5PCD4PWT3U/S2R/W1P0x86-64 page-table entry (64 bits, 4 KiB page)
BitMeaning (Tanenbaum's name)Who writes it?
PPresent/absent: if 0, an access causes a page faultOS
R/W, U/S, NXProtection: writable? accessible in user mode? executable?OS
AAccessed (referenced): set on a read or write; used by page replacementhardware (the OS clears it)
DDirty (modified): set on a write; a dirty page is written to disk when evictedhardware
PCD, PWT, PATCache disable / write policy: for memory-mapped I/OOS
GGlobal: not flushed from the TLB on a context switch (kernel pages)OS

The disk address isn't kept in the PTE: when the page isn't in memory, the entry's remaining bits belong to the OS (Linux encodes the swap entry there).

Paging · Step by step

The Valid–Invalid Bit

process: 0 … 10,468page 0page 1page 2page 3page 4page 5page 6page 7page table02v13v24v37v48v59v60i70i14-bit address: 0 … 16,3832 KiB pages → 8 pagesv = valid, i = invalid

The process uses 0–10,468: pages 0–5 are valid (frames 2, 3, 4, 7, 8, 9); pages 6 and 7 are invalid.

process: 0 … 10,468page 0page 1page 2page 3page 4page 5page 6page 7page table02v13v24v37v48v59v60i70i14-bit address: 0 … 16,3832 KiB pages → 8 pagesv = valid, i = invalidaddress 3,000= 1 × 2048 + 952 → page 1valid → frame 3physical = 3 × 2048 + 952 = 7,096

3,000: page 1, offset 952 → frame 3 → 7,096.

process: 0 … 10,468page 0page 1page 2page 3page 4page 5page 6page 7page table02v13v24v37v48v59v60i70i14-bit address: 0 … 16,3832 KiB pages → 8 pagesv = valid, i = invalidaddress 10,468= 5 × 2048 + 228 → page 5valid → frame 9physical = 9 × 2048 + 228 = 18,660

10,468: inside page 5 → 18,660. The process's last address.

process: 0 … 10,468page 0page 1page 2page 3page 4page 5page 6page 7page table02v13v24v37v48v59v60i70i14-bit address: 0 … 16,3832 KiB pages → 8 pagesv = valid, i = invalidaddress 12,000= 5 × 2048 + 1760 → page 5valid → frame 9physical = 9 × 2048 + 1760 = 20,192

12,000 lies outside the program but inside page 5 (10,240–12,287) → the hardware doesn't catch it: 20,192. The price of page granularity, i.e., internal fragmentation.

process: 0 … 10,468page 0page 1page 2page 3page 4page 5page 6page 7page table02v13v24v37v48v59v60i70i14-bit address: 0 … 16,3832 KiB pages → 8 pagesv = valid, i = invalidaddress 12,500= 6 × 2048 + 212 → page 6invalid bit → trap (protection fault)

12,500: page 6, invalid → trap. The OS distinguishes an illegal access (SIGSEGV) from a page not yet loaded.

Instead of a full table, a page-table length register (PTLR) can be used: a page number beyond the length causes a trap (Silberschatz Figure 9.13).

Paging

Protection Bits

Read / write (R/W)

Code pages are mapped read-only; an attempt to write to code causes a protection fault.

User / supervisor (U/S)

Kernel pages are inaccessible in user mode. The same table can hold both kernel and process mappings.

No-execute (NX / XD)

Code cannot run from data, stack, or heap pages. OSes enforce W^X: a page is either writable or executable.

SMEP / SMAP

In kernel mode, prevents executing code in user pages (SMEP) and unauthorized access to user data (SMAP).

Protection keys (PKU)

A 4-bit key in the PTE plus a user register (PKRU) change a group of pages' permissions without a system call.

Valid / invalid

Access control at the coarsest level: if a page isn't in the address space at all, every access traps.

Protection is at page granularity: permissions are kept separately in each PTE and checked with the TLB entry on every access.

Paging

Shared Pages

  • Reentrant code that never modifies itself can be shared: if three users run the same editor, its code frames are in memory once; only the data pages differ.
  • Shared libraries (libc) are shared this way. When the owning process ends, the frame can't be freed at once: a reference count is kept.
  • fork(): parent and child share all pages with copy-on-write (COW); pages are marked read-only and copied on the first write.
  • Memory-mapped files (mmap) and shared memory (shm_open): two processes see the same frames and communicate without copying.
P1ed 1 → 3ed 2 → 4ed 3 → 6P1 data → 1P2ed 1 → 3ed 2 → 4ed 3 → 6P2 data → 7P3ed 1 → 3ed 2 → 4ed 3 → 6P3 data → 2physical memory (frame no. below)0P11P32ed 13ed 245ed 36P27

TLB

Where Is the Page Table? The TLB

CPUpdTLB (associative memory)pageframehitfdphysical addressPhysical memorymisspage table (in memory)PTBRentry loaded into TLB
  • The table is too large to keep in registers; it lives in memory and the PTBR (x86: CR3, ARM: TTBR0/1) points to its start. But then every access becomes two memory accesses.
  • TLB (translation look-aside buffer): a small, fast, associative (searched in parallel) cache inside the MMU; typically 64 L1 entries + 1–3 thousand L2 entries.
  • If entries are tagged with an ASID/PCID, the TLB needn't be flushed on a context switch.

TLB · Step by step

TLB Hits and Misses

access sequence (16 bits, 4 KiB pages)0x0010p = 00x2004p = 20x0020p = 00x5010p = 50x9000p = 90x2008p = 20xB004p = 110x0030p = 0TLB (4 entries, LRU; oldest to newest, left to right)

The page table is Tanenbaum's table from the MMU example (0→2, 2→6, 5→3, 9→5, 11→7). The TLB starts empty.

access sequence (16 bits, 4 KiB pages)0x0010p = 0miss0x2004p = 20x0020p = 00x5010p = 50x9000p = 90x2008p = 20xB004p = 110x0030p = 0TLB (4 entries, LRU; oldest to newest, left to right)0 → 2physical2·4096 + 16 = 0x2010

0x0010: page 0 absent → miss; read from the page table and written to the TLB.

access sequence (16 bits, 4 KiB pages)0x0010p = 0miss0x2004p = 2miss0x0020p = 00x5010p = 50x9000p = 90x2008p = 20xB004p = 110x0030p = 0TLB (4 entries, LRU; oldest to newest, left to right)0 → 22 → 6physical6·4096 + 4 = 0x6004

0x2004: page 2 absent → miss; read from the page table and written to the TLB.

access sequence (16 bits, 4 KiB pages)0x0010p = 0miss0x2004p = 2miss0x0020p = 0hit0x5010p = 50x9000p = 90x2008p = 20xB004p = 110x0030p = 0TLB (4 entries, LRU; oldest to newest, left to right)2 → 60 → 2physical2·4096 + 32 = 0x2020

0x0020: page 0 is in the TLB → hit; no page-table access.

access sequence (16 bits, 4 KiB pages)0x0010p = 0miss0x2004p = 2miss0x0020p = 0hit0x5010p = 5miss0x9000p = 90x2008p = 20xB004p = 110x0030p = 0TLB (4 entries, LRU; oldest to newest, left to right)2 → 60 → 25 → 3physical3·4096 + 16 = 0x3010

0x5010: page 5 absent → miss; read from the page table and written to the TLB.

access sequence (16 bits, 4 KiB pages)0x0010p = 0miss0x2004p = 2miss0x0020p = 0hit0x5010p = 5miss0x9000p = 9miss0x2008p = 20xB004p = 110x0030p = 0TLB (4 entries, LRU; oldest to newest, left to right)2 → 60 → 25 → 39 → 5physical5·4096 + 0 = 0x5000

0x9000: page 9 absent → miss; read from the page table and written to the TLB.

access sequence (16 bits, 4 KiB pages)0x0010p = 0miss0x2004p = 2miss0x0020p = 0hit0x5010p = 5miss0x9000p = 9miss0x2008p = 2hit0xB004p = 110x0030p = 0TLB (4 entries, LRU; oldest to newest, left to right)0 → 25 → 39 → 52 → 6physical6·4096 + 8 = 0x6008

0x2008: page 2 is in the TLB → hit; no page-table access.

access sequence (16 bits, 4 KiB pages)0x0010p = 0miss0x2004p = 2miss0x0020p = 0hit0x5010p = 5miss0x9000p = 9miss0x2008p = 2hit0xB004p = 11miss0x0030p = 0TLB (4 entries, LRU; oldest to newest, left to right)5 → 39 → 52 → 611 → 7physical7·4096 + 4 = 0x7004evicted: page 0

0xB004: page 11 absent → miss; read from the page table and written to the TLB; it's full, so the least recently used page, 0, is evicted.

access sequence (16 bits, 4 KiB pages)0x0010p = 0miss0x2004p = 2miss0x0020p = 0hit0x5010p = 5miss0x9000p = 9miss0x2008p = 2hit0xB004p = 11miss0x0030p = 0missTLB (4 entries, LRU; oldest to newest, left to right)9 → 52 → 611 → 70 → 2physical2·4096 + 48 = 0x2030evicted: page 5

0x0030: page 0 absent → miss; read from the page table and written to the TLB; it's full, so the least recently used page, 5, is evicted. Total: 2 hits / 8 accesses. In real programs locality pushes the ratio above 99%.

TLB · Step-by-step calculation

Effective Access Time

TLB lookup time ε = 20 ns, memory access t = 100 ns, hit ratio α.
EAT = (t + ε)·α + (2t + ε)·(1 − α) = 2t + ε − t·α

The formula: one memory access on a hit, two on a miss (table + data).

Hit: 20 + 100 = 120 ns; miss: 20 + 100 + 100 = 220 ns

The time for each case.

α = 80%: 120 × 0.80 + 220 × 0.20 = 140 ns (40% slowdown)

At 80% hits the average is 140 ns: good compared with 200 ns without a TLB, but still 40% slower than memory.

α = 98%: 120 × 0.98 + 220 × 0.02 = 122 ns

At 98% hits, 122 ns. In real workloads the ratio is usually above 99%.

Four-level table (x86-64): miss = 20 + 4·100 + 100 = 520 ns; α = 98% → 128 ns, α = 99% → 124 ns

With multilevel tables a miss is far more expensive; that's why hardware also caches the intermediate levels (a page-walk cache) and the TLB hit ratio matters even more.

TLB

TLB Management

TopicDescription
Hardware-managedx86-64, ARMv8, RISC-V: on a miss the MMU walks the page table itself (a page walk); the OS only builds the table.
Software-managedIn older RISCs such as MIPS, SPARC, and Alpha, a miss traps to the OS; the kernel finds the entry and writes it to the TLB. Not true of all RISCs: ARMv8 and RISC-V are hardware-managed.
Soft / hard missSoft miss: the page is in memory, just not in the TLB (ns). Hard miss: the page is on disk and needs I/O (µs–ms).
ASID / PCIDEntries are tagged with a process ID, so the TLB isn't flushed on a context switch. After Meltdown (2018), PCID reduces the cost of KPTI.
Global entriesKernel mappings are marked with the G bit and stay valid in all processes.
TLB shootdownOn multicore, when a mapping changes, the other cores' TLBs are invalidated with interrupts (IPIs); newer processors have broadcast instructions (AMD INVLPGB).
TLBASID 7 · page 0x400 → 0x1A2ASID 9 · page 0x400 → 0x3F0ASID — · page 0xFFFF8 → 0x001 · Gsame virtual page, two processes: entries told apart by ASIDG: valid in all processes (kernel)

Page-Table Structures

Hierarchical (Two-Level) Page Tables

  • A single-level table with 32 bits and 4 KiB pages has 220 entries × 4 B = 4 MiB, for every process, and it must be contiguous.
  • Single-level at 64 bits: 252 × 8 B = 32 PiB (≈ 33.6 million GiB). Even using 48 bits, 236 × 8 B = 512 GiB.
  • The solution: page the page table. 32-bit x86: 10 bits for the outer table (page directory), 10 bits for the second level, 12 bits of offset. What gets split is the virtual address, not the PTE.
  • Only regions in use get a second-level table: a process using 4 MiB of code + 4 MiB of data + 4 MiB of stack → 1 + 3 tables = 16 KiB (instead of 4 MiB).
PT110 bitPT210 bitoffset12 bit32-bit virtual addressouter table (1024)0: code1: data2⋮1023: stack2nd level: code2nd level: data2nd level: stackthe other 1021 outer entries: "absent"

Page-Table Structures · Step by step

A Two-Level Translation Example

virtual address 0x00403004 = 4,206,59600000000010000000011000000000100PT1 = 1PT2 = 3offset = 4outer table00–4 MiB14–8 MiB28–12 MiB3…

Virtual address 0x00403004 (4,206,596): between 4 MiB and 8 MiB, in the data region (Tanenbaum Figure 3-13).

virtual address 0x00403004 = 4,206,59600000000010000000011000000000100PT1 = 1PT2 = 3offset = 4outer table00–4 MiB14–8 MiB28–12 MiB3…

The top 10 bits, PT1 = 1: outer-table entry 1 points to the second-level table for the 4–8 MiB region. Each entry covers 4 MiB.

virtual address 0x00403004 = 4,206,59600000000010000000011000000000100PT1 = 1PT2 = 3offset = 4outer table00–4 MiB14–8 MiB28–12 MiB3…2nd-level table (4–8 MiB)00x0A210x1C02absent30x1F340x0B7

The next 10 bits, PT2 = 3: look at entry 3 of that table (the page covering bytes 12,288–16,383). With the present bit 0, it would be a page fault.

virtual address 0x00403004 = 4,206,59600000000010000000011000000000100PT1 = 1PT2 = 3offset = 4outer table00–4 MiB14–8 MiB28–12 MiB3…2nd-level table (4–8 MiB)00x0A210x1C02absent30x1F340x0B7frame 0x1F3physical 0x1F3004

The entry gives the frame number (0x1F3 here); offset = 4 is added → physical 0x1F3004. On a miss: 2 table accesses + 1 data access.

The frame numbers are illustrative. On 32-bit x86 the outer table's address is in CR3; the table at each level fills exactly one 4 KiB page (1024 × 4 B).

Page-Table Structures · Step through the code

Two-Level Translation: Code Trace

/* 32 bits: 10 + 10 + 12 bits */
uint32_t translate(uint32_t va) {
  uint32_t p1 = va >> 22;
  uint32_t p2 = (va >> 12) & 0x3FF;
  uint32_t d  = va & 0xFFF;
  pte_t e1 = outer[p1];
  if (!e1.p) page_fault(va);
  pte_t e2 = e1.table[p2];
  if (!e2.p) page_fault(va);
  return (e2.frame << 12) | d;
}
va = 0x00801A3C0000000010p10000000001p2101000111100d

Virtual address 0x00801A3C (just above 8 MiB). Only entry 2 of the outer table is valid; in its table, entry 1 → frame 0x2F.

va = 0x00801A3C0000000010p1 = 20000000001p2101000111100d

Top 10 bits: 0x00801A3C >> 22 = 2.

va = 0x00801A3C0000000010p1 = 20000000001p2 = 1101000111100d

Middle 10 bits: (va >> 12) & 0x3FF = 0x801 & 0x3FF = 1.

va = 0x00801A3C0000000010p1 = 20000000001p2 = 1101000111100d = 0xA3C

Low 12-bit offset: 0xA3C; it never changes during translation.

va = 0x00801A3C0000000010p1 = 20000000001p2 = 1101000111100d = 0xA3Couter[2]: P = 1

Outer-table entry 2 is valid (P = 1): it gives the frame of the second-level table. The first memory access.

va = 0x00801A3C0000000010p1 = 20000000001p2 = 1101000111100d = 0xA3Couter[2]: P = 1table[1]: frame 0x2F

Second-level entry 1: frame 0x2F, P = 1. The second memory access.

va = 0x00801A3C0000000010p1 = 20000000001p2 = 1101000111100d = 0xA3Couter[2]: P = 1table[1]: frame 0x2Fphysical = 0x0002FA3C

0x2F << 12 = 0x2F000; combined with the offset, physical 0x0002FA3C. The third access is the data.

va = 0x014030040000000101p1 = 50000000011p2 = 3000000000100d = 0x004

A second address, 0x01403004: p1 = 5, p2 = 3, offset = 0x004.

va = 0x014030040000000101p1 = 50000000011p2 = 3000000000100d = 0x004outer[5]: absentP = 0 → page fault (trap)

Outer-table entry 5 is invalid (P = 0): there is no second-level table at all for the 20–24 MiB region. The MMU traps; no memory was spent on a table.

Page-Table Structures

x86-64: Four- and Five-Level Paging

x86-64, 4 levels (48-bit virtual address, 256 TiB)sign63–48PML49 bitPDPT9 bitPD9 bitPT9 bitoffset12 bitx86-64, 5 levels (LA57: 57 bits, 128 PiB)sign63–57PML59PML49PDPT9PD9PT9offset12
  • Each table is 512 entries × 8 B = 4 KiB, exactly one page; a 9-bit index. 4 × 9 + 12 = 48 bits.
  • The top 16 bits must copy bit 47 (a canonical address): the lower half for user space, the upper half for the kernel.
  • 5 levels (Intel Ice Lake server, AMD Zen 4 and later) give 57-bit virtual addresses; Linux supports them since 4.14.
  • Hierarchical tables work at 64 bits too: since not all 64 address bits are used, today's x86-64 and ARMv8 use exactly such tables.
  • If the PS bit is 1 in an intermediate entry, the walk stops there: a 2 MiB huge page at the PD level, 1 GiB at the PDPT level.

Page-Table Structures · Step by step

A Four-Level Walk

VA = 0x00007FFE12345678PML4[255]PDPT[504]PD[145]PT[325]offset 0x678CR30x1A2BPML4 tablePDPT tablePD tablePT table

An address near the user stack. Its split (9+9+9+9+12): PML4 = 255, PDPT = 504, PD = 145, PT = 325, offset = 0x678.

VA = 0x00007FFE12345678PML4[255]PDPT[504]PD[145]PT[325]offset 0x678CR30x1A2BPML4 table[255] → 0x3C4frame 0x1A2BPDPT tablePD tablePT table

CR3 gives the frame of the PML4 table; entry 255 is read → the frame of the PDPT table.

VA = 0x00007FFE12345678PML4[255]PDPT[504]PD[145]PT[325]offset 0x678CR30x1A2BPML4 table[255] → 0x3C4frame 0x1A2BPDPT table[504] → 0x7D10frame 0x3C4PD tablePT table

PDPT[504] → the frame of the page directory (PD). Had PS = 1 here, a 1 GiB page would have been found.

VA = 0x00007FFE12345678PML4[255]PDPT[504]PD[145]PT[325]offset 0x678CR30x1A2BPML4 table[255] → 0x3C4frame 0x1A2BPDPT table[504] → 0x7D10frame 0x3C4PD table[145] → 0x2E95frame 0x7D10PT table

PD[145] → the last-level table. With PS = 1, a 2 MiB page.

VA = 0x00007FFE12345678PML4[255]PDPT[504]PD[145]PT[325]offset 0x678CR30x1A2BPML4 table[255] → 0x3C4frame 0x1A2BPDPT table[504] → 0x7D10frame 0x3C4PD table[145] → 0x2E95frame 0x7D10PT table[325] → 0x5A3F7frame 0x2E95physical 0x5A3F7678

PT[325] gives frame 0x5A3F7; the offset is added. A TLB miss costs 4 memory accesses + the data access; the frame values are examples.

Page-Table Structures

Huge Pages

TLB reach = entries × page size, 1536 entries4 KiB pages6 MiB2 MiB pages3 GiB(log scale)
  • Programs with large working sets (databases, virtual machines, AI) suffer constant TLB misses with 4 KiB pages.
  • Mapping 1 GiB with 4 KiB pages takes 262,144 PTEs = a 2 MiB table; with 2 MiB pages, only 512 PD entries (4 KiB).
  • Linux: hugetlbfs (preallocated 2 MiB / 1 GiB), transparent huge pages (THP, merged by the kernel automatically), and in recent versions mTHP (16–64 KiB intermediate sizes). Windows: large pages.
  • The cost: internal fragmentation, and the need to find 2 MiB of contiguous physical frames (the buddy system and compaction return here).
  • ARMv8's contiguous bit: 16 adjacent 4 KiB entries can be held in one TLB entry (64 KiB).

Page-Table Structures · Step by step

Hashed Page Tables

virtual address 0x1A0AB → p = 26 (0x1A), d = 0xABh(p) = p mod 8hash table0p=8 → f=4p=16 → f=612p=10 → f=7p=26 → f=3345p=13 → f=167p=31 → f=2

Each bucket is a chain of (virtual page, frame, next) entries that hash to the same value. Here, 8 buckets, h(p) = p mod 8.

virtual address 0x1A0AB → p = 26 (0x1A), d = 0xABh(p) = p mod 8h(26) = 2hash table0p=8 → f=4p=16 → f=612p=10 → f=7p=26 → f=3345p=13 → f=167p=31 → f=2

Page number 26 is hashed: bucket 2.

virtual address 0x1A0AB → p = 26 (0x1A), d = 0xABh(p) = p mod 8h(26) = 2hash table0p=8 → f=4p=16 → f=612p=10 → f=7p=26 → f=3345p=13 → f=167p=31 → f=2

The chain's first entry is p = 10: no match, move to the next.

virtual address 0x1A0AB → p = 26 (0x1A), d = 0xABh(p) = p mod 8h(26) = 2hash table0p=8 → f=4p=16 → f=612p=10 → f=7p=26 → f=3345p=13 → f=167p=31 → f=2frame 3physical 0x30AB

p = 26 matches → frame 3; physical = 3 × 4096 + 0xAB = 0x30AB.

  • Used for address spaces larger than 32 bits; the table's size is proportional to the number of pages in use, not to the address space.
  • Clustered variant: each entry covers several (e.g., 16) consecutive pages; suitable for sparse 64-bit spaces.

Page-Table Structures · Step by step

Inverted Page Tables

logical address: (pid = 2, p = 5, d = 0x123)inverted table: one entry per frame0pid 1, page 01pid 2, page 32pid 1, page 53pid 3, page 14pid 2, page 55pid 1, page 26pid 3, page 07pid 2, page 0frame

The table has one entry per physical frame, not per process: which page of which process is in that frame.

logical address: (pid = 2, p = 5, d = 0x123)inverted table: one entry per frame0pid 1, page 01pid 2, page 32pid 1, page 53pid 3, page 14pid 2, page 55pid 1, page 26pid 3, page 07pid 2, page 0frame

Entry 0: (pid 1, page 0) ≠ (2, 5), next.

logical address: (pid = 2, p = 5, d = 0x123)inverted table: one entry per frame0pid 1, page 01pid 2, page 32pid 1, page 53pid 3, page 14pid 2, page 55pid 1, page 26pid 3, page 07pid 2, page 0frame

Entry 1: (pid 2, page 3) ≠ (2, 5), next.

logical address: (pid = 2, p = 5, d = 0x123)inverted table: one entry per frame0pid 1, page 01pid 2, page 32pid 1, page 53pid 3, page 14pid 2, page 55pid 1, page 26pid 3, page 07pid 2, page 0frame

Entry 2: (pid 1, page 5) ≠ (2, 5), next.

logical address: (pid = 2, p = 5, d = 0x123)inverted table: one entry per frame0pid 1, page 01pid 2, page 32pid 1, page 53pid 3, page 14pid 2, page 55pid 1, page 26pid 3, page 07pid 2, page 0frame

Entry 3: (pid 3, page 1) ≠ (2, 5), next.

logical address: (pid = 2, p = 5, d = 0x123)inverted table: one entry per frame0pid 1, page 01pid 2, page 32pid 1, page 53pid 3, page 14pid 2, page 55pid 1, page 26pid 3, page 07pid 2, page 0framematch: index 4 = framephysical 0x4123

Entry 4 matches: the index itself is the frame number → 4 × 4096 + 0x123 = 0x4123. A linear search is expensive, so real systems use a hash table + TLB.

  • The table's size is proportional to physical memory, and there is one per system: 16 GiB RAM, 4 KiB frames → 4,194,304 entries; 32 MiB at 8 B per entry.
  • Drawbacks: shared memory is hard (one virtual page per frame); handling page faults needs separate per-process external tables. Examples: PowerPC/IBM POWER (hashed inverted table), IA-64.

Segmentation

Segmentation: Why?

  • A compiler builds many tables while compiling: source text, the symbol table, constants, the parse tree, the call stack. All of them grow and shrink.
  • In a one-dimensional address space, when one table fills up it collides with its neighbor, even if the neighbor is empty.
  • Segmentation: several independent, variable-length address spaces. An address = (segment number, offset).
  • It matches the programmer's view: main program, functions, stack, arrays, and symbol table are separate units.
(a) one-dimensional spacecall stackparse treeconstantssource textsymbol tablethe symbol table grewand hits the source text!(b) separate segmentssymbols0source1consts2tree3stack4each segment starts at 0 and grows on its own

Segmentation · Step by step

Translation with a Segment Table

segment tablelimitbase01,0001,40014006,30024004,30031,1003,20041,0004,700physical memorysegment 01,400segment 16,300segment 24,300segment 33,200segment 44,700

Each entry holds a segment's base (physical start) and limit (length); the table's location and size are in the STBR and STLR registers.

segment tablelimitbase01,0001,40014006,30024004,30031,1003,20041,0004,700physical memorysegment 01,400segment 16,300segment 24,300segment 33,200segment 44,700(s, d) = (2, 53)d < limit ? 53 < 400yes → base + d4,300 + 53 = 4,353

(2, 53): 53 < 400 → 4,300 + 53 = 4,353.

segment tablelimitbase01,0001,40014006,30024004,30031,1003,20041,0004,700physical memorysegment 01,400segment 16,300segment 24,300segment 33,200segment 44,700(s, d) = (3, 852)d < limit ? 852 < 1,100yes → base + d3,200 + 852 = 4,052

(3, 852): 852 < 1,100 → 3,200 + 852 = 4,052.

segment tablelimitbase01,0001,40014006,30024004,30031,1003,20041,0004,700physical memorysegment 01,400segment 16,300segment 24,300segment 33,200segment 44,700(s, d) = (1, 399)d < limit ? 399 < 400yes → base + d6,300 + 399 = 6,699

(1, 399): 399 < 400 → 6,300 + 399 = 6,699.

segment tablelimitbase01,0001,40014006,30024004,30031,1003,20041,0004,700physical memorysegment 01,400segment 16,300segment 24,300segment 33,200segment 44,700(s, d) = (0, 1,222)d < limit ? 1,222 < 1,000no → trap (addressing error)

(0, 1,222): segment 0 is only 1,000 bytes long → trap. Without the check, address 2,622, outside the segment, would be accessed.

Silberschatz's five-segment example. Segmentation is a separate base–limit pair for each segment.

Segmentation · Step by step

Segment Bits and a Downward-Growing Stack

segmentbasesizegrowsprotection00 code24,5763,072+r-x01 heap40,9602,048+rw-11 stack32,7682,048−rw-virtual address 1,00000001111101000segmentoffset (12 bits) = 1,000segment 00 → codeoffset < size ? 1,000 < 3,072yes → base + offset24,576 + 1,000 = 25,576physical memory24 K42 Kcodeheapstack25,576

1,000: top bits 00 → code. 1,000 < 3,072 → 24,576 + 1,000 = 25,576.

segmentbasesizegrowsprotection00 code24,5763,072+r-x01 heap40,9602,048+rw-11 stack32,7682,048−rw-virtual address 4,50001000110010100segmentoffset (12 bits) = 404segment 01 → heapoffset < size ? 404 < 2,048yes → base + offset40,960 + 404 = 41,364physical memory24 K42 Kcodeheapstack41,364

4,500: 01 → heap. Offset 4,500 − 4,096 = 404 → 40,960 + 404 = 41,364 (not address + base = 45,460).

segmentbasesizegrowsprotection00 code24,5763,072+r-x01 heap40,9602,048+rw-11 stack32,7682,048−rw-virtual address 16,00011111010000000segmentoffset (12 bits) = 3,712segment 11 → stackgrows down: negative offset = 3,712 − 4,096 = −384|−384| ≤ 2,048 → valid32,768 − 384 = 32,384physical memory24 K42 Kcodeheapstack32,384

16,000: 11 → stack. It grows down, so the maximum segment size (4,096) is subtracted from the offset: −384 → 32,384.

segmentbasesizegrowsprotection00 code24,5763,072+r-x01 heap40,9602,048+rw-11 stack32,7682,048−rw-virtual address 7,00001101101011000segmentoffset (12 bits) = 2,904segment 01 → heapoffset < size ? 2,904 < 2,048no → trap (protection fault)physical memory24 K42 Kcodeheapstack

7,000: 01 → heap, but offset 2,904 ≥ 2,048: the hardware traps; the OS kills the process with SIGSEGV.

A 14-bit virtual address (16 KiB): the top two bits select one of four segments, the other 12 give up to 4 KiB each. The grows bit enables the stack, the protection bits enable sharing: an r-x code segment can map to the same frames in many processes; writing to it is a protection fault.

Segmentation

Pros and Cons of Segmentation

Advantages

  • Growing and shrinking tables don't affect each other.
  • Segments can be compiled separately; changing one function doesn't change the others' addresses.
  • A library can be placed in a segment and shared.
  • Per-segment protection: code x, constants r, data rw.

Disadvantages

  • Variable length → external fragmentation (checkerboarding), compaction.
  • A segment must be entirely in memory; the fix is to page the segments (MULTICS, Intel 386).
(a)S0 4KS1 8KS2 5KS3 8KS4 7K(b)S0 4KS7 5K3KS2 5KS3 8KS4 7K(c)S0 4KS7 5K3KS2 5KS5 4K4KS4 7K(d)S0 4KS7 5KS2 5KS5 4KS4 7K7K(b)–(c) external fragmentation (checkerboarding), (d) compaction

Segmentation

Paging versus Segmentation

QuestionPagingSegmentation
Must the programmer be aware of the technique?NoYes
How many linear address spaces are there?1Many
Can the total address space exceed physical memory?YesYes
Can code and data be separated and protected separately?Yes, via page bits*Yes
Are tables whose size fluctuates easily accommodated?NoYes
Is sharing functions between users easy?With shared pages*Yes
FragmentationInternal (last page)External
Why was it invented?A large linear address space beyond physical memoryTo split programs and data into independent logical spaces; sharing, protection
paging: one linear space, equal pagessegmentation: separate logical spacescodedatastacksymbols

Adapted from Tanenbaum Figure 3-32. * Tanenbaum says "no" for paging here; modern R/W/NX bits and shared pages do the same job at page level.

Real Architectures

x86: Segmentation + Paging, and the Flat Model

selectoroffset (32 bits)logical address (selector : offset)GDT / LDTdescriptor: base, limit+linear addresssegmentationpagingCR3 →page tablesphysical
  • 8086 real mode: physical = segment × 16 + offset (20 bits, 1 MiB); no protection.
  • 80386 protected mode: a 16-bit selector (13-bit index, table select, RPL) picks an 8-byte descriptor from the GDT/LDT; base + offset = the linear address, followed by two-level paging. The hardware counterpart of the MULTICS idea.
  • Even on 32 bits, Linux and Windows used a flat model, giving every segment base 0 and limit 4 GiB: segmentation effectively off, protection via pages.
  • x86-64 long mode: the CS, DS, ES, SS bases are treated as 0 by hardware and limits aren't checked. Only the FS and GS bases are used: thread-local storage (TLS) in user space, per-CPU data in the kernel.

Real Architectures

A Note on ARMv8-A and RISC-V

ARMv8-A (AArch64)RISC-V
Page (granule)4, 16, or 64 KiB; Apple Silicon and some Android 15+ devices use 16 KiB4 KiB (+ 2 MiB, 1 GiB, 512 GiB huge pages)
Virtual address48 bits (4 levels with 4 KiB, L0–L3); 52 bits with LVA/LPA2Sv39 (3 levels), Sv48 (4), Sv57 (5)
Table baseTTBR0_EL1 lower half (user), TTBR1_EL1 upper half (kernel)satp (mode + ASID + root frame)
Process tagASID 8/16 bitASID (up to 16 bits)
ProtectionAP bits, UXN/PXN (no-execute), PAN (≈ SMAP)R/W/X/U bits, SUM
TLBHardware walk; A/D bits optionally by hardware (v8.1); contiguous bitHardware walk (most implementations); sfence.vma
sign extension (25)VPN[2] 9VPN[1] 9VPN[0] 9offset 12Sv39: 3 levels × 512 entries; satp holds the root table's frame and the ASID

Neither architecture has segmentation: a flat virtual address, multilevel page tables, a TLB, and ASIDs. The same ideas as x86-64, different names.

Implementation Issues

Separating Policy and Mechanism

user spacekernel spaceUser processExternal pagerFault handlerMMU handlerDisk1 page fault2 needed page3 request4 arrived5 here's the page6 map
  • Paging involves the OS at four points: process creation (the table is set up), context switch (the table is activated, TLB), page faults, and process exit (frames and swap space are released).
  • The mechanism is in the kernel: the machine-dependent MMU handler and the machine-independent page-fault handler. The policy (which page, when, where) lives in a user-space external pager (Mach).
  • Flexibility and modularity are gained; the price is crossing the user/kernel boundary many times.
  • Today's equivalents: Linux userfaultfd (live migration of virtual machines, CRIU), and user-space pagers in microkernels (seL4, Fuchsia).

Summary

Summary

TechniqueIdeaProsCons
Base–limitOne contiguous region + MMUSimple, fastExternal fragmentation, hard to share
Variable partitionsChoosing holes with first/best fitNo internal fragmentation50-percent rule, compaction
PagingFixed page ↔ frame, page tableNo external fragmentation; sharing, protectionTable size, double access
TLBTranslation cache, ASIDEAT ≈ memory timeContext switches, shootdowns
Hierarchical tablePage the table (x86-64: 4/5 levels)Sparse spaces are cheapMany accesses on a miss
Hashed / invertedOne entry per used page / frameSmall tableLookup, sharing is hard
Segmentation(segment, offset), segment tableLogical division, protectionExternal fragmentation; flat model on x86-64

Today's general-purpose systems: a flat virtual address space + multilevel paging + TLB + huge pages; the buddy system for physical frames, slab (SLUB) for kernel objects.

Exercises

Exercises

  1. The loop for (i = 0; i < 1000; i++) a[i] = 0; compiles to the four instructions below. With a linear page table and no TLB, how many memory accesses occur in total? With a TLB? (a starts at virtual 40,000, int is 4 B, pages are 4 KiB, the code fits in one page.)
    loop:  movl $0,(%edi,%eax,4)  /* a[i] = 0 */
           incl %eax              /* i++      */
           cmpl $1000,%eax
           jne  loop
  2. With the segment registers from the "Segment Bits" slide, where do virtual addresses 13,000 and 14,500 go?
  3. An allocator with 16 B headers does malloc(1000) three times from a 4,096 B heap. What is the free chunk's size? Can a fourth malloc(1040) be satisfied?
  4. What happens if free(p + 4) is called?
Answers
  1. Each iteration has 4 instruction fetches + 1 write = 5 accesses, each with one PTE read: 10 × 1,000 = 10,000. With a TLB, 5,000 accesses + 3 misses (the code page and pages 9 and 10, which the array spans) = 5,003.
  2. 13,000: segment 11, offset 712 → −3,384; |−3,384| > 2,048 → trap. 14,500: offset 2,212 → −1,884 → 32,768 − 1,884 = 30,884.
  3. 4,080 − 3 × 1,016 = 1,032 B. Since 1,040 > 1,032, no: the allocator grows the heap with sbrk or mmap.
  4. The allocator looks for the header at p + 4 − 16; there is no valid header there. Undefined behavior; with a magic-number check the program is aborted.

Summary

Review Questions

  1. With base = 300,040 and limit = 120,900, what happens on an access to address 420,940?
  2. Holes 100, 500, 200, 300, 600 K; requests 212, 417, 112, 426 K. Which algorithm places them all?
  3. With 32-bit addresses and 4 KiB pages, what are p and d for 0x12345678?
  4. In Tanenbaum's table (5 → 3), what physical address does virtual 20,500 translate to?
  5. What is the EAT for a single-level table with ε = 20 ns, t = 100 ns, α = 90%?
  6. Why can hierarchical page tables be used on 64-bit systems too? How many levels does x86-64 use?
  7. What is left of segmentation in x86-64 long mode?
  8. An empty direct-mapped cache with 4 sets and 64 B lines sees accesses to addresses 0, 128, 256, 0. How many hits?
Answers
  1. Base + limit = 420,940 is outside the range (the last valid is 420,939) → trap.
  2. Only best fit; in the others 426 K waits (external fragmentation).
  3. p = 0x12345 (74,565), d = 0x678 (1,656).
  4. 3 × 4096 + 20 = 12,308.
  5. 120 × 0.9 + 220 × 0.1 = 130 ns.
  6. Not all of the virtual address is used (48/57 bits); only regions in use have tables. 4 (48 bits) or 5 (57 bits, LA57).
  7. FS and GS, whose bases are used (TLS, per-CPU data); CS carries the privilege level.
  8. Blocks 0, 2, 4, 0 → sets 0, 2, 0, 0. Block 256 evicts 0 → 0 hits, 4 misses.

Summary

References

  • A. Silberschatz, P. B. Galvin, G. Gagne, Operating System Concepts, 10th ed., Wiley, 2018 (Chapter 9: Main Memory).
  • A. S. Tanenbaum, H. Bos, Modern Operating Systems, 4th ed. (2014) / 5th ed. (2023), Pearson (Chapter 3: Memory Management, §3.1–3.3, 3.5–3.7).
  • R. H. Arpaci-Dusseau, A. C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces (OSTEP), ostep.org: "Address Spaces", "Address Translation", "Segmentation", "Free-Space Management", "Paging", "TLBs", "Smaller Tables".
  • D. E. Knuth, The Art of Computer Programming, Vol. 1, §2.5 Dynamic Storage Allocation (the 50-percent rule).
  • Intel 64 and IA-32 Architectures Software Developer's Manual, Vol. 3A, Chapters 4–5 (Paging, Protection); Arm Architecture Reference Manual for A-profile (VMSAv8-64); RISC-V Privileged Specification (Sv39/48/57).
  • U. Drepper, "How To Write Shared Libraries", 2011; Linux kernel documentation: mm/, "Transparent Hugepage Support", userfaultfd.

The End

Chapter 8: Memory Management
Previous: Chapter 7: Deadlocks · Next: Chapter 9: Virtual Memory
1 / 1