Chapter 9: Virtual Memory

Operating Systems
Demand pagingPage faultsEffective access time Copy-on-writemmapFIFO and Belady OPT and LRUClock, NRU, agingWorking set, WSClock Allocation and thrashingPage size, TLB reachBuddy, slabLinux and Windows
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Demand paging

Explain the purpose of virtual memory; describe the valid–invalid bit, the steps in handling a page fault, and instruction restart.

Performance

Compute the effective access time (EAT) from the page-fault probability; explain the difference between HDD, SSD, and compressed swap.

COW and mmap

Trace copy-on-write after fork() and how memory-mapped files relate to the page cache.

Replacement

Run FIFO, OPT, LRU, second chance, clock, NRU, aging, working set, and WSClock by hand on a reference string.

Allocation and thrashing

Compare equal/proportional and local/global allocation; explain thrashing with the working set and PFF.

Design and real systems

Recognize page size, TLB reach, program structure, pinning, buddy/slab, and the approaches of Linux (MGLRU, zswap) and Windows.

Introduction

Paging and the TLB

  • Virtual address = (p, d); the page table maps p to a frame, and the offset d stays the same.
  • The TLB caches recent translations; its hit ratio is above 99%, and on a miss the page table is walked (4 or 5 levels on x86-64).
  • Each entry: frame number, valid (present), R (accessed), M (dirty), protection bits (R/W, U/S, NX).
  • If the entry is invalid, a page fault occurs; if the page is not in memory, it is brought in from the backing store, replacing a chosen victim page if needed.
pdvirtual addressTLBhitmisspage tablev: frame ffdphysical addressi: page fault→ kernel

Introduction

Why Virtual Memory?

  • Programs rarely use all their code: error-handling routines, rarely used options, arrays allocated larger than needed.
  • Swapping moves a process in its entirety to or from disk; for a large process this is expensive.
  • Virtual memory separates logical memory from physical memory; a process runs while only partly in memory.
  • Benefits: programs aren't limited by physical memory; fewer frames per process → more processes; less I/O for loading and swapping; easier sharing.
  • Bloated software needn't be split by hand (the old overlay technique); the OS does it at page granularity.
virtual memorypage 0page 1page 2page 3page 4page 5page 6page 7physical memoryp 0p 5p 2disk (swap)p1 p3 p4 p6 p7MMUmapping

Introduction

The Virtual Address Space

  • A process's logical view of memory: contiguous addresses starting at 0. Physical memory is made of scattered frames; the MMU does the mapping.
  • The stack grows down from the highest user address and the heap grows up; the unused space between is a hole and costs no pages or frames.
  • Until the stack or heap grows into a new page, that page needs no physical memory (a sparse address space).
  • x86-64: 48-bit virtual addresses (128 TiB for user space); 57 bits with 5-level paging. ASLR randomizes the regions' locations on every run.
  • Virtual memory can be built two ways: demand paging (universal today) and demand segmentation (historical: Burroughs, OS/2).
kernel (inaccessible)stack ↓holelibraries, mmapholeheap ↑data + bsscode (text)max0

A process's regions on Linux: cat /proc/self/maps; each line is a virtual memory area (VMA).

Introduction

Shared Pages and Libraries

  • Virtual memory makes it easy to share pages among processes: the same frame appears in several page tables.
  • Shared libraries (libc, libstdc++): read-only code pages are mapped at each process's own address, with one physical copy.
  • Interprocess communication through shared memory: two processes map the same region.
  • During fork() pages are shared rather than copied (copy-on-write).
  • Writable data pages, however, are private to each process.
process Astacklibc.soheapcodeprocess Bstacklibc.soheapcodephysical memorylibc codelibc codeA's dataB's data

Demand Paging

Demand Paging

  • No pages (or very few) are loaded when a process starts; a page is brought in only when it is accessed (lazy loading).
  • Less I/O, less memory, faster startup, more users.
  • On access: if the address is invalid (in no region), the process is terminated; if it is valid but not in memory, the page is brought in.
  • The component that deals with individual pages rather than whole processes is the pager; "lazy swapper" is an old and misleading name.
  • In pure demand paging even the first instruction faults; thanks to locality, the fault rate soon drops.
logicalpage tablephysicalA04vB1iC26vD3iE4iF59vG6iH7i01234A56C789FdiskABCDEFGH

Demand Paging

The Valid–Invalid Bit

  • One bit per entry: v = valid and in memory, i = not in memory or not in the address space. Telling the two apart is the OS's job.
page table0i1i2i3iVMA: 0–3physical memory012345

Initially every entry of the table is set to i (invalid/not in memory); no frames are allocated.

page table0i1i2i3iVMA: 0–3load p2physical memory012345trap:page fault

The CPU accesses page 2; the MMU sees i in the entry and raises a page-fault trap.

page table0i1i25v3iVMA: 0–3load p2physical memory012345p 2diskpage 2

The kernel verifies that the address lies in a valid region (VMA), reads the page into free frame 5, sets the entry to 5, v; the instruction reruns.

page table0i1i25v3iVMA: 0–3load p2physical memory012345p 2

Later accesses to the same page are now translated directly (and loaded into the TLB).

page table0i1i25v3iVMA: 0–3load p6physical memory012345p 2SIGSEGV

Page 6 is in no region: an invalid reference; the kernel terminates the process with SIGSEGV.

Demand Paging

Handling a Page Fault

operating systemprocessload Mpage tableibacking store(swap / file)physical memoryframe 7: free

The process accesses address M; the entry's bit in the page table is i.

operating systemprocessload Mpage tableibacking store(swap / file)physical memoryframe 7: free1

1. Reference: the MMU reads the entry; the page isn't in memory.

operating systemprocessload Mpage tableibacking store(swap / file)physical memoryframe 7: free12

2. Trap: the hardware enters the kernel; the kernel verifies the reference is valid (otherwise it terminates the process).

operating systemprocessload Mpage tableibacking store(swap / file)physical memoryframe 7: free123

3. Page is on backing store: the page's location on disk is found (swap space or a file), and a free frame is chosen.

operating systemprocessload Mpage tableibacking store(swap / file)physical memoryframe 7: page1234

4. Bring it in: a disk read is scheduled; the page is read into frame 7. Meanwhile the CPU goes to another process.

operating systemprocessload Mpage tablev7backing store(swap / file)physical memoryframe 7: page12345

5. Update the table: frame 7 is written into the entry and the bit set to v.

operating systemprocessload Mpage tablev7backing store(swap / file)physical memoryframe 7: page123456

6. Restart: the faulting instruction runs again; the process continues as if the page had always been there.

A redrawing of Silberschatz Figure 10.5. A page fault is a fault-type trap: the saved PC points to the faulting instruction, so it can be rerun. On x86 the faulting address is written to CR2. A dirty victim is written to disk first, with its frame marked busy meanwhile.

Demand Paging · Step through the code

Address Translation and Page Faults: Code Trace

int translate(int va) {
  int p = va >> 12;      /* page no */
  int d = va & 0xFFF;    /* offset */
  int f = tlb_find(p);   /* -1: miss */
  if (f < 0) {
    if (!pt[p].valid)    /* not in memory */
      page_fault(p);     /* from disk */
    f = pt[p].frame;
    tlb_add(p, f);
  }
  pt[p].R = 1;
  return (f << 12) | d;
}
VAp = –d = –→ PA–TLBpf0114Page tablepbitfR0v101v402v503i–0Diskp3

The TLB holds translations for pages 0 and 1; page 2 is in memory (frame 5) but not in the TLB; page 3 is on disk. First call: translate(0x2A10).

VAp = 2d = 0xA10→ PA–TLBpf0114Page tablepbitfR0v101v402v503i–0Diskp3

The upper bits are the page number, the low 12 bits the offset: 0x2A10 → p = 2, d = 0xA10 (4 KB pages).

VAp = 2d = 0xA10→ PA–TLBpf0114Page tablepbitfR0v101v402v503i–0Diskp3

p = 2 isn't in the TLB: a miss, f = −1. Translation goes to the page table (a memory access).

VAp = 2d = 0xA10→ PA–TLBpf0114Page tablepbitfR0v101v402v503i–0Diskp3

Entry 2's bit is v: the page is in memory, no page fault; line 7 is skipped.

VAp = 2d = 0xA10→ PA–TLBpf011425Page tablepbitfR0v101v402v503i–0Diskp3

f = 5 is read from the table and (2, 5) is added to the TLB: the next access to this page is translated without a table walk.

VAp = 2d = 0xA10→ PA0x5A10TLBpf011425Page tablepbitfR0v101v402v513i–0Diskp3

The R bit is set (replacement algorithms use it); physical address (5 << 12) | 0xA10 = 0x5A10.

VAp = 2d = 0xB04→ PA–TLBpf011425Page tablepbitfR0v101v402v513i–0Diskp3

translate(0x2B04): p = 2, d = 0xB04. A TLB hit, f = 5; the page table isn't touched.

VAp = 2d = 0xB04→ PA0x5B04TLBpf011425Page tablepbitfR0v101v402v513i–0Diskp3

A second access to the same page: the if block is skipped, result 0x5B04. Thanks to locality, most accesses follow this path.

VAp = 3d = 0x008→ PA–TLBpf011425Page tablepbitfR0v101v402v513i–0Diskp3

translate(0x3008): p = 3, d = 0x008. Not in the TLB: a miss, f = −1.

VAp = 3d = 0x008→ PA–TLBpf011425Page tablepbitfR0v101v402v513i–0Diskp3page faultp3 → frame 7

Entry 3's bit is i: a page fault. The kernel reads the page from disk into free frame 7; the process waits meanwhile.

VAp = 3d = 0x008→ PA–TLBpf01142537Page tablepbitfR0v101v402v513v70Disk–

The fault handler set the entry to (v, 7); when the instruction reruns, f = 7 is read and added to the TLB.

VAp = 3d = 0x008→ PA0x7008TLBpf01142537Page tablepbitfR0v101v402v513v71Disk–

Result: 0x7008. Three translations: 1 TLB hit, 2 misses, one of them a page fault (disk I/O).

Demand Paging

A Page Fault: Timeline

The textbook's 15 steps fall into three major costs: servicing the trap (µs), reading the page (ms on HDD, 10–100 µs on NVMe), and restarting the process (µs).

process Akerneldiskprocess Btime →runningtrap, save, check

1–5. Trap to the kernel; registers and process state are saved; the interrupt is found to be a page fault and the reference legal; the page's location on disk is found.

process Akerneldiskprocess Btime →runningtrap, save, checkrequest I/Oqueue + readB runningA waiting (blocked)

6–9. A read request goes on the disk/SSD queue; the device is awaited (seek + rotational latency on an HDD). Meanwhile the CPU goes to another process (B).

process Akerneldiskprocess Btime →runningtrap, save, checkrequest I/Oqueue + readB runninginterrupt, PTA waiting (blocked)

10–13. The I/O-complete interrupt arrives; B's state is saved, the interrupt is identified as coming from the disk, the page table is updated, and A is put on the ready queue.

process Akerneldiskprocess Btime →runningtrap, save, checkrequest I/Oqueue + readB runninginterrupt, PTrestartA waiting (blocked)

14–15. When A gets the CPU again, its registers and page table are reloaded; the interrupted instruction runs from the start.

Demand Paging

Instruction Restart and Fault Types

The restart problem

  • A fault may occur while fetching the instruction or reading an operand; in most cases rerunning the instruction suffices.
  • The hard case: the instruction faults after modifying some state. E.g., an IBM 360 MVC block move crosses a page boundary; autoincrement addressing has already changed a register.
  • On the Motorola 680x0, if the three-word MOVE.L 6(A1),2(A0) faults while reading an operand, the OS can't tell where the instruction began; some CPUs therefore copy the PC to a hidden register before each instruction.
  • Solutions: probe both ends before the instruction starts; keep old values in temporary registers and roll back. On modern CPUs precise exceptions guarantee this in hardware.

Fault types (Linux)

TypeWhat happens?
MinorThe page is already in memory (page cache, shared library, zero page, COW): only the table is updated, no I/O.
MajorThe page must be read from disk/swap: I/O and waiting.
InvalidNo such region, or a permission violation: SIGSEGV.
$ /usr/bin/time -v ./prog | grep -i fault
$ ps -o min_flt,maj_flt -p PID
$ perf stat -e page-faults,major-faults ./prog

Performance

Effective Access Time (EAT)

Formula

EAT = (1 − p) × ma + p × page-fault time

ma: memory access (100 ns); fault time: trap + read + restart; p = 0 → no faults. This covers faults that read the page from disk/swap (major); a minor fault, where the page is already in memory and only the table is updated, is far cheaper.

HDD: 25 ms = 25,000,000 ns

EAT = 100(1 − p) + 25,000,000 p = 100 + 24,999,900 p

p = 0.001 → EAT = 25,099.9 ns ≈ 25 µs

One fault per thousand accesses slows memory down about 251×.

Goal: at most 10% slowdown

110 > 100 + 24,999,900 p → p < 4.0 × 10⁻⁷

i.e., fewer than one fault per 2.5 million accesses

p < 0.0000004: faults must be extremely rare.

2026: NVMe 10–100 µs, zram ≈ 10 µs

NVMe (at 100 µs), p = 0.001 → EAT ≈ 199.9 ns; for 10%, p < 1.0 × 10⁻⁴

zram, p = 0.001 → EAT ≈ 109.9 ns

Fast storage cuts the penalty 250–2500×, but memory is still hundreds of times faster, so replacement algorithms still matter.

100 ns1 µs10 µs100 µs1 ms10−710−610−510−410−310−2page-fault probability pHDD 25 msNVMe 100 µszram 10 µs– – 10% slowdown limit (110 ns)

