Skip to main content

Lesson 12 of 26 · 13 min read

Page faults, replacement policies, and VAX/VMS

Follow the complete translation path across TLB hits, resident misses, and disk page faults, compare FIFO, LRU, and Clock eviction, and inspect real VAX/VMS VM tricks.

Four swapping and page-fault stages: Present = 0, Fetch from swap, Clock sweep, and Copy-on-Write.
The Present bit, disk swap, Clock page replacement, and Copy-on-Write let virtual memory exceed physical RAM.
Course syllabus · lesson 12 of 26
In this lesson

Applications can map more memory than the machine can hold in RAM. A virtual page can represent a resident frame, data in a file or swap, or a range whose first access should supply zero-filled storage. Each legal access still has to observe the bytes and permissions promised by its mapping. The kernel uses page faults to restore that promise on demand and replacement policies to decide what can leave RAM.

A translation diagram with a TLB hit, a resident page-table walk, and a swap fault, followed by a Clock sweep and VAX/VMS techniques.
Open full-size diagram
A resident translation can be refilled from the table. A swap fault needs the kernel to restore the page before the access can finish.

Trace the three translation outcomes

4 KiB pages (12-bit offset). Choose a scenario and step from virtual address split through TLB, page table, and physical memory or swap.

Virtual Address0x1234 (VPN 1 · 0x234)
Page Table EntryValid=1 · Present=1 · PFN=5
Resolved TargetPhysical 0x5234 (0 extra page-table reads)

Follow translation from CPU to RAM

TLB Hit (0x1234). Split 16-bit virtual address 0x1234 under 4 KiB (12-bit offset) pages: upper bits select VPN = 1; low 12 bits hold offset = 0x234.CPU0x1234TLBlookup nextPage tableskippedRAM—Swap diskunusedClock

Step 1 of 5 · CPU / MMU

Split VA

Split 16-bit virtual address 0x1234 under 4 KiB (12-bit offset) pages: upper bits select VPN = 1; low 12 bits hold offset = 0x234.

VPN = 1 · Offset = 0x234 (offset never changes)

Distinguish a missing translation from a missing page

Start with a model that separates two states. Valid means the address belongs to a legal mapping. Present means its contents currently have a usable physical frame. A hardware entry may combine these meanings or keep some absent-page state in kernel metadata. Use the distinction to reason about each path.

  • On an allowed TLB hit, the cache supplies a physical frame number (PFN) and the access proceeds with its byte offset.
  • On a TLB miss for a resident page, the walker finds its translation in the page table and can refill the cache. The kernel need not fetch the page from storage.
  • On an access to a legal, nonresident page, the processor faults and the kernel consults backing information. The contents may come from a swap slot, the executable or another mapped file, or a fresh zero-filled frame.
  • For an unmapped or disallowed address, the kernel handles an illegal reference, often by delivering a signal to the process. A permitted write to a copy-on-write mapping is a separate recoverable fault even when the old page is resident.

On a swap fault, the kernel needs a frame, so it first tries the free pool. It can initiate or wait for reclaim when the pool is empty. When reclaiming a victim, it must coordinate changes to the mapping with invalidation of cached translations before the frame is reused. If the victim has changed since its backing copy, that data needs writeback before reuse. A clean file-backed page can be discarded and reloaded from its file.

The kernel reserves the destination frame during the read. The faulting thread blocks if it needs to wait for storage, allowing the scheduler to run another ready thread. On completion, the kernel publishes the new resident mapping, wakes the thread, and lets the instruction retry. Concurrent faults for the same page can wait on one in-progress fill. Installing a present entry before the bytes arrive could expose incomplete data to other threads. Background reclaim can start below a low free-frame watermark and continue until a higher target is reached. This keeps some work off the demand-fault path and creates room to group writes.

To put the cost in perspective, suppose a completed access takes 100 ns when resident, a storage fault adds 1 ms, and one access in 10,000 faults this way. In this simplified model the added mean cost is 0.0001 * 1,000,000 ns = 100 ns. That small fraction of storage faults doubles the average, even before accounting for dirty writeback or queuing.

Decide which page to replace

Replacement treats physical memory as a cache of virtual pages. For a fixed reference stream and frame count, count a miss each time the requested page is absent. With an empty initial cache, first references miss under every demand replacement policy. Later misses depend on whether the requested page survived earlier evictions.

Belady's optimal policy, OPT or MIN, evicts the resident page whose next reference lies farthest in the future. A page never used again has infinite next-use distance. To see the exchange argument, imagine an optimal schedule that evicts an earlier-needed page at the first point where it differs from OPT. Keep that earlier-needed page and evict the later-needed page instead. Follow the original schedule on the common pages. If it discards the later-needed page before the first reference to the earlier-needed one, discard the earlier-needed page in its place and the caches agree again. If neither page returns, keeping either one has the same effect. Otherwise the first reference that distinguishes the caches is to the earlier-needed page. The original schedule pays to load it, while the modified schedule already has it. If the original evicts the later-needed page, the caches agree. If it evicts a common page, the modified schedule can discard that same common page and reserve the vacated slot for the later-needed page. Fetch it on demand if it is used before the original schedule evicts it. This costs at most the miss already saved and lets the modified schedule follow the original from that point. Thus an optimal schedule can agree with the farthest-next-use choice. OPT is a lower bound on page faults for the model. A running kernel lacks the future trace, and minimizing faults also leaves out the differing costs of dirty and clean victims.

First-in, first-out (FIFO) keeps pages in load order. On a miss it removes the oldest page when full and enqueues the new one. Hits leave the queue unchanged. Least recently used (LRU) orders pages by their most recent reference, so a hit moves a page to the newest end. Exact LRU involves recording every reference or maintaining equivalent ordering information. Doing that on every instruction fetch, load, and store is costly.

LRU has a useful monotonicity property. After any prefix of a reference stream, order all referenced pages by their latest use. A capacity of three holds the first three in this order and a capacity of four holds the first four. Every hit at capacity three is also a hit at capacity four. FIFO uses load order, which can diverge between capacities because they miss at different times. Its resident sets need not be nested.

Approximate recency with Clock

Clock walks a ring of candidate frames. A reference sets the use bit of a page. On examination, a frame with use one gets a second chance: clear its bit and advance the hand. The first frame with use zero is the basic algorithm's victim. Keep the hand at the next frame for the following eviction. The bit says whether a reference happened since the last clear. It does not encode an exact timestamp.

Suppose the hand starts at A in a ring A, B, C. A has use one and is clean, B has use zero and is dirty, and C has use zero and is clean. Basic Clock clears A's use bit and chooses B. A clean-preferring variant can remember B as a fallback and continue to C, avoiding immediate writeback. In this variant a frame with a set use bit gets its second chance even if it is clean. A dirty bit tracks modification since the backing copy, while a use bit can be set by a read. Clearing the use bit therefore never authorizes discarding modified data.

Recognize a working set that does not fit

The working set describes pages referenced during a recent window of execution. A large mapped region can have a small working set when the process reuses just a few pages. If concurrently running processes need 3000, 2500, and 2000 distinct pages in overlapping windows but only 6000 frames are available after other reservations, the demand exceeds supply by 1500 frames. Faulting pages may repeatedly replace other pages still needed in the same window.

This sustained churn is thrashing. It shows up as frequent page-ins and evictions with little useful progress, often because threads wait on storage. A burst of first-touch faults that settles once pages are resident behaves differently. Admission control can reduce the concurrent demand by suspending some work. A smarter victim choice can help when a hot subset fits. When active demand consistently exceeds the available frames, the system also needs to reduce that demand or increase the capacity.

See how VAX/VMS combined the mechanisms

VAX-11 virtual addresses were 32 bits with 512-byte pages, yielding a 9-bit offset and a 23-bit VPN. P0 held user code and heap, P1 held the user stack, and system space held privileged mappings. Separate tables for P0 and P1 avoided entries for much of the gap between heap and stack. Those user tables lived in kernel virtual memory and could themselves be paged. Resolving a user translation could therefore require translating the location of its PTE through the system page table. A TLB hit bypassed that work. Page zero was deliberately inaccessible, and supervisor permissions protected the system mappings.