Silberschatz 10e uses 200 ns and 8 ms: p = 0.001 → 8.2 µs (41×). zram/zswap keep the page compressed in RAM; a "read" is a decompression.

Demand Paging

Where Do Pages Come From?

  • File-backed pages (code, read-only data, mmaped files): if clean they are dropped and reread from the file when needed.
  • Anonymous pages (heap, stack, modified data): they have no file and must be written to swap space before being dropped.
  • Zero-fill-on-demand: bss and new heap pages are given a zeroed frame on first access; this is essential so old data doesn't leak.
  • The OS keeps a free-frame pool so no search is needed at fault time. When the pool runs low, reclaim starts.
  • Swap space can be a separate partition or a file; on Linux, swapon --show.
address spacestackheap (anonymous)bssdatacodeexecutablefile (ELF)swap space(swap)free-frame listzero-fill

Demand Paging

The Backing Store (Swap Space)

(a) static swap spacememoryp0p3p4p6page tablep0p1p2p3p4p5p6p7diskp0p1p2p3p4p5p6p7a contiguous area per process: address = base + page × size(b) dynamic swap spacememoryp0p3p4p6page tablep0p1p2p3p4p5p6p7diskp1p7p2p5disk mapa disk address is kept per page
  • Static: when the process starts, contiguous swap space of its full size is allocated; the start address is in the process table. Code, data, and stack may have separate areas; the stack can grow.
  • Dynamic: nothing is allocated in advance; when a page goes out a slot is found and its address kept in a per-page disk map. Unmodified code pages are never swapped; they are reread from the executable.
  • Location: a separate disk/partition (a Linux swap partition) or a file in the file system (swapfile, Windows pagefile.sys).

Copy-on-Write

Copy-on-Write

  • fork() doesn't copy the parent's pages; parent and child share them, and writable pages are marked read-only + COW in both.
  • A write by either causes a protection fault; the kernel copies that one page and maps the writable copy into the writer.
  • Unmodified pages (code, read-only data) stay shared; fork() + exec() is almost free.
  • The copy's frame comes from the zero-fill pool.
  • vfork() doesn't even copy the page table (the parent waits); posix_spawn() is preferred today.
after fork()process 1process 2page Apage Bpage Cprocess 1 wrote Cprocess 1process 2page Apage Bpage Ccopy of C

The unit of sharing is the page, not the file: only the written page is copied; the others stay shared.

Copy-on-Write · Step through the code

fork() and COW, Step by Step

int x = 1;                 /* data: D */
int main(void) {
  char *b = malloc(4096);  /* heap: H */
  b[0] = 'a';
  pid_t pid = fork();
  if (pid == 0) {          /* child */
    printf("%d\n", x);    /* read */
    x = 2;                 /* write */
    _exit(0);
  }
  wait(NULL);
  b[0] = 'b';              /* parent */
  return 0;
}
parentchildframesf1: Dref=1f2: Href=1f3: —freeDRWHRW

The parent uses pages D (data) and H (heap) as writable.

parentchildframesf1: Dref=2f2: Href=2f3: —freeDROHRODROHRO

fork(): only the page tables are copied; both sides are read-only (RO), reference counts 2.

parentchildframesf1: Dref=2f2: Href=2f3: —freeDROHRODROHRO

The child reads x: reading is allowed, so no fault and no copy.

parentchildframesf1: Dref=1f2: Href=2f3: D'ref=1DROHRODRWHRO

The child writes x = 2 → protection fault. The kernel recognizes COW: it allocates f3, copies D, and maps it RW into the child.

parentchildframesf1: Dref=1f2: Href=1f3: —freeDROHRO

The child exits: its mappings are removed, f3 is freed, and f2's reference count drops to 1.

parentchildframesf1: Dref=1f2: Href=1f3: —freeDROHRW

The parent writes H → fault; the reference count is 1, so no copy is needed; the page is simply made writable.

Memory-Mapped Files