VAX supplied a dirty bit but no use bit. VMS used segmented FIFO with a per-process resident set limit and global clean and dirty second-chance lists. An evicted page stayed recoverable from its old frame while on one of those lists. If its process referenced it before the frame was reused, VMS could restore the mapping without a disk read. Grouping dirty pages into a batch improved write efficiency with small pages. This design bounded a process's resident set while retaining a chance to recover pages recently removed from it.

On first touch to a demand-zero page, the kernel can supply zeroed contents without reading storage. Copy-on-write starts with a different promise: two mappings share existing contents until an allowed write requires a private version. Mark the shared mappings read-only and track that the protection is for copy-on-write. On the write fault, allocate a new frame if the contents are still shared, copy the old bytes, and install the writable private mapping before retrying. A read-only mapping without the copy-on-write state stays read-only. Copy-on-write makes a fork followed by exec cheaper because pages the child never writes need no private copy.

Trace a swap fault through the lab

In the lab's swap case, the load at 0x3108 has VPN 3 and offset 0x108 with 4 KiB pages. A missing TLB entry leads to PTE[3], where the logical state is valid and nonresident. Follow the trap and the wait for I/O. After the contents are restored into PFN 12, the resumed instruction accesses physical (12 * 4096) + 0x108 = 0xC108. The offset stays fixed through the retry. A later load from that page can use the reestablished translation until the kernel changes it or the cache entry is replaced.

Worked trace of Belady's anomaly

Start with empty FIFO queues and reference 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5. Each queue below shows the resident pages after the reference, from oldest to newest. M means miss and H means hit.

Code exampleTrace
Step  Page    3 frames        4 frames
  1     1     M [1]           M [1]
  2     2     M [1,2]         M [1,2]
  3     3     M [1,2,3]       M [1,2,3]
  4     4     M [2,3,4]       M [1,2,3,4]
  5     1     M [3,4,1]       H [1,2,3,4]
  6     2     M [4,1,2]       H [1,2,3,4]
  7     5     M [1,2,5]       M [2,3,4,5]
  8     1     H [1,2,5]       M [3,4,5,1]
  9     2     H [1,2,5]       M [4,5,1,2]
 10     3     M [2,5,3]       M [5,1,2,3]
 11     4     M [5,3,4]       M [1,2,3,4]
 12     5     H [5,3,4]       M [2,3,4,5]

Three frames incur nine faults and four frames incur ten. The hits on steps 5 and 6 leave pages 1 and 2 old in the four-frame queue. At step 7, the three-frame queue has already reloaded both and retains them, while the four-frame queue evicts page 1. This is the split that sets up the run of four-frame misses.

For an OPT comparison at three frames, the first eviction is on step 4. Pages 1 and 2 are needed at steps 5 and 6, so evict page 3, next needed at step 10. At step 7, evict page 4, next needed at step 11. At steps 10 and 11, evict pages 1 and 2 in either order because neither returns. The complete miss set is steps 1, 2, 3, 4, 7, 10, and 11, for seven faults.

Change one input. Keep the four-frame capacity and change the replacement policy from FIFO to LRU. After step 6 the oldest-to-newest order is [3,4,1,2]. Step 7 evicts page 3, preserving the hits at steps 8 and 9. The miss set becomes steps 1, 2, 3, 4, 7, 10, 11, and 12, for eight faults.

In the page-walk lab above, compare a resident TLB miss with a swap fault. Only the swap path needs to wait for the requested bytes to arrive from storage. Next, see how threads share an address space in threads, shared memory, and race conditions.

Before you move on

Try it in your workspace

Trace the three translation outcomes — TLB hit, TLB miss (resident), and page fault (on disk) — then run a 4-page FIFO vs. LRU trace and compare fault counts using the interactive page-walk lab.

Keep a short note of what you tried, what passed, and what you still need to check.

Your practice record

0 of 3 checked.

Saved in this browser when storage is available. Uncheck any item to revisit it. This is your own record, not an assessment.