Memory-Mapped Files

  • mmap() maps a file into a region of the address space; the file is read and written through memory accesses instead of read()/write().
  • Pages are brought in on demand; the first access causes a (minor or major) page fault. Dirty pages are written to the file in the background or by msync()/munmap().
  • MAP_SHARED: changes are visible in the file and to everyone mapping it → IPC via shared memory.
  • MAP_PRIVATE: writes go to a private copy via COW; the file is unchanged (this is how executables' data sections are loaded).
  • MAP_ANONYMOUS: a region with no file (large malloc blocks). Windows: MapViewOfFile.
process Apage 0page 1page 2process Bpage 0page 1page 2page cacheframe 3frame 8frame 5data.bin (disk)block 0block 1block 2read / write back

Memory-Mapped Files · Step through the code

mmap, Step by Step

int fd = open("data.bin", O_RDWR);
struct stat st;
fstat(fd, &st);          /* 12 KiB = 3 pages */
char *p = mmap(NULL, st.st_size,
     PROT_READ | PROT_WRITE,
     MAP_SHARED, fd, 0);
close(fd);               /* mapping stays */
char c = p[0];           /* page 0 */
p[5000] = 'x';           /* page 1 */
msync(p, st.st_size, MS_SYNC);
munmap(p, st.st_size);
virtual pagespage cachep0: —absentp1: —absentp2: —absent

The file is opened and its size found: 12 KiB, i.e., 3 pages.

virtual pagespage cachep0: iabsentp1: iabsentp2: iabsent

mmap only creates a VMA: the table entries are empty and no page has been read yet.

virtual pagespage cachep0: iabsentp1: iabsentp2: iabsent

close(fd) doesn't break the mapping; the VMA keeps its own reference to the file.

virtual pagespage cachep0: vblock 0p1: iabsentp2: iabsent

p[0] → page fault; block 0 is read into the page cache (a major fault), and the entry becomes valid.

virtual pagespage cachep0: vblock 0p1: vblock 1 dirtyp2: iabsent

p[5000]: 5000 / 4096 → page 1; fault, read, write → the page is dirty (M=1).

virtual pagespage cachep0: vblock 0p1: vblock 1p2: iabsent

msync writes the dirty page to the file; M is cleared. Page 2 was never touched, never read.

virtual pagespage cachep0: —block 0p1: —block 1p2: —absent

munmap removes the VMA; the pages may stay in the cache, and another process opening the file uses them with a minor fault.

Page Replacement

No Free Frame: Page Replacement

  • Memory is usually full (free RAM is wasted RAM). Options: kill the process (unacceptable), swap out a whole process, or replace a single page.
page tablevictimv…desirediphysical memoryframe f: victimbacking storevictimdesired

A page fault: the desired page was found on disk, but there is no free frame.

page tablevictimv…desirediphysical memoryframe f: victimbacking storevictimdesired1

1. The replacement algorithm picks a victim frame.

page tablevictimv…desirediphysical memoryframe f: victimbacking storevictimdesired12only if M = 1

2. If the victim is dirty (M = 1) it is written to swap/file; if clean, no write is needed: I/O is halved.

page tablevictimi…desirediphysical memoryframe f: victimbacking storevictimdesired12only if M = 13

3. The victim's entry is set to i (and removed from the TLB: a TLB shootdown on multicore).

page tablevictimi…desirediphysical memoryframe f: desiredbacking storevictimdesired12only if M = 134

4. The desired page is read into the freed frame.

page tablevictimi…desiredvphysical memoryframe f: desiredbacking storevictimdesired12only if M = 1345

5. The new entry is set to v; the trapping instruction is restarted.

Two decisions: frame allocation (how many frames per process?) and replacement (which page goes?). Goal: fewest page faults; avoid the same page going back and forth.

Page Replacement

Evaluating the Algorithms

  • An algorithm is run on a reference string (page numbers only) and its page faults are counted.
  • It is derived from an address trace; consecutive references to the same page count once. With 100-byte pages, 0100 0432 0101 0612 0102 … → 1 4 1 6 1 6 1 6 1 6 1.
  • This chapter's common string (Silberschatz), 3 frames:

7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1

  • Expected: faults decrease as frames increase; the initial loads (here 6 distinct pages: 7, 0, 1, 2, 3, 4) are unavoidable.
  • Algorithms: OPT, FIFO, LRU, second chance, clock, NRU, NFU/aging, working set, WSClock.
051015201234567number of framesnumber of faultsFIFOLRUOPT

Page Replacement · Step by step

FIFO: First In, First Out

  • Pages are kept in a queue in load order; the most recently loaded is at the tail.
  • When space is needed, the oldest page, at the head of the queue, leaves.
  • Easy to implement; needs no bits.
  • Ignores usage entirely: the oldest page may be the most heavily used.
reference70120304230321201701frame 0frame 1frame 27faultqueue (old → new):7

1. reference 7: fault; loaded into free frame 0. Faults: 1

reference70120304230321201701frame 0frame 1frame 2770faultqueue (old → new):70

2. reference 0: fault; loaded into free frame 1. Faults: 2

reference70120304230321201701frame 0frame 1frame 2770701faultqueue (old → new):701

3. reference 1: fault; loaded into free frame 2. Faults: 3

reference70120304230321201701frame 0frame 1frame 2770701201faultqueue (old → new):012

4. reference 2: fault; the oldest page, 7, leaves the head and 2 joins the tail. Faults: 4

reference70120304230321201701frame 0frame 1frame 2770701201201faultqueue (old → new):012

5. reference 0: in memory → hit; the FIFO order is unchanged.

reference70120304230321201701frame 0frame 1frame 2770701201201231faultqueue (old → new):123

6. reference 3: fault; the oldest page, 0, leaves the head and 3 joins the tail. Faults: 5

reference70120304230321201701frame 0frame 1frame 2770701201201231230faultqueue (old → new):230

7. reference 0: fault; the oldest page, 1, leaves the head and 0 joins the tail. Faults: 6

reference70120304230321201701frame 0frame 1frame 2770701201201231230430faultqueue (old → new):304

8. reference 4: fault; the oldest page, 2, leaves the head and 4 joins the tail. Faults: 7

reference70120304230321201701frame 0frame 1frame 2770701201201231230430420faultqueue (old → new):042

9. reference 2: fault; the oldest page, 3, leaves the head and 2 joins the tail. Faults: 8

reference70120304230321201701frame 0frame 1frame 2770701201201231230430420423faultqueue (old → new):423

10. reference 3: fault; the oldest page, 0, leaves the head and 3 joins the tail. Faults: 9

reference70120304230321201701frame 0frame 1frame 2770701201201231230430420423023faultqueue (old → new):230

11. reference 0: fault; the oldest page, 4, leaves the head and 0 joins the tail. Faults: 10

reference70120304230321201701frame 0frame 1frame 2770701201201231230430420423023023faultqueue (old → new):230

12. reference 3: in memory → hit; the FIFO order is unchanged.

reference70120304230321201701frame 0frame 1frame 2770701201201231230430420423023023023faultqueue (old → new):230

13. reference 2: in memory → hit; the FIFO order is unchanged.

reference70120304230321201701frame 0frame 1frame 2770701201201231230430420423023023023013faultqueue (old → new):301

14. reference 1: fault; the oldest page, 2, leaves the head and 1 joins the tail. Faults: 11

reference70120304230321201701frame 0frame 1frame 2770701201201231230430420423023023023013012faultqueue (old → new):012

15. reference 2: fault; the oldest page, 3, leaves the head and 2 joins the tail. Faults: 12

reference70120304230321201701frame 0frame 1frame 2770701201201231230430420423023023023013012012faultqueue (old → new):012

16. reference 0: in memory → hit; the FIFO order is unchanged.

reference70120304230321201701frame 0frame 1frame 2770701201201231230430420423023023023013012012012faultqueue (old → new):012

17. reference 1: in memory → hit; the FIFO order is unchanged.

reference70120304230321201701frame 0frame 1frame 2770701201201231230430420423023023023013012012012712faultqueue (old → new):127

18. reference 7: fault; the oldest page, 0, leaves the head and 7 joins the tail. Faults: 13

reference70120304230321201701frame 0frame 1frame 2770701201201231230430420423023023023013012012012712702faultqueue (old → new):270

19. reference 0: fault; the oldest page, 1, leaves the head and 0 joins the tail. Faults: 14

reference70120304230321201701frame 0frame 1frame 2770701201201231230430420423023023023013012012012712702701faultqueue (old → new):701

20. reference 1: fault; the oldest page, 2, leaves the head and 1 joins the tail. Faults: 15 Total: 15 faults (3 initial loads + 12 replacements).

Red cell: page loaded by a fault · green: hit · red dot: page fault. FIFO tracks loading, not access: the most recently loaded page is at the end of the list.

Page Replacement · Step by step

Belady's Anomaly

More frames should mean fewer faults… not always with FIFO. String: 1 2 3 4 1 2 5 1 2 3 4 5

3 frames — faults: 1reference123412512345frame 0frame 1frame 21fault4 frames — faults: 1reference123412512345frame 0frame 1frame 2frame 31fault

1. reference 1 — 3 frames: fault · 4 frames: fault.

3 frames — faults: 2reference123412512345frame 0frame 1frame 2112fault4 frames — faults: 2reference123412512345frame 0frame 1frame 2frame 3112fault

2. reference 2 — 3 frames: fault · 4 frames: fault.

3 frames — faults: 3reference123412512345frame 0frame 1frame 2112123fault4 frames — faults: 3reference123412512345frame 0frame 1frame 2frame 3112123fault

3. reference 3 — 3 frames: fault · 4 frames: fault.

3 frames — faults: 4reference123412512345frame 0frame 1frame 2112123423fault4 frames — faults: 4reference123412512345frame 0frame 1frame 2frame 31121231234fault

4. reference 4 — 3 frames: fault (1 out) · 4 frames: fault.

3 frames — faults: 5reference123412512345frame 0frame 1frame 2112123423413fault4 frames — faults: 4reference123412512345frame 0frame 1frame 2frame 311212312341234fault

5. reference 1 — 3 frames: fault (2 out) · 4 frames: hit.

3 frames — faults: 6reference123412512345frame 0frame 1frame 2112123423413412fault4 frames — faults: 4reference123412512345frame 0frame 1frame 2frame 3112123123412341234fault

6. reference 2 — 3 frames: fault (3 out) · 4 frames: hit.

3 frames — faults: 7reference123412512345frame 0frame 1frame 2112123423413412512fault4 frames — faults: 5reference123412512345frame 0frame 1frame 2frame 31121231234123412345234fault

7. reference 5 — 3 frames: fault (4 out) · 4 frames: fault (1 out).

3 frames — faults: 7reference123412512345frame 0frame 1frame 2112123423413412512512fault4 frames — faults: 6reference123412512345frame 0frame 1frame 2frame 311212312341234123452345134fault

8. reference 1 — 3 frames: hit · 4 frames: fault (2 out).

3 frames — faults: 7reference123412512345frame 0frame 1frame 2112123423413412512512512fault4 frames — faults: 7reference123412512345frame 0frame 1frame 2frame 3112123123412341234523451345124fault

9. reference 2 — 3 frames: hit · 4 frames: fault (3 out).

3 frames — faults: 8reference123412512345frame 0frame 1frame 2112123423413412512512512532fault4 frames — faults: 8reference123412512345frame 0frame 1frame 2frame 31121231234123412345234513451245123fault

10. reference 3 — 3 frames: fault (1 out) · 4 frames: fault (4 out).

3 frames — faults: 9reference123412512345frame 0frame 1frame 2112123423413412512512512532534fault4 frames — faults: 9reference123412512345frame 0frame 1frame 2frame 311212312341234123452345134512451234123fault

11. reference 4 — 3 frames: fault (2 out) · 4 frames: fault (5 out).

3 frames — faults: 9reference123412512345frame 0frame 1frame 2112123423413412512512512532534534fault4 frames — faults: 10reference123412512345frame 0frame 1frame 2frame 3112123123412341234523451345124512341234523fault

12. reference 5 — 3 frames: hit · 4 frames: fault (1 out). Result: 9 < 10.

048121234567number of frames10 > 9

Stack algorithms (OPT, LRU): the set of pages in n frames is always a subset of the set in n+1 frames → the anomaly cannot occur.

Page Replacement · Step by step

OPT: Replace the Page Not Used for the Longest Time

  • Replace the page that will be used furthest in the future (or never) (Belady, 1966).
  • Easy to define, impossible to implement: the OS can't know the future (a crystal ball).
  • It is a lower bound: used to compare other algorithms.
  • On a real system it can be computed only by running a trace twice.
reference70120304230321201701frame 0frame 1frame 27faultnext use:7: t=18

1. reference 7: fault; loaded into free frame 0. Faults: 1

reference70120304230321201701frame 0frame 1frame 2770faultnext use:7: t=180: t=5

2. reference 0: fault; loaded into free frame 1. Faults: 2

reference70120304230321201701frame 0frame 1frame 2770701faultnext use:7: t=180: t=51: t=14

3. reference 1: fault; loaded into free frame 2. Faults: 3

reference70120304230321201701frame 0frame 1frame 2770701201faultnext use:2: t=90: t=51: t=14

4. reference 2: fault; next uses (7→18, 0→5, 1→14); the farthest, 7, leaves. Faults: 4

reference70120304230321201701frame 0frame 1frame 2770701201201faultnext use:2: t=90: t=71: t=14

5. reference 0: in memory → hit.

reference70120304230321201701frame 0frame 1frame 2770701201201203faultnext use:2: t=90: t=73: t=10

6. reference 3: fault; next uses (2→9, 0→7, 1→14); the farthest, 1, leaves. Faults: 5

reference70120304230321201701frame 0frame 1frame 2770701201201203203faultnext use:2: t=90: t=113: t=10

7. reference 0: in memory → hit.

reference70120304230321201701frame 0frame 1frame 2770701201201203203243faultnext use:2: t=94: none3: t=10

8. reference 4: fault; next uses (2→9, 0→11, 3→10); the farthest, 0, leaves. Faults: 6

reference70120304230321201701frame 0frame 1frame 2770701201201203203243243faultnext use:2: t=134: none3: t=10

9. reference 2: in memory → hit.

reference70120304230321201701frame 0frame 1frame 2770701201201203203243243243faultnext use:2: t=134: none3: t=12

10. reference 3: in memory → hit.

reference70120304230321201701frame 0frame 1frame 2770701201201203203243243243203faultnext use:2: t=130: t=163: t=12

11. reference 0: fault; next uses (2→13, 4→none, 3→12); the farthest, 4, leaves. Faults: 7

reference70120304230321201701frame 0frame 1frame 2770701201201203203243243243203203faultnext use:2: t=130: t=163: none

12. reference 3: in memory → hit.

reference70120304230321201701frame 0frame 1frame 2770701201201203203243243243203203203faultnext use:2: t=150: t=163: none

13. reference 2: in memory → hit.

reference70120304230321201701frame 0frame 1frame 2770701201201203203243243243203203203201faultnext use:2: t=150: t=161: t=17

14. reference 1: fault; next uses (2→15, 0→16, 3→none); the farthest, 3, leaves. Faults: 8

reference70120304230321201701frame 0frame 1frame 2770701201201203203243243243203203203201201faultnext use:2: none0: t=161: t=17

15. reference 2: in memory → hit.

reference70120304230321201701frame 0frame 1frame 2770701201201203203243243243203203203201201201faultnext use:2: none0: t=191: t=17

16. reference 0: in memory → hit.

reference70120304230321201701frame 0frame 1frame 2770701201201203203243243243203203203201201201201faultnext use:2: none0: t=191: t=20

17. reference 1: in memory → hit.

reference70120304230321201701frame 0frame 1frame 2770701201201203203243243243203203203201201201201701faultnext use:7: none0: t=191: t=20

18. reference 7: fault; next uses (2→none, 0→19, 1→20); the farthest, 2, leaves. Faults: 9

reference70120304230321201701frame 0frame 1frame 2770701201201203203243243243203203203201201201201701701faultnext use:7: none0: none1: t=20

19. reference 0: in memory → hit.

reference70120304230321201701frame 0frame 1frame 2770701201201203203243243243203203203201201201201701701701faultnext use:7: none0: none1: none

20. reference 1: in memory → hit. Total: 9 faults (6 replacements): the lower bound for this string and 3 frames.

Page Replacement · Step by step

LRU: Least Recently Used

  • OPT looking backward: a page not used in the recent past probably won't be used in the near future (locality).
  • The page least recently used is replaced.
  • Works well, but the record must be updated on every access: expensive without hardware support.
  • The algorithm that evicts the most recently used page is MRU; LRU evicts the least recently used.
reference70120304230321201701frame 0frame 1frame 27faultlast use (new → old):7

1. reference 7: fault; loaded into free frame 0. Faults: 1

reference70120304230321201701frame 0frame 1frame 2770faultlast use (new → old):07

2. reference 0: fault; loaded into free frame 1. Faults: 2

reference70120304230321201701frame 0frame 1frame 2770701faultlast use (new → old):107

3. reference 1: fault; loaded into free frame 2. Faults: 3

reference70120304230321201701frame 0frame 1frame 2770701201faultlast use (new → old):210

4. reference 2: fault; the least recently used, 7, leaves. Faults: 4

reference70120304230321201701frame 0frame 1frame 2770701201201faultlast use (new → old):021

5. reference 0: in memory → hit; 0 becomes the most recent.

reference70120304230321201701frame 0frame 1frame 2770701201201203faultlast use (new → old):302

6. reference 3: fault; the least recently used, 1, leaves. Faults: 5

reference70120304230321201701frame 0frame 1frame 2770701201201203203faultlast use (new → old):032

7. reference 0: in memory → hit; 0 becomes the most recent.

reference70120304230321201701frame 0frame 1frame 2770701201201203203403faultlast use (new → old):403

8. reference 4: fault; the least recently used, 2, leaves. Faults: 6

reference70120304230321201701frame 0frame 1frame 2770701201201203203403402faultlast use (new → old):240

9. reference 2: fault; the least recently used, 3, leaves. Faults: 7

reference70120304230321201701frame 0frame 1frame 2770701201201203203403402432faultlast use (new → old):324

10. reference 3: fault; the least recently used, 0, leaves. Faults: 8

reference70120304230321201701frame 0frame 1frame 2770701201201203203403402432032faultlast use (new → old):032

11. reference 0: fault; the least recently used, 4, leaves. Faults: 9

reference70120304230321201701frame 0frame 1frame 2770701201201203203403402432032032faultlast use (new → old):302

12. reference 3: in memory → hit; 3 becomes the most recent.

reference70120304230321201701frame 0frame 1frame 2770701201201203203403402432032032032faultlast use (new → old):230

13. reference 2: in memory → hit; 2 becomes the most recent.

reference70120304230321201701frame 0frame 1frame 2770701201201203203403402432032032032132faultlast use (new → old):123

14. reference 1: fault; the least recently used, 0, leaves. Faults: 10

reference70120304230321201701frame 0frame 1frame 2770701201201203203403402432032032032132132faultlast use (new → old):213

15. reference 2: in memory → hit; 2 becomes the most recent.

reference70120304230321201701frame 0frame 1frame 2770701201201203203403402432032032032132132102faultlast use (new → old):021

16. reference 0: fault; the least recently used, 3, leaves. Faults: 11

reference70120304230321201701frame 0frame 1frame 2770701201201203203403402432032032032132132102102faultlast use (new → old):102

17. reference 1: in memory → hit; 1 becomes the most recent.

reference70120304230321201701frame 0frame 1frame 2770701201201203203403402432032032032132132102102107faultlast use (new → old):710

18. reference 7: fault; the least recently used, 2, leaves. Faults: 12

reference70120304230321201701frame 0frame 1frame 2770701201201203203403402432032032032132132102102107107faultlast use (new → old):071

19. reference 0: in memory → hit; 0 becomes the most recent.

reference70120304230321201701frame 0frame 1frame 2770701201201203203403402432032032032132132102102107107107faultlast use (new → old):107

20. reference 1: in memory → hit; 1 becomes the most recent. Total: 12 faults (9 replacements): better than FIFO, worse than OPT.

Page Replacement · Step by step

Implementing LRU: Counters and a Stack

  • Counter (time stamp): a logical clock in the CPU; on every access the clock value is written to the page's entry. Victim: the smallest stamp → a scan of the whole table on every fault.
  • That means a table write on every memory access; hardware support is essential.
  • Stack: page numbers kept in a stack; an accessed page moves to the top, and the victim is at the bottom.
  • Updating is costly, selection is cheap. Neither exists in hardware today; in practice approximations are used.
reference4707101212712stack (top = newest)4← LRU victimdoubly linked listaccess: unlink the node,add at the head (6 pointers)victim: from the tail, O(1)no search needed

4 wasn't on the stack: it is pushed on top.

reference4707101212712stack (top = newest)74← LRU victim← newestdoubly linked listaccess: unlink the node,add at the head (6 pointers)victim: from the tail, O(1)no search needed

 wasn't on the stack: it is pushed on top.

reference4707101212712stack (top = newest)074← LRU victim← newestdoubly linked listaccess: unlink the node,add at the head (6 pointers)victim: from the tail, O(1)no search needed

 wasn't on the stack: it is pushed on top.

reference4707101212712stack (top = newest)704← LRU victim← newestdoubly linked listaccess: unlink the node,add at the head (6 pointers)victim: from the tail, O(1)no search needed

7 was on the stack: it is taken out of the middle and put on top.

reference4707101212712stack (top = newest)1704← LRU victim← newestdoubly linked listaccess: unlink the node,add at the head (6 pointers)victim: from the tail, O(1)no search needed

 wasn't on the stack: it is pushed on top.

reference4707101212712stack (top = newest)0174← LRU victim← newestdoubly linked listaccess: unlink the node,add at the head (6 pointers)victim: from the tail, O(1)no search needed

 was on the stack: it is taken out of the middle and put on top.

reference4707101212712stack (top = newest)1074← LRU victim← newestdoubly linked listaccess: unlink the node,add at the head (6 pointers)victim: from the tail, O(1)no search needed

 was on the stack: it is taken out of the middle and put on top.

reference4707101212712stack (top = newest)21074← LRU victim← newestdoubly linked listaccess: unlink the node,add at the head (6 pointers)victim: from the tail, O(1)no search needed

 wasn't on the stack: it is pushed on top.

reference4707101212712stack (top = newest)12074← LRU victim← newestdoubly linked listaccess: unlink the node,add at the head (6 pointers)victim: from the tail, O(1)no search needed

 was on the stack: it is taken out of the middle and put on top.

reference4707101212712stack (top = newest)21074← LRU victim← newestdoubly linked listaccess: unlink the node,add at the head (6 pointers)victim: from the tail, O(1)no search needed

 was on the stack: it is taken out of the middle and put on top.

reference4707101212712stack (top = newest)72104← LRU victim← newestdoubly linked listaccess: unlink the node,add at the head (6 pointers)victim: from the tail, O(1)no search needed

7 was on the stack: it is taken out of the middle and put on top.

reference4707101212712stack (top = newest)17204← LRU victim← newestdoubly linked listaccess: unlink the node,add at the head (6 pointers)victim: from the tail, O(1)no search needed

 was on the stack: it is taken out of the middle and put on top.

reference4707101212712stack (top = newest)21704← LRU victim← newestdoubly linked listaccess: unlink the node,add at the head (6 pointers)victim: from the tail, O(1)no search needed

2 was on the stack: it is taken out of the middle and put on top. 4 is at the bottom: it leaves on the next replacement.

Page Replacement · Step by step

Implementing LRU: The Matrix Method

Tanenbaum's hardware solution: 4 frames, access order 0 1 2 3 2 1 0 3 2 3. The larger a row's binary value, the more recently the page was used.

reference0123210323012300111= 710000= 0← smallest20000= 030000= 0n frames → n×n bitsaccess k: row k ← 1…1column k ← 0…0victim: smallest row

Page 0: row 0 set to 1s, column 0 to 0s. Row values 7, 0, 0, 0 → smallest row: 1 (no victim is chosen while a frame is free).

reference0123210323012300011= 311011= 1120000= 0← smallest30000= 0n frames → n×n bitsaccess k: row k ← 1…1column k ← 0…0victim: smallest row

Page : row  set to 1s, column  to 0s. Row values  → smallest row:  (no victim is chosen while a frame is free).

reference0123210323012300001= 111001= 921101= 1330000= 0← smallestn frames → n×n bitsaccess k: row k ← 1…1column k ← 0…0victim: smallest row

Page : row  set to 1s, column  to 0s. Row values  → smallest row:  (no victim is chosen while a frame is free).

reference0123210323012300000= 0← LRU11000= 821100= 1231110= 14n frames → n×n bitsaccess k: row k ← 1…1column k ← 0…0victim: smallest row

Page 3: row 3 set to 1s, column 3 to 0s. Row values 0, 8, 12, 14 → LRU (victim): 0. The frames are now full.

reference0123210323012300000= 0← LRU11000= 821101= 1331100= 12n frames → n×n bitsaccess k: row k ← 1…1column k ← 0…0victim: smallest row

Page 2: row 2 set to 1s, column 2 to 0s. Row values 0, 8, 13, 12 → LRU (victim): 0.

reference0123210323012300000= 0← LRU11011= 1121001= 931000= 8n frames → n×n bitsaccess k: row k ← 1…1column k ← 0…0victim: smallest row

Page : row  set to 1s, column  to 0s. Row values  → LRU (victim): .

reference0123210323012300111= 710011= 320001= 130000= 0← LRUn frames → n×n bitsaccess k: row k ← 1…1column k ← 0…0victim: smallest row

Page : row  set to 1s, column  to 0s. Row values  → LRU (victim): .

reference0123210323012300110= 610010= 220000= 0← LRU31110= 14n frames → n×n bitsaccess k: row k ← 1…1column k ← 0…0victim: smallest row

Page : row  set to 1s, column  to 0s. Row values  → LRU (victim): .

reference0123210323012300100= 410000= 0← LRU21101= 1331100= 12n frames → n×n bitsaccess k: row k ← 1…1column k ← 0…0victim: smallest row

Page : row  set to 1s, column  to 0s. Row values  → LRU (victim): .

reference0123210323012300100= 410000= 0← LRU21100= 1231110= 14n frames → n×n bitsaccess k: row k ← 1…1column k ← 0…0victim: smallest row

Page : row  set to 1s, column  to 0s. Row values  → LRU (victim): .

Page Replacement · Step by step

Second Chance

  • An improved FIFO: look at the R (referenced) bit of the page at the head.
  • R = 0 → the page is old and unused: evict it at once.
  • R = 1 → clear R, move the page to the tail (a second chance), and keep searching.
  • The problem: constantly moving pages around the list is needless work → the clock algorithm.
0AR=13BR=07CR=18DR=012ER=114FR=015GR=118HR=1oldest (head)newest (tail)numbers above: load time

Pages are in FIFO order; at time 20 a page fault occurs and space is needed.

0AR=13BR=07CR=18DR=012ER=114FR=015GR=118HR=1oldest (head)newest (tail)numbers above: load time

A at the head has R = 1: it was used recently. Instead of evicting it, give it a second chance.

3BR=07CR=18DR=012ER=114FR=015GR=118HR=120AR=0oldest (head)newest (tail)numbers above: load time

A's R is cleared and it moves to the tail as if newly loaded (time 20).

3BR=07CR=18DR=012ER=114FR=015GR=118HR=120AR=0oldest (head)newest (tail)numbers above: load time

The new head, B: R = 0 → both old and unused: the victim.

7CR=18DR=012ER=114FR=015GR=118HR=120AR=020IR=1oldest (head)newest (tail)numbers above: load time

B is evicted and the new page I joins the tail. Had every R been 1, the list would go around once and behave like pure FIFO.

Tanenbaum MOS Figure 3-15 (redrawn).

Page Replacement · Step by step

The Clock Algorithm

  • The same decision as second chance, a different structure: frames sit in a circular list, a hand (pointer) points to the oldest page; pages don't move.
  • A loaded or accessed page gets R = 1. The hand moves only on a fault.
  • The hand moves around the frame list, not the page table.
7R10R01R12R13R04R1R = 0 → evictR = 1 → R ← 0,hand advances
reference70120304230321201701frame 0frame 1frame 27R1faultR bits:7: 1

1. reference 7: fault; loaded into free frame 0. Faults: 1

reference70120304230321201701frame 0frame 1frame 277R10R1faultR bits:7: 10: 1

2. reference 0: fault; loaded into free frame 1. Faults: 2

reference70120304230321201701frame 0frame 1frame 27707R10R11R1faultR bits:7: 10: 11: 1

3. reference 1: fault; loaded into free frame 2. Faults: 3

reference70120304230321201701frame 0frame 1frame 27707012R10R01R0faultR bits:2: 10: 01: 0

4. reference 2: fault; the hand clears R for 7, 0, 1 (R=1) and moves on; 7 under the hand has R = 0 → evicted. Faults: 4

reference70120304230321201701frame 0frame 1frame 27707012012R10R11R0faultR bits:2: 10: 11: 0

5. reference 0: in memory → hit; hardware sets R ← 1, the hand doesn't move.

reference70120304230321201701frame 0frame 1frame 27707012012012R10R03R1faultR bits:2: 10: 03: 1

6. reference 3: fault; the hand clears R for 0 (R=1) and moves on; 1 under the hand has R = 0 → evicted. Faults: 5

reference70120304230321201701frame 0frame 1frame 27707012012012032R10R13R1faultR bits:2: 10: 13: 1

7. reference 0: in memory → hit; hardware sets R ← 1, the hand doesn't move.

reference70120304230321201701frame 0frame 1frame 27707012012012032034R10R03R0faultR bits:4: 10: 03: 0

8. reference 4: fault; the hand clears R for 2, 0, 3 (R=1) and moves on; 2 under the hand has R = 0 → evicted. Faults: 6

reference70120304230321201701frame 0frame 1frame 27707012012012032034034R12R13R0faultR bits:4: 12: 13: 0

9. reference 2: fault; 0 under the hand has R = 0 → evicted. Faults: 7

reference70120304230321201701frame 0frame 1frame 27707012012012032034034234R12R13R1faultR bits:4: 12: 13: 1

10. reference 3: in memory → hit; hardware sets R ← 1, the hand doesn't move.

reference70120304230321201701frame 0frame 1frame 27707012012012032034034234234R02R00R1faultR bits:4: 02: 00: 1

11. reference 0: fault; the hand clears R for 3, 4, 2 (R=1) and moves on; 3 under the hand has R = 0 → evicted. Faults: 8

reference70120304230321201701frame 0frame 1frame 27707012012012032034034234234203R12R00R1faultR bits:3: 12: 00: 1

12. reference 3: fault; 4 under the hand has R = 0 → evicted. Faults: 9

reference70120304230321201701frame 0frame 1frame 27707012012012032034034234234203203R12R10R1faultR bits:3: 12: 10: 1

13. reference 2: in memory → hit; hardware sets R ← 1, the hand doesn't move.

reference70120304230321201701frame 0frame 1frame 27707012012012032034034234234203203203R01R10R0faultR bits:3: 01: 10: 0

14. reference 1: fault; the hand clears R for 2, 0, 3 (R=1) and moves on; 2 under the hand has R = 0 → evicted. Faults: 10

reference70120304230321201701frame 0frame 1frame 27707012012012032034034234234203203203103R01R12R1faultR bits:3: 01: 12: 1

15. reference 2: fault; 0 under the hand has R = 0 → evicted. Faults: 11

reference70120304230321201701frame 0frame 1frame 27707012012012032034034234234203203203103120R11R12R1faultR bits:0: 11: 12: 1

16. reference 0: fault; 3 under the hand has R = 0 → evicted. Faults: 12

reference70120304230321201701frame 0frame 1frame 27707012012012032034034234234203203203103120120R11R12R1faultR bits:0: 11: 12: 1

17. reference 1: in memory → hit; hardware sets R ← 1, the hand doesn't move.

reference70120304230321201701frame 0frame 1frame 27707012012012032034034234234203203203103120120120R07R12R0faultR bits:0: 07: 12: 0

18. reference 7: fault; the hand clears R for 1, 2, 0 (R=1) and moves on; 1 under the hand has R = 0 → evicted. Faults: 13

reference70120304230321201701frame 0frame 1frame 27707012012012032034034234234203203203103120120120720R17R12R0faultR bits:0: 17: 12: 0

19. reference 0: in memory → hit; hardware sets R ← 1, the hand doesn't move.

reference70120304230321201701frame 0frame 1frame 27707012012012032034034234234203203203103120120120720720R17R11R1faultR bits:0: 17: 11: 1

20. reference 1: fault; 2 under the hand has R = 0 → evicted. Faults: 14 Total: 14 faults: one fewer than FIFO (15), more than LRU (12).

Page Replacement · Step through the code

The Clock Algorithm: Code Trace

int pick_victim(void) {
  while (frm[hand].R == 1) { /* 2nd chance */
    frm[hand].R = 0;
    hand = (hand + 1) % N;
  }
  int k = hand;            /* victim */
  hand = (hand + 1) % N;
  return k;
}
void fault(int p) {
  int k = pick_victim();
  if (frm[k].M) write_back(k); /* dirty */
  frm[k].page = p; frm[k].R = 1;
  frm[k].M = 0;
}
f0: s3R=1 M=0f1: s8R=0 M=1f2: s5R=1 M=0f3: s6R=1 M=1hand = 2 · written to disk: 0

Four frames are full and the hand is at frame 2. A fault occurs for page 9: fault(9) looks for a victim.

f0: s3R=1 M=0f1: s8R=0 M=1f2: s5R=0 M=0f3: s6R=1 M=1hand = 3 · written to disk: 0

Frame 2: s5 has R = 1, so it was used recently. R ← 0 (second chance) and the hand moves to 3.

f0: s3R=1 M=0f1: s8R=0 M=1f2: s5R=0 M=0f3: s6R=0 M=1hand = 0 · written to disk: 0

Frame 3: s6 has R = 1, so it was used recently. R ← 0 (second chance) and the hand moves to 0.

f0: s3R=0 M=0f1: s8R=0 M=1f2: s5R=0 M=0f3: s6R=0 M=1hand = 1 · written to disk: 0

Frame 0: s3 has R = 1, so it was used recently. R ← 0 (second chance) and the hand moves to 1.

f0: s3R=0 M=0f1: s8R=0 M=1f2: s5R=0 M=0f3: s6R=0 M=1hand = 2 · written to disk: 0

Frame 1 has R = 0: the loop ends. k = 1 is the victim; the hand moves on to the next frame (2).

f0: s3R=0 M=0f1: s8R=0 M=1f2: s5R=0 M=0f3: s6R=0 M=1M = 1: p8 written to disk first · written to disk: 1

p8's M (dirty) bit is 1: the copy in memory isn't on disk. It is written to disk before eviction (extra I/O).

f0: s3R=0 M=0f1: s9R=1 M=0f2: s5R=0 M=0f3: s6R=0 M=1hand = 2 · written to disk: 1

p9 is loaded into frame 1: R = 1, M = 0. A full lap was made; three pages now have R = 0.

f0: s3R=0 M=0f1: s9R=1 M=0f2: s5R=0 M=0f3: s6R=0 M=1hand = 3 · written to disk: 1

fault(4): the hand is at frame 2 and R = 0 (cleared on the previous lap, unused since). The victim is found at once: k = 2.

f0: s3R=0 M=0f1: s9R=1 M=0f2: s5R=0 M=0f3: s6R=0 M=1hand = 3 · written to disk: 1

p5 is clean (M = 0): the copy on disk is current, no write needed. A clean victim is cheaper.

f0: s3R=0 M=0f1: s9R=1 M=0f2: s4R=1 M=0f3: s6R=0 M=1hand = 3 · written to disk: 1

p4 has been loaded into frame 2. Two faults cleared 3 R bits and caused 1 disk write; the hand waits at frame 3.

Page Replacement · Step by step

NRU: Not Recently Used

  • Each page has an R (referenced) and an M (modified) bit; the clock interrupt clears the R bits periodically.
  • Classes: 0 = (0,0), 1 = (0,1), 2 = (1,0), 3 = (1,1). A page from the lowest nonempty class is evicted (ties here go to the oldest loaded).
  • Class 1 is possible: a modified page's R bit was cleared by a tick.
  • LRU vs. NRU: LRU tracks the exact order of use; NRU only asks whether the page was used in the last clock interval and whether it is dirty: crude but cheap.
reference70*1203*042*303212*01701frame 0frame 1frame 271 0faultclass (2R + M):7: 2

1. reference 7: fault; loaded into free frame 0. Faults: 1

reference70*1203*042*303212*01701frame 0frame 1frame 2771 001 1faultclass (2R + M):7: 20: 3

2. reference 0*: fault; loaded into free frame 1. Faults: 2

reference70*1203*042*303212*01701frame 0frame 1frame 277071 001 111 0faultclass (2R + M):7: 20: 31: 2

3. reference 1: fault; loaded into free frame 2. Faults: 3

reference70*1203*042*303212*01701frame 0frame 1frame 277070121 001 111 0faultclass (2R + M):2: 20: 31: 2

4. reference 2: fault; classes (7→2, 0→3, 1→2); 7, in the lowest class, leaves. Faults: 4 Clock tick: all R ← 0.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120 001 110 0faultclass (2R + M):2: 00: 31: 0

5. reference 0: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120 001 131 1faultclass (2R + M):2: 00: 33: 3

6. reference 3*: fault; classes (2→0, 0→3, 1→0); 1, in the lowest class, leaves. Faults: 5

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320 001 131 1faultclass (2R + M):2: 00: 33: 3

7. reference 0: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320341 001 131 1faultclass (2R + M):4: 20: 33: 3

8. reference 4: fault; classes (2→0, 0→3, 3→3); 2, in the lowest class, leaves. Faults: 6 Clock tick: all R ← 0.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340321 100 130 1faultclass (2R + M):2: 30: 13: 1

9. reference 2*: fault; classes (4→0, 0→1, 3→1); 4, in the lowest class, leaves. Faults: 7

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320321 100 131 1faultclass (2R + M):2: 30: 13: 3

10. reference 3: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320321 101 131 1faultclass (2R + M):2: 30: 33: 3

11. reference 0: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320321 101 131 1faultclass (2R + M):2: 30: 33: 3

12. reference 3: hit; R ← 1. Clock tick: all R ← 0.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320321 100 130 1faultclass (2R + M):2: 30: 13: 1

13. reference 2: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320320321 111 030 1faultclass (2R + M):2: 31: 23: 1

14. reference 1: fault; classes (2→3, 0→1, 3→1); 0, in the lowest class, leaves (dirty → written to disk first). Faults: 8

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320320321321 111 030 1faultclass (2R + M):2: 31: 23: 1

15. reference 2*: hit; R ← 1, write → M ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320320321321321 111 001 0faultclass (2R + M):2: 31: 20: 2

16. reference 0: fault; classes (2→3, 1→2, 3→1); 3, in the lowest class, leaves (dirty → written to disk first). Faults: 9 Clock tick: all R ← 0.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320320321321321020 111 000 0faultclass (2R + M):2: 11: 20: 0

17. reference 1: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320320321321321021020 111 071 0faultclass (2R + M):2: 11: 27: 2

18. reference 7: fault; classes (2→1, 1→2, 0→0); 0, in the lowest class, leaves. Faults: 10

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320320321321321021021701 011 071 0faultclass (2R + M):0: 21: 27: 2

19. reference 0: fault; classes (2→1, 1→2, 7→2); 2, in the lowest class, leaves (dirty → written to disk first). Faults: 11

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320320321321321021021701701 011 071 0faultclass (2R + M):0: 21: 27: 2

20. reference 1: hit; R ← 1. Clock tick: all R ← 0. Total: 11 faults, 3 dirty-page writes.

* write access · small numbers in the bottom row: R M · a tick every 4 references.

Page Replacement · Step by step

Enhanced Clock: (R, M) Classes

  • The clock looks at M as well as R; evicting a clean page needs no I/O.
  • Pass 1: starting at the hand, look for (0,0) without touching any bits.
  • Pass 2: look for (0,1); the hand clears the R bit of every page it passes. If none is found, go back to pass 1 (now all R = 0).
  • Old macOS, and Silberschatz's "enhanced second-chance" algorithm.
reference70*1203*042*303212*01701frame 0frame 1frame 271 0fault(R, M):7: 10

1. reference 7: fault; loaded into free frame 0. Faults: 1

reference70*1203*042*303212*01701frame 0frame 1frame 2771 001 1fault(R, M):7: 100: 11

2. reference 0*: fault; loaded into free frame 1. Faults: 2

reference70*1203*042*303212*01701frame 0frame 1frame 277071 001 111 0fault(R, M):7: 100: 111: 10

3. reference 1: fault; loaded into free frame 2. Faults: 3

reference70*1203*042*303212*01701frame 0frame 1frame 277070121 000 110 0fault(R, M):2: 100: 011: 00

4. reference 2: fault; a 2-pass scan picks 7 from the lowest class. Faults: 4

reference70*1203*042*303212*01701frame 0frame 1frame 277070120121 001 110 0fault(R, M):2: 100: 111: 00

5. reference 0: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120121 001 131 1fault(R, M):2: 100: 113: 11

6. reference 3*: fault; a 1-pass scan picks 1 from the lowest class. Faults: 5

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120321 001 131 1fault(R, M):2: 100: 113: 11

7. reference 0: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320341 000 130 1fault(R, M):4: 100: 013: 01

8. reference 4: fault; a 2-pass scan picks 2 from the lowest class. Faults: 6

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340341 021 130 1fault(R, M):4: 102: 113: 01

9. reference 2*: fault; a 1-pass scan picks 0 from the lowest class; it is dirty, so it is written to disk first. Faults: 7

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340342341 021 131 1fault(R, M):4: 102: 113: 11

10. reference 3: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340342342301 020 130 1fault(R, M):0: 102: 013: 01

11. reference 0: fault; a 2-pass scan picks 4 from the lowest class. Faults: 8

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340342342302301 020 131 1fault(R, M):0: 102: 013: 11

12. reference 3: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340342342302302301 021 131 1fault(R, M):0: 102: 113: 11

13. reference 2: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340342342302302302311 020 130 1fault(R, M):1: 102: 013: 01

14. reference 1: fault; a 2-pass scan picks 0 from the lowest class. Faults: 9

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340342342302302302312311 021 130 1fault(R, M):1: 102: 113: 01

15. reference 2*: hit; R ← 1, write → M ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340342342302302302312312311 020 101 0fault(R, M):1: 102: 010: 10

16. reference 0: fault; a 1-pass scan picks 3 from the lowest class; it is dirty, so it is written to disk first. Faults: 10

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340342342302302302312312312011 020 101 0fault(R, M):1: 102: 010: 10

17. reference 1: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340342342302302302312312312012010 071 001 0fault(R, M):1: 007: 100: 10

18. reference 7: fault; a 1-pass scan picks 2 from the lowest class; it is dirty, so it is written to disk first. Faults: 11

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340342342302302302312312312012017010 071 001 0fault(R, M):1: 007: 100: 10

19. reference 0: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340342342302302302312312312012017017011 071 001 0fault(R, M):1: 107: 100: 10

20. reference 1: hit; R ← 1. Total: 11 faults, 3 dirty writes.

* write access · small numbers: R M. The same string as NRU; here R is cleared only as the hand passes, with no clock tick.

Page Replacement · Step by step

NFU and Aging

  • NFU (not frequently used): a software counter per page; on every tick, counter += R. The page with the smallest counter is evicted.
  • NFU never forgets: a page heavily used long ago stays forever. Access times aren't recorded, only the counter.
  • Aging: first shift right, then add R at the leftmost bit → the recent past weighs more.
  • A good approximation of LRU; the differences: order within a tick is unknown, and 8 bits remember only 8 ticks.
tick 0 R bits:101011(pages 0 … 5)page 010000000= 128page 100000000= 0page 210000000= 128page 300000000= 0page 410000000= 128page 510000000= 128on every tick:counter ← counter >> 1counter |= R << 7R ← 0fault: smallest counter

Tick 0: R bits 1 0 1 0 1 1 added on the left.

tick 1 R bits:110010(pages 0 … 5)page 011000000= 192page 110000000= 128page 201000000= 64page 300000000= 0page 411000000= 192page 501000000= 64on every tick:counter ← counter >> 1counter |= R << 7R ← 0fault: smallest counter

Tick 1: R bits 1 1 0 0 1 0 added on the left.

tick 2 R bits:110101(pages 0 … 5)page 011100000= 224page 111000000= 192page 200100000= 32page 310000000= 128page 401100000= 96page 510100000= 160on every tick:counter ← counter >> 1counter |= R << 7R ← 0fault: smallest counter

Tick 2: R bits 1 1 0 1 0 1 added on the left.

tick 3 R bits:100010(pages 0 … 5)page 011110000= 240page 101100000= 96page 200010000= 16page 301000000= 64page 410110000= 176page 501010000= 80on every tick:counter ← counter >> 1counter |= R << 7R ← 0fault: smallest counter

Tick 3: R bits 1 0 0 0 1 0 added on the left.

tick 4 R bits:011000(pages 0 … 5)page 001111000= 120page 110110000= 176page 210001000= 136page 300100000= 32← victimpage 401011000= 88page 500101000= 40on every tick:counter ← counter >> 1counter |= R << 7R ← 0fault: smallest counter

Tick 4: R bits 0 1 1 0 0 0 added on the left. A fault now would evict page 3, whose counter is smallest.

Tanenbaum MOS Figure 3-17; counter values recomputed from the R bits.

Page Replacement · Step by step

Aging on the Common String

The same string, 3 frames, a clock tick every 4 references. A newly loaded page has counter 0 and R = 1; in comparisons this interval's R bit counts as the most significant bit (otherwise a new page would be evicted at once).

reference70120304230321201701frame 0frame 1frame 27R1faultcounters:7: 00000000

1. reference 7: fault; loaded into free frame 0. Faults: 1

reference70120304230321201701frame 0frame 1frame 277R10R1faultcounters:7: 000000000: 00000000

2. reference 0: fault; loaded into free frame 1. Faults: 2

reference70120304230321201701frame 0frame 1frame 27707R10R11R1faultcounters:7: 000000000: 000000001: 00000000

3. reference 1: fault; loaded into free frame 2. Faults: 3

reference70120304230321201701frame 0frame 1frame 27707012R10R11R1faultcounters:2: 100000000: 100000001: 10000000

4. reference 2: fault; R and counter together (R as the top bit): 7: 100000000, 0: 100000000, 1: 100000000 → the smallest, 7, leaves. Faults: 4 Tick: counters shifted, R bits added on the left.

reference70120304230321201701frame 0frame 1frame 27707012012R00R11R0faultcounters:2: 100000000: 100000001: 10000000

5. reference 0: hit; R ← 1.

reference70120304230321201701frame 0frame 1frame 27707012012012R00R13R1faultcounters:2: 100000000: 100000003: 00000000

6. reference 3: fault; R and counter together (R as the top bit): 2: 010000000, 0: 110000000, 1: 010000000 → the smallest, 1, leaves. Faults: 5

reference70120304230321201701frame 0frame 1frame 27707012012012032R00R13R1faultcounters:2: 100000000: 100000003: 00000000

7. reference 0: hit; R ← 1.

reference70120304230321201701frame 0frame 1frame 27707012012012032034R10R13R1faultcounters:4: 100000000: 110000003: 10000000

8. reference 4: fault; R and counter together (R as the top bit): 2: 010000000, 0: 110000000, 3: 100000000 → the smallest, 2, leaves. Faults: 6 Tick: counters shifted, R bits added on the left.

reference70120304230321201701frame 0frame 1frame 27707012012012032034034R00R02R1faultcounters:4: 100000000: 110000002: 00000000

9. reference 2: fault; R and counter together (R as the top bit): 4: 010000000, 0: 011000000, 3: 010000000 → the smallest, 3, leaves. Faults: 7

reference70120304230321201701frame 0frame 1frame 27707012012012032034034023R10R02R1faultcounters:3: 000000000: 110000002: 00000000

10. reference 3: fault; R and counter together (R as the top bit): 4: 010000000, 0: 011000000, 2: 100000000 → the smallest, 4, leaves. Faults: 8

reference70120304230321201701frame 0frame 1frame 27707012012012032034034023023R10R12R1faultcounters:3: 000000000: 110000002: 00000000

11. reference 0: hit; R ← 1.

reference70120304230321201701frame 0frame 1frame 27707012012012032034034023023023R10R12R1faultcounters:3: 100000000: 111000002: 10000000

12. reference 3: hit; R ← 1. Tick: counters shifted, R bits added on the left.

reference70120304230321201701frame 0frame 1frame 27707012012012032034034023023023023R00R02R1faultcounters:3: 100000000: 111000002: 10000000

13. reference 2: hit; R ← 1.

reference70120304230321201701frame 0frame 1frame 27707012012012032034034023023023023021R10R02R1faultcounters:1: 000000000: 111000002: 10000000

14. reference 1: fault; R and counter together (R as the top bit): 3: 010000000, 0: 011100000, 2: 110000000 → the smallest, 3, leaves. Faults: 9

reference70120304230321201701frame 0frame 1frame 27707012012012032034034023023023023021021R10R02R1faultcounters:1: 000000000: 111000002: 10000000

15. reference 2: hit; R ← 1.

reference70120304230321201701frame 0frame 1frame 27707012012012032034034023023023023021021021R10R12R1faultcounters:1: 100000000: 111100002: 11000000

16. reference 0: hit; R ← 1. Tick: counters shifted, R bits added on the left.

reference70120304230321201701frame 0frame 1frame 27707012012012032034034023023023023021021021021R10R02R0faultcounters:1: 100000000: 111100002: 11000000

17. reference 1: hit; R ← 1.

reference70120304230321201701frame 0frame 1frame 27707012012012032034034023023023023021021021021021R10R07R1faultcounters:1: 100000000: 111100007: 00000000

18. reference 7: fault; R and counter together (R as the top bit): 1: 110000000, 0: 011110000, 2: 011000000 → the smallest, 2, leaves. Faults: 10

reference70120304230321201701frame 0frame 1frame 27707012012012032034034023023023023021021021021021071R10R17R1faultcounters:1: 100000000: 111100007: 00000000

19. reference 0: hit; R ← 1.

reference70120304230321201701frame 0frame 1frame 27707012012012032034034023023023023021021021021021071071R10R17R1faultcounters:1: 110000000: 111110007: 10000000

20. reference 1: hit; R ← 1. Tick: counters shifted, R bits added on the left. Total: 10 faults.

Dashed line: a clock tick. A single short string can't rank the algorithms; the tick rate and tie rule change the outcome.

Working Set · Step by step

The Working-Set Model

  • Locality: a process uses a small group of pages for a while, then moves to another group.
  • The working set WS(t, Δ): the set of pages used in the most recent Δ memory references (Denning, 1968).
  • If Δ is too small it misses the locality; if too large it overlaps several localities; Δ = ∞ is the whole program.
  • If the working set is in memory, faults are few; if not, thrashing.
2615777751623412344434344413234443444WS = {1, 2, 5, 6, 7}0246|WS(t)|

t = 10: window = references 1–10; working set {1, 2, 5, 6, 7}, size 5. (t1 in Silberschatz)

2615777751623412344434344413234443444WS = {1, 2, 3, 4, 5, 6, 7}0246|WS(t)|

t = 14: window = references 5–14; working set {1, 2, 3, 4, 5, 6, 7}, size 7.

2615777751623412344434344413234443444WS = {1, 2, 3, 4, 5, 6}0246|WS(t)|

t = 18: window = references 9–18; working set {1, 2, 3, 4, 5, 6}, size 6.

2615777751623412344434344413234443444WS = {1, 2, 3, 4}0246|WS(t)|

t = 22: window = references 13–22; working set {1, 2, 3, 4}, size 4.

2615777751623412344434344413234443444WS = {3, 4}0246|WS(t)|

t = 26: window = references 17–26; working set {3, 4}, size 2. (t2): the process has moved to a new locality.

2615777751623412344434344413234443444WS = {1, 2, 3, 4}0246|WS(t)|

t = 30: window = references 21–30; working set {1, 2, 3, 4}, size 4.

2615777751623412344434344413234443444WS = {2, 3, 4}0246|WS(t)|

t = 37: window = references 28–37; working set {2, 3, 4}, size 3.

Working Set · Step by step

The Working-Set Algorithm

  • The exact definition (last Δ references) needs a register shifted on every access: far too expensive. The approximation: the pages used in the last τ of virtual time.
2204current virtual timetime of last useR2084120031198011213020141202012032116200scan all pages:R = 1 → time ← now,R ← 0R = 0, age > τ → evictR = 0, age ≤ τ → rememberthe oldestif none: the oldest(preferably clean) page

Each entry has its time of last use and an R bit. Time is virtual time: the process's own CPU time (measured in ticks). Let τ = 500.

2204current virtual timetime of last useR2204022040220401213022040220402204016200scan all pages:R = 1 → time ← now,R ← 0R = 0, age > τ → evictR = 0, age ≤ τ → rememberthe oldestif none: the oldest(preferably clean) page

On a page fault the table is scanned: pages with R = 1 were used in this tick → their time is set to 2204 and R cleared.

2204current virtual timetime of last useR22040220402204012130age 991 > τ22040220402204016200age 584 > τscan all pages:R = 1 → time ← now,R ← 0R = 0, age > τ → evictR = 0, age ≤ τ → rememberthe oldestif none: the oldest(preferably clean) page

For pages with R = 0 the age is computed: 2204 − 1213 = 991 and 2204 − 1620 = 584; both exceed τ → they are not in the working set.

2204current virtual timetime of last useR22040220402204012130age 991 > τ → evict22040220402204016200age 584 > τscan all pages:R = 1 → time ← now,R ← 0R = 0, age > τ → evictR = 0, age ≤ τ → rememberthe oldestif none: the oldest(preferably clean) page

The first suitable page found (1213) is evicted; the scan continues, updating the other entries. If all are in the set, the oldest R = 0 page is chosen, or failing that a random (preferably clean) page.

Tanenbaum MOS Figure 3-20 (redrawn). The drawback: scanning the whole table on every fault.

Working Set · Step by step

WSClock

  • The working-set idea + the clock's circular list (Carr and Hennessy, 1981): no need to scan the whole table.
  • An old dirty page isn't evicted at once: its write is scheduled and the hand looks for a clean page.
  • If the hand goes all the way around: if at least one write was scheduled, it keeps going until it finds the first page cleaned.
  • If no writes were scheduled, all pages are in the working set: any clean page; failing that, the page under the hand is written and evicted.
1620020321202012014112130198012003120841when the hand reaches a page:R = 1 → R ← 0, time ← now,advanceR = 0, age > τ, clean → evictR = 0, age > τ, dirty → schedulethe write, advanceR = 0, age ≤ τ → advanceτ = 500, now = 2204

Page fault: the hand is at the page with time 2014; R = 1 → used in this tick, not a good victim.

1620020321202012204012130198012003120841when the hand reaches a page:R = 1 → R ← 0, time ← now,advanceR = 0, age > τ, clean → evictR = 0, age > τ, dirty → schedulethe write, advanceR = 0, age ≤ τ → advanceτ = 500, now = 2204

R ← 0, its time becomes 2204, and the hand advances. Next page: time 1213, R = 0.

1620020321202012204012130198012003120841when the hand reaches a page:R = 1 → R ← 0, time ← now,advanceR = 0, age > τ, clean → evictR = 0, age > τ, dirty → schedulethe write, advanceR = 0, age ≤ τ → advanceτ = 500, now = 2204

Age = 2204 − 1213 = 991 > τ: not in the working set. If clean, it's the victim; if dirty, its write would be scheduled and the hand would move on.

1620020321202012204022041198012003120841when the hand reaches a page:R = 1 → R ← 0, time ← now,advanceR = 0, age > τ, clean → evictR = 0, age > τ, dirty → schedulethe write, advanceR = 0, age ≤ τ → advanceτ = 500, now = 2204

The new page is loaded into this frame: time 2204, R = 1. The hand moves to the next frame.

Tanenbaum MOS Figure 3-21 (redrawn). A simple, efficient design, widely used in practice.

Working Set · Step by step

WSClock on the Common String

Virtual time = reference number, τ = 4. The string and the writes (*) are the same as for NRU. A page whose write was scheduled counts as cleaned by the end of the lap.

reference70*1203*042*303212*01701frame 0frame 1frame 271 0faultlast use (virtual time t = 1):7: 1

1. reference 7: fault; loaded into free frame 0. Faults: 1

reference70*1203*042*303212*01701frame 0frame 1frame 2771 001 1faultlast use (virtual time t = 2):7: 10: 2

2. reference 0*: fault; loaded into free frame 1. Faults: 2

reference70*1203*042*303212*01701frame 0frame 1frame 277071 001 111 0faultlast use (virtual time t = 3):7: 10: 21: 3

3. reference 1: fault; loaded into free frame 2. Faults: 3

reference70*1203*042*303212*01701frame 0frame 1frame 277070121 000 110 0faultlast use (virtual time t = 4):2: 40: 41: 4

4. reference 2: fault; hand scan: 7: R=1 → R=0, time=4; 0: R=1 → R=0, time=4; 1: R=1 → R=0, time=4; pass over, no writes: first clean page 7 → victim. Faults: 4

reference70*1203*042*303212*01701frame 0frame 1frame 277070120121 001 110 0faultlast use (virtual time t = 5):2: 40: 41: 4

5. reference 0: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120 000 131 1faultlast use (virtual time t = 6):2: 60: 63: 6

6. reference 3*: fault; hand scan: 0: R=1 → R=0, time=6; 1: R=0, age 2 ≤ τ → in working set; 2: R=1 → R=0, time=6; pass over, no writes: first clean page 1 → victim. Faults: 5

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320 001 131 1faultlast use (virtual time t = 7):2: 60: 63: 6

7. reference 0: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320341 000 130 1faultlast use (virtual time t = 8):4: 80: 83: 8

8. reference 4: fault; hand scan: 2: R=0, age 2 ≤ τ → in working set; 0: R=1 → R=0, time=8; 3: R=1 → R=0, time=8; pass over, no writes: first clean page 2 → victim. Faults: 6

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340321 100 130 1faultlast use (virtual time t = 9):2: 90: 83: 8

9. reference 2*: fault; hand scan: 0: R=0, age 1 ≤ τ → in working set; 3: R=0, age 1 ≤ τ → in working set; 4: R=1 → R=0, time=9; pass over, no writes: first clean page 4 → victim. Faults: 7

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320321 100 131 1faultlast use (virtual time t = 10):2: 90: 83: 8

10. reference 3: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320321 101 131 1faultlast use (virtual time t = 11):2: 90: 83: 8

11. reference 0: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320321 101 131 1faultlast use (virtual time t = 12):2: 90: 83: 8

12. reference 3: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320321 101 131 1faultlast use (virtual time t = 13):2: 90: 83: 8

13. reference 2: hit; R ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320320320 111 030 1faultlast use (virtual time t = 14):2: 141: 143: 14

14. reference 1: fault; hand scan: 0: R=1 → R=0, time=14; 3: R=1 → R=0, time=14; 2: R=1 → R=0, time=14; pass over, nothing clean: 0 is written and evicted. Faults: 8

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320320321321 111 030 1faultlast use (virtual time t = 15):2: 141: 143: 14

15. reference 2*: hit; R ← 1, write → M ← 1.

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320320321321320 101 030 1faultlast use (virtual time t = 16):2: 160: 163: 14

16. reference 0: fault; hand scan: 3: R=0, age 2 ≤ τ → in working set; 2: R=1 → R=0, time=16; 1: R=1 → R=0, time=16; pass over, no writes: first clean page 1 → victim. Faults: 9

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320320321321320320 111 030 1faultlast use (virtual time t = 17):2: 161: 173: 14

17. reference 1: fault; hand scan: 3: R=0, age 3 ≤ τ → in working set; 2: R=0, age 1 ≤ τ → in working set; 0: R=1 → R=0, time=17; pass over, no writes: first clean page 0 → victim. Faults: 10

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320320321321320321320 171 030 1faultlast use (virtual time t = 18):2: 167: 183: 14

18. reference 7: fault; hand scan: 3: R=0, age 4 ≤ τ → in working set; 2: R=0, age 2 ≤ τ → in working set; 1: R=1 → R=0, time=18; pass over, no writes: first clean page 1 → victim. Faults: 11

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320320321321320321327320 170 001 0faultlast use (virtual time t = 19):2: 167: 190: 19

19. reference 0: fault; hand scan: 3: R=0, age 5 > τ, dirty → write scheduled; 2: R=0, age 3 ≤ τ → in working set; 7: R=1 → R=0, time=19; pass over: 3, now written, is clean → victim. Faults: 12

reference70*1203*042*303212*01701frame 0frame 1frame 277070120120120320340320320320320320321321320321327327020 111 000 0faultlast use (virtual time t = 20):2: 161: 200: 20

20. reference 1: fault; hand scan: 2: R=0, age 4 ≤ τ → in working set; 7: R=0, age 1 ≤ τ → in working set; 0: R=1 → R=0, time=20; pass over, no writes: first clean page 7 → victim. Faults: 13 Total: 13 faults, 2 dirty writes. With only 3 frames and a short string, the working-set idea gives no advantage.

Page Replacement

Comparing the Algorithms

AlgorithmComment
OPTNot implementable; a lower bound for comparison.
NRUA very crude approximation of LRU; cheap.
FIFOMay evict important pages; Belady's anomaly.
Second chanceA big improvement over FIFO.
ClockA realistic implementation of second chance.
LRUExcellent, but the exact version needs hardware.
NFUA fairly crude approximation of LRU (never forgets).
AgingAn efficient algorithm that approximates LRU well.
Working setGood, but expensive to implement.
WSClockGood and efficient.
OPT9Aging*10NRU*11Enh. clock*11LRU12WSClock*13Clock14FIFO15
For other reference strings, see Tanenbaum MOS §3.4. * Algorithms using R/M bits also use write and tick information; one short string gives no general ranking. NFU never forgets, so it doesn't approximate LRU well; aging does (Tanenbaum).

Page Replacement · Step by step

The Workload Decides the Policy

020406080100020406080100hit ratio (%)frames (100 distinct pages)No locality: each access random among 100 pagesOPTLRUClockFIFORandomThe past tells nothing:four curves overlap,hits ≈ frames / 100.At 100 frames, all reach 99%.

Without locality it doesn't matter which pages are kept: LRU, FIFO, clock, and random trace the same line. Only OPT, which knows the future, is ahead; once memory holds every page, policy is irrelevant.

020406080100020406080100hit ratio (%)frames (100 distinct pages)80-20: 80% of accesses go to 20% of pagesOPTLRUClockFIFORandomWith 20 frames:OPT 81%, LRU 58%,clock 56%,FIFO and random 53%.

With hot pages, policies that use history win: LRU keeps the 20 hot pages in memory. Clock comes very close to LRU; FIFO is no better than random.

020406080100020406080100hit ratio (%)frames (50 distinct pages)Loop: 0, 1, …, 49, 0, 1, … in orderOPTLRUClockFIFORandomWith 49 frames:LRU, FIFO, clock 0%,random 95.5%, OPT 97.5%.At 50 frames, all reach 99.5%.

A loop slightly larger than memory is LRU's worst case: the evicted page is always the one needed soonest, so every access misses. Blind random choice crushes LRU here.

A 10,000-access simulation (random: average of 5 trials). Sequential scans hit LRU in real systems too: reading a large file once can evict hot pages. That's why Linux first puts a new page on the inactive list and promotes it to the active list only on a second access.

Frame Allocation

Frame Allocation: Equal and Proportional

  • Lower bound: all pages an instruction can touch must be in memory at once (instruction + operands + indirect addresses may cross page boundaries). The architecture sets this.
  • Upper bound: free physical memory.
  • Equal: m frames, n processes → ⌊m / n⌋ each.
  • Proportional: aᵢ = sᵢ / S × m. Example: 10/137 × 62 ≈ 4, 127/137 × 62 ≈ 57.
  • Priority: the ratio is based on priority instead of (or as well as) size; a high-priority process may take frames from a low-priority one.
  • On NUMA, where a frame is matters too: a process should be fed from memory close to the node it runs on.
equalP1: 31P2: 31proportionalP1: 4P2: 571 frees₁ = 10 pages, s₂ = 127 pages, m = 62aᵢ = sᵢ / S × m (rounded down)

Frame Allocation · Step by step

Local and Global Replacement

  • Global: the victim is chosen among all frames; a process can take a frame from another. Higher throughput, but a process's run time can vary widely with others' behavior.
  • Linux and Windows use global reclaim (bounded by cgroup/job-object limits).
  • Local: a process chooses only among its own frames; more consistent performance, but idle frames can't pass to others.
  • A local policy needs a mechanism that adjusts the frame count as the working set grows and shrinks (PFF).
A010A17A25A34A46A53B09B14B26B32B45B56B612C13C25C36frames (processes A, B, C)bottom row: aging counter (small value = unused for long)

Three processes share 16 frames; the numbers are aging counter values (small = unused for a long time). A faults on page A6.

A010A17A25A34A46A63B09B14B26B32B45B56B612C13C25C36frames (processes A, B, C)bottom row: aging counter (small value = unused for long)

Local: the victim comes only from A's frames → A5, with the smallest value. A's frame count stays the same (6).

A010A17A25A34A46A53B09B14B26A62B45B56B612C13C25C36frames (processes A, B, C)bottom row: aging counter (small value = unused for long)

Global: every frame is a candidate → B3, with the smallest value, leaves. A grows, B shrinks: allocation changes dynamically.

Tanenbaum MOS Figure 3-22 (redrawn).

Thrashing

Thrashing

  • If processes' active pages (their working sets) don't fit in memory, the page-fault rate climbs: an evicted page is needed again right away.
  • Processes spend most of their time waiting for pages; the disk queue fills and CPU utilization drops.
  • Old long-term schedulers read this as "CPU idle" and admitted more processes → things got worse (a feedback loop).
  • The cause: Σ |WSᵢ| > memory. Local replacement confines thrashing to one process but doesn't cure it.
  • Remedies: lower the degree of multiprogramming (suspend/swap out processes), more RAM, or the system's OOM killer.
thrashingdegree of multiprogrammingCPUutil.

Symptoms on Linux: high si/so and wa in vmstat 1; /proc/pressure/memory (PSI) reports memory pressure as a percentage.

Thrashing

Preventing Thrashing: Working Set and PFF

  • Working-set strategy: D = Σ |WSᵢ| is the total demand. If D > m, a process is suspended; if D is small enough, a new process is started.
  • For a process returning from suspension, loading its working set first (prepaging) avoids a fault storm.
  • PFF (page-fault frequency): more direct feedback. If the fault rate exceeds an upper bound the process gets more frames; below a lower bound, frames are taken away.
  • If the rate is high and there are no free frames, a process is suspended and its frames redistributed.
Page faults and localities are linked: moving to a new locality causes a peak in the fault rate, which drops once the working set is loaded.
upper bound → add frameslower bound → remove framesnumber of framesfaultrate

Thrashing · Step through the code

PFF: Code Trace

void pff(Proc *s, int p, int t) {
  if (t - s->last > C) {  /* rare faults */
    for (int i = s->n - 1; i >= 0; i--)
      if (s->set[i].R == 0)
        evict(s, i);    /* unused */
  }
  for (int i = 0; i < s->n; i++)
    s->set[i].R = 0;      /* clear */
  add(s, p);              /* with R = 1 */
  s->last = t;
}
t112213341536173849410511412513214red: page fault · green: hit · C = 2beforefree

The process starts with no memory; the threshold is C = 2 references. pff is called on every fault and checks the time since the last fault.

t112213341536173849410511412513214red: page fault · green: hit · C = 2beforefreeafterp1R=1gap = 1 ≤ C

t = 1, first fault (p1): last = 0, gap 1 ≤ C. Nothing to evict; p1 is added with R = 1.

t112213341536173849410511412513214red: page fault · green: hit · C = 2beforep1R=1afterp1R=0p2R=1gap = 1 ≤ C

t = 2, fault on s2: gap 1 ≤ C, faults are frequent. Nothing is evicted, R bits are cleared, and the set grows to 2 pages.

t112213341536173849410511412513214red: page fault · green: hit · C = 2beforep1R=1p2R=1afterp1R=0p2R=0p3R=1gap = 2 ≤ C

t = 4, fault on s3: gap 2 ≤ C, faults are frequent. Nothing is evicted, R bits are cleared, and the set grows to 3 pages.

t112213341536173849410511412513214red: page fault · green: hit · C = 2beforep1R=1p2R=0p3R=1afterp1R=0p3R=0p4R=1gap = 5 > C

t = 9, fault on s4: gap 5 > C, faults are rare. Pages unused since the last fault (R = 0), s2, are evicted; the set shrinks.

t112213341536173849410511412513214red: page fault · green: hit · C = 2beforep1R=0p3R=0p4R=1afterp1R=0p3R=0p4R=0p5R=1gap = 2 ≤ C

t = 11, fault on s5: gap 2 ≤ C, faults are frequent. Nothing is evicted, R bits are cleared, and the set grows to 4 pages.

t112213341536173849410511412513214red: page fault · green: hit · C = 2beforep1R=0p3R=0p4R=1p5R=1afterp4R=0p5R=0p2R=1gap = 3 > C

t = 14, fault on s2: gap 3 > C, faults are rare. Pages unused since the last fault (R = 0), s3, s1, are evicted; the set shrinks.

t112213341536173849410511412513214red: page fault · green: hit · C = 2beforep4R=0p5R=0p2R=1

6 faults in 14 references. When the locality changed (from 1–3 to 4–5), faults first became frequent and the set grew; then the old pages were released on a rare fault.

Design Issues

Cleaning Policy and the Paging Daemon

  • Instead of searching for a victim at fault time, a paging daemon wakes up periodically; if free frames are scarce, it frees pages ahead of time.
  • It writes dirty pages to disk in advance: at eviction time the page is clean, and a fault waits for only one read.
  • A freed frame isn't wiped at once; if needed again it can be reclaimed from the pool (a minor fault).
  • Two-handed clock: the front hand writes dirty pages / clears R, the back hand frees eligible pages.
  • Linux: kswapd works per zone with min / low / high watermarks; writeback threads write dirty file pages.
freepageshighkswapd goes back to sleeplowkswapd wakes upmindirect reclaim

Design Issues

Prepaging

  • Demand paging suffers a fault storm at startup (and on return from suspension). Prepaging brings needed pages in bulk ahead of time.
  • s pages are prepaged and a fraction α is used: the gain is α·s faults, the cost (1 − α)·s unneeded pages. If α is near 0, prepaging loses.
  • Remembering a suspended process's working set and loading it on resumption is the classic example.
  • Linux: readahead for files, page-cluster for swap; applications give hints with madvise(MADV_WILLNEED) or mmap(MAP_POPULATE).
  • Windows: the Prefetcher and SysMain (formerly SuperFetch) load frequently used pages while idle.
demand paginghhhhhhhh8 faults, 8 separate I/Osprepagingone bulk read (s pages)α·s used, (1 − α)·s wasted

Design Issues

Page Size

  • Overhead = se/p (page table) + p/2 (internal fragmentation in the last page); s: process size, e: entry size.
  • Setting the derivative to zero: −se/p² + 1/2 = 0 → p = √(2se). s = 1 MB, e = 8 B → p = √(2²⁴) = 4 KB.
Small pagesLarge pages
less internal fragmentationsmaller page table
track locality finely, less unneeded datafewer faults, efficient I/O (seek/setup cost paid once)
many TLB misseslarge TLB reach

2026: x86-64 4 KB (+2 MB, 1 GB); Apple Silicon 16 KB; Android 15+ supports 16 KB kernels; some ARM64 servers use 64 KB. 1 KB pages appear only on old systems.

010 K20 K30 K5121K2K4K8K16K32K64K4 KB → 4096 B overheadpage size p (bytes)

Design Issues

TLB Reach and Huge Pages

  • TLB reach = number of TLB entries × page size: the amount of memory accessible from the TLB without a miss.
  • If the working set doesn't fit within the reach, time goes to page walks. Adding entries is expensive (associative memory); larger pages are more effective.
  • Huge pages: 2 MB and 1 GB on x86-64. Linux: hugetlbfs and transparent huge pages (THP, khugepaged); multi-size folio/mTHP support in 6.x kernels. Windows: the large-page privilege.
  • The cost: internal fragmentation, finding large contiguous regions (compaction), longer copying/zeroing per page.
  • Databases and virtual machines benefit most from huge pages.
64 × 4 KB256 KB1536 × 4 KB6 MB1536 × 2 MB3 GB16 × 1 GB16 GBreach (log scale)

Design Issues · Step through the code

Program Structure and Locality

int data[128][128];   /* row = 1 page */
/* A: column by column */
for (int j = 0; j < 128; j++)
  for (int i = 0; i < 128; i++)
    data[i][j] = 0;
/* B: row by row */
for (int i = 0; i < 128; i++)
  for (int j = 0; j < 128; j++)
    data[i][j] = 0;
p0s16s32s48s64s80s96s112green: rows (pages) in memory64frameLRU128 pages

128 × 128 int, pages of 128 words: each row is a separate page. The process has 64 frames, none in use.

p0s16s32s48s64s80s96s112green: rows (pages) in memory64frameLRU128 pages

A, in column 0: data[0][0] → a fault on page 0.

p0s16s32s48s64s80s96s112green: rows (pages) in memory64frameLRU128 pages

Along column 0 every i is a different page: 128 faults; they don't fit in 64 frames, so pages 0–63 were evicted long ago.

p0s16s32s48s64s80s96s112green: rows (pages) in memory64frameLRU128 pages

Column 1: data[0][1] is again on page 0, but it was evicted → another fault.

p0s16s32s48s64s80s96s112green: rows (pages) in memory64frameLRU128 pages

Every access faults: 128 × 128 = 16,384 faults.

p0s16s32s48s64s80s96s112green: rows (pages) in memory64frameLRU128 pages

B, row 0: data[0][0] → a fault on page 0, then data[0][1…127] are on the same page: hits.

p0s16s32s48s64s80s96s112green: rows (pages) in memory64frameLRU128 pages

Row 1 → a single fault on page 1; one fault per row.

p0s16s32s48s64s80s96s112green: rows (pages) in memory64frameLRU128 pages

128 faults in total: the same work, 128 times fewer faults.

C stores arrays in row-major order, Fortran in column-major. Stacks have good locality; hash tables and pointer-chasing structures have poor locality; compilers can perform loop interchange.

Design Issues

I/O Interlock and Page Pinning

process Aread(fd, buf)disk + DMAwrites to a physical addressframe 42A's buffer

A requests a read from disk; the kernel programs the DMA with the physical address of frame 42, which holds the buffer.

process Aread(fd, buf)disk + DMAwrites to a physical addressframe 42A's bufferDMA in progress

A blocks while the device queues/works. Meanwhile frame 42 looks like an ordinary page.

process Aread(fd, buf)disk + DMAwrites to a physical addressframe 42B's page!DMA in progressB faulted, the globalpolicy picked 42

B page-faults; the global policy chooses 42 as the victim and loads B's page. When the DMA finishes, it writes over B's page: corruption.

process Aread(fd, buf)disk + DMAwrites to a physical addressframe 42A's buffer (locked)DMA in progresslock bit = 1:cannot be a victim

The fix: the frame is locked (pinned) for the duration of the I/O and never chosen as a victim; or the I/O goes to kernel buffers and is copied afterward.

  • Other pinned pages: parts of kernel code and data, and the memory of real-time processes (mlock(), mlockall()).
  • Linux drivers pin user pages with pin_user_pages() (RDMA, GPUs, io_uring fixed buffers). An IOMMU lets devices use translated addresses too.
  • A page of a newly loaded low-priority process that hasn't run yet can also be protected until it is used once.
  • The risk: a locked page that is forgotten makes its frame unusable forever; so locking is limited (ulimit -l).

Kernel Memory

Kernel Memory: The Buddy System

Kernel memory differs from user memory: it is often not paged, needs structures of various sizes, and must be physically contiguous for DMA.

256 KB

The kernel has a physically contiguous 256 KB region; a 21 KB request arrives.

256 KBA_L 128 KBA_R 128 KB

The block is split into two equal buddies: AL and AR, 128 KB each.

256 KBA_L 128 KBA_R 128 KBB_L 64 KBB_R 64 KB

AL is split again: BL, BR (64 KB).

256 KBA_L 128 KBA_R 128 KBB_L 64 KBB_R 64 KBC_L 32C_R 32

BL is split: CL, CR (32 KB). The smallest power of 2 that holds 21 KB is 32 KB → CL is allocated.

256 KBA_L 128 KBA_R 128 KBB_L 64 KBB_R 64 KBC_L 32C_R 3221 KB used, 11 KB internal fragmentation

The cost is internal fragmentation: 21 requested, 32 KB given.

256 KBC_L + C_R → B_L, B_L + B_R → A_L, A_L + A_R → 256 KB

On release, free buddies coalesce quickly: a buddy's address is found by flipping a single bit.

The Linux page allocator is a buddy allocator: lists of orders 0 to 10 (4 KB … 4 MB), cat /proc/buddyinfo.

Kernel Memory · Step through the code

The Buddy Allocator: Code Trace

int alloc(int k) {         /* 2^k KB */
  int j = k;
  while (fl[j] == NULL) j++;
  int a = pop(j);
  while (j > k) {   /* split, right half free */
    j--; push(j, a + (1 << j));
  }
  return a;
}
void release(int a, int k) {
  int b = a ^ (1 << k);    /* buddy */
  while (k < MAX && remove(k, b)) {
    a = a < b ? a : b;     /* merge */
    k++; b = a ^ (1 << k);
  }
  push(k, a);
}
a 256 KB physically contiguous region256@0064128192256fl[8] (256 KB):@0fl[7] (128 KB):—fl[6] (64 KB):—fl[5] (32 KB):—

A single free block: 256 KB at @0 (order 8). The 21 KB request rounds up to 32 KB (25): A = alloc(5).

a 256 KB physically contiguous region256@0064128192256fl[8] (256 KB):@0fl[7] (128 KB):—fl[6] (64 KB):—fl[5] (32 KB):—

fl[5], fl[6], fl[7] are empty; the first nonempty list is fl[8]: j = 8. A larger block will be split.

a 256 KB physically contiguous region256@0064128192256fl[8] (256 KB):—fl[7] (128 KB):—fl[6] (64 KB):—fl[5] (32 KB):—

The 256 KB block is taken from the list: a = 0.

a 256 KB physically contiguous region128@0128@128064128192256fl[8] (256 KB):—fl[7] (128 KB):@128fl[6] (64 KB):—fl[5] (32 KB):—

j = 7: the block is split; the right half, @128 (128 KB), goes on fl[7] and the left half is split further.

a 256 KB physically contiguous region64@064@64128@128064128192256fl[8] (256 KB):—fl[7] (128 KB):@128fl[6] (64 KB):@64fl[5] (32 KB):—

j = 6: the block is split; the right half, @64 (64 KB), goes on fl[6] and the left half is split further.

a 256 KB physically contiguous region32@032@3264@64128@128064128192256fl[8] (256 KB):—fl[7] (128 KB):@128fl[6] (64 KB):@64fl[5] (32 KB):@32

j = 5: the last split; @32 goes to fl[5]. Since j = k the loop ends, and the left half (@0, 32 KB) is given to A.

a 256 KB physically contiguous regionA 32@032@32B 64@64128@128064128192256fl[8] (256 KB):—fl[7] (128 KB):@128fl[6] (64 KB):—fl[5] (32 KB):@32

B = alloc(6): @64 is ready on fl[6]; no split needed, it returns at once. Internal fragmentation: 32 − 21 = 11 KB in A.

a 256 KB physically contiguous region32@32B 64@64128@128064128192256fl[8] (256 KB):—fl[7] (128 KB):@128fl[6] (64 KB):—fl[5] (32 KB):@32

release(0, 5): A is being returned. The buddy's address is found by flipping one bit: 0 ⊕ 32 = 32.

a 256 KB physically contiguous region64@0B 64@64128@128064128192256fl[8] (256 KB):—fl[7] (128 KB):@128fl[6] (64 KB):—fl[5] (32 KB):—

@32 is on fl[5]: the buddy is free, so they merge → 64 KB at @0. The new buddy is 0 ⊕ 64 = 64.

a 256 KB physically contiguous region64@0B 64@64128@128064128192256fl[8] (256 KB):—fl[7] (128 KB):@128fl[6] (64 KB):@0fl[5] (32 KB):—

@64 isn't on fl[6] (B is using it): merging stops, and @0 (64 KB) goes on fl[6].

a 256 KB physically contiguous region128@0128@128064128192256fl[8] (256 KB):—fl[7] (128 KB):@128fl[6] (64 KB):—fl[5] (32 KB):—

release(64, 6): its buddy 64 ⊕ 64 = 0 is free → a 128 KB block at @0 forms; the new buddy is @128.

a 256 KB physically contiguous region256@0064128192256fl[8] (256 KB):@0fl[7] (128 KB):—fl[6] (64 KB):—fl[5] (32 KB):—

@128 is free too: they merge into 256 KB; k = MAX, so it stops. The region is back to one piece, as at the start.

Kernel Memory

Buddy: An Example Request Sequence

Start with a single free 1 MiB (1024 K) block; each request rounds up to the next power of 2. A buddy's address is address XOR size.

EventBlockSplit or mergeInternal fragmentation
A = 70 K128 K1024 → 512 + 512 → 256 + 256 → 128 + 12858 K
B = 35 K64 K128 → 64 + 6429 K
C = 80 K128 K256 → 128 + 12848 K
A released—its buddy (128–256) is split and holds B → no merge—
D = 60 K64 KB's free buddy is given; no split needed4 K
B released—its buddy D is in use → no merge—
D released—64 + 64 → 128, with A's free 128 → 256—
C released—128 + 128 → 256 → 512 → 1024: memory is a single block again—

Total internal fragmentation for A, B, and C: 58 + 29 + 48 = 135 K. Buddy allocation and coalescing are fast (a free list per size, O(log n)), but in the worst case about half of a block is wasted.

Kernel Memory

The Slab Allocator

objectscachesslabs (contiguous pages)task_structinodetask_structcacheinodecachefullpartialobjects wait in a constructed state; allocation = take one from the list

One cache for each frequently used kernel object; a cache consists of one or more slabs, and a slab of contiguous pages.

objectscachesslabs (contiguous pages)task_structinodetask_structcacheinodecachefullpartialobjects wait in a constructed state; allocation = take one from the list

A request for a new task_struct (e.g., fork()): first look for a free object in a partial slab; one is found and returned.

objectscachesslabs (contiguous pages)task_structinodetask_structcacheinodecachefullfullobjects wait in a constructed state; allocation = take one from the list

The partial slab is now full. No fragmentation: objects are exactly sized and packed side by side within the page.

objectscachesslabs (contiguous pages)task_structinodetask_structcacheinodecachefullfullnew2 pages taken from buddyobjects wait in a constructed state; allocation = take one from the list

No partial or empty slab is left: new pages come from the buddy allocator and a new slab is created. Empty slabs are returned under memory pressure.

  • Bonwick (Solaris, 1994). Benefits: no internal fragmentation, O(1) allocation/freeing, hot ready-made objects in the cache, and a lock-free fast path with per-CPU lists.
  • Linux: SLOB was removed in 6.4 and classic SLAB in 6.8 (2024); today the only allocator is SLUB. kmalloc() draws from size-class slab caches; monitor with slabtop, /proc/slabinfo.

Real Systems

Linux: Page Reclaim

  • When free pages drop below the low watermark, kswapd wakes; below min, the allocating process does direct reclaim (latency!).
  • The classic LRU is a pair of clock-like lists: active / inactive, separately for anonymous and file pages (plus unevictable for locked pages).
  • vm.swappiness (0–200) sets the preference between anonymous and file pages. When memory runs out, the OOM killer kills a process.
anonymousactiveinactivereclaim from the tailfileactiveinactivereclaim from the tailleft arrow: promote on second accessright arrow: demote when agedanonymous → swap / zswapclean file → just drop

Real Systems

Linux: MGLRU, zswap, and zram

MGLRU (6.1, 2022)

Multi-generational LRU: pages are divided into 4 generations; access bits are collected by walking page tables, and the victim comes from the oldest generation. Less CPU, better victim choice. /sys/kernel/mm/lru_gen/enabled

zswap

A compressed cache in front of swap: an anonymous page is compressed in RAM before going to disk; when the pool fills, the oldest entries are written to real swap.

zram

A compressed block device in RAM, used as swap. Fedora (since 33), Android, and ChromeOS use it by default.

MGLRU generationsgeneration 3youngestgeneration 2generation 1generation 0oldestevictaccessed (A bit) → move to youngestthe zswap pathanonymous pagezswap(RAM, compressed)swapwrite when pool fillszram: the swap device itself is in RAMa compressed block device (never goes to disk)

MGLRU is the default on ChromeOS and Android; many distribution kernels enable it as a build option. Compression costs CPU, but it is hundreds of times cheaper than going to disk.

Real Systems

Windows: Working Sets and Compression

  • Each process has a working set (with minimum/maximum limits); a fault either makes room in the set or grows it.
  • When memory runs low, the working-set manager (balance set manager) trims sets; a trimmed page isn't discarded at once but goes to the standby or modified list and can be recovered with a minor fault.
  • Memory compression (Windows 10, 2015): trimmed pages are kept in a compression store in a system process; writing to pagefile.sys is the last resort.
  • Prefetching: the Prefetcher and SysMain; large pages via VirtualAlloc(MEM_LARGE_PAGES).
  • macOS/iOS are similar: compressed memory, no swap on iOS, and app termination under pressure (Jetsam).
working setmodifiedstandbyfree / zerocompression storetrim (dirty)trim (clean)writereusecompressfault: standby → back (minor fault)

Summary

Summary

Mechanism

Demand paging: invalid entry → page fault → bring in the page → update the table → restart the instruction. COW and mmap use the same mechanism.

Performance

EAT is extremely sensitive to the fault rate: on an HDD, p = 0.001 slows memory down 251×. Fast SSDs and compression shrink the penalty but don't eliminate it.

Policy

On the common string (3 frames): OPT 9, LRU 12, clock 14, FIFO 15 faults. In practice: variants of clock, aging, and WSClock; MGLRU on Linux.

System

Allocation (equal/proportional, local/global), the working set and PFF against thrashing, page size and TLB reach, pinning, buddy + slab.

Thanks to locality, only processes' working sets need to be in memory; the OS's job is to keep those sets resident with little I/O.

Summary

Review Questions

  1. How many faults does FIFO make on 1 2 3 4 1 2 5 1 2 3 4 5 with 3 and 4 frames? Why doesn't LRU show this anomaly?
  2. Memory access 200 ns, fault service 8 ms: EAT for p = 0.001? Largest p for a 10% slowdown?
  3. 62 frames, two processes of 10 and 127 pages: what does proportional allocation give?
  4. What is the TLB reach of a 64-entry TLB with 4 KB and with 2 MB pages?
  5. After fork() the child writes 3 of 100 pages, then calls exec(). How many pages are copied?
  6. Distinguish minor and major page faults with an example of each.
  7. Why does thrashing lower CPU utilization? How does the working-set model prevent it?
  8. What is the optimal page size for s = 4 MB, e = 8 B?
  9. Why must a buffer page be locked during I/O?
  10. In a 1 MiB buddy system, what block does a 70 K request get? How much internal fragmentation?
Answers
  1. 9 and 10. LRU is a stack algorithm: the set in n frames is a subset of that in n+1.
  2. EAT = 200 × 0.999 + 8,000,000 × 0.001 ≈ 8,199.8 ns ≈ 8.2 µs (41×). 220 > 200 + 7,999,800 p → p < 2.5 × 10⁻⁶.
  3. ⌊10/137 × 62⌋ = 4 and ⌊127/137 × 62⌋ = 57 frames.
  4. 64 × 4 KB = 256 KB; 64 × 2 MB = 128 MB.
  5. Only the 3 written pages (COW); exec() then drops all mappings.
  6. Minor: a shared-library page already cached; only the table changes. Major: a swapped-out page is read from disk.
  7. Processes all wait for paging I/O; none is ready. When D = Σ |WSᵢ| > m, suspend a process.
  8. √(2 × 2²² × 8) = √(2²⁶) = 8 KB.
  9. DMA writes to a physical address; if the frame is reassigned meanwhile, another process's data is corrupted.
  10. 128 K; 128 − 70 = 58 K.

Summary

References

  • A. Silberschatz, P. B. Galvin, G. Gagne, Operating System Concepts, 10th ed., Wiley, 2018 (Chapter 10: Virtual Memory).
  • A. S. Tanenbaum, H. Bos, Modern Operating Systems, 4th ed. (2014) / 5th ed. (2023), Pearson (Chapter 3: Memory Management).
  • R. H. Arpaci-Dusseau, A. C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces (OSTEP), ostep.org: "Swapping: Mechanisms", "Swapping: Policies", "Complete VM Systems".
  • L. A. Belady, "A Study of Replacement Algorithms for a Virtual-Storage Computer", IBM Systems Journal 5(2), 1966; P. J. Denning, "The Working Set Model for Program Behavior", CACM 11(5), 1968.
  • R. W. Carr, J. L. Hennessy, "WSCLOCK: A Simple and Effective Algorithm for Virtual Memory Management", SOSP 1981; J. Bonwick, "The Slab Allocator", USENIX Summer 1994.
  • Linux kernel documentation: admin-guide/mm/multigen_lru.rst, admin-guide/mm/zswap.rst, admin-guide/mm/transhuge.rst, admin-guide/blockdev/zram.rst.
  • P. Yosifovich, A. Ionescu, M. Russinovich, D. Solomon, Windows Internals, 7th ed., Part 1, Chapter 5: Memory management.

The End

Chapter 9: Virtual Memory
Previous: Chapter 8: Memory Management · Next: Chapter 10: File Systems
1 / 1