Translates

How to say it in other languages, and what words mean

Spanish

Page Fault Handling in Address Translation

· 6 min read · Spanish

In this article
  1. Background: Virtual Memory and Paging
  2. How Page Faults Occur
  3. Step-by-Step: The Page Fault Handler
  4. Page Replacement and Thrashing
  5. Common Misconceptions and Related Questions
  6. Performance Impact and Optimization
  7. Frequently Asked Questions

When the operating system encounters a page fault during address translation, it pauses the faulting process, verifies that the virtual address is valid, loads the required page from disk into physical memory (or creates a zero-filled page if it's a demand-zero page), updates the page table, and then resumes the process so it can retry the faulting instruction. This mechanism is the core of virtual memory and enables programs to use more memory than physically available.

Background: Virtual Memory and Paging

Modern operating systems use virtual memory to give each process its own private address space. The CPU's memory management unit (MMU) translates virtual addresses to physical addresses using page tables. Memory is divided into fixed-size blocks called pages (typically 4 KB on x86). When a program accesses an address, the MMU looks up the page table entry (PTE) for that page. If the PTE indicates the page is present in physical memory, the translation completes immediately. If not, the MMU triggers a page fault exception, which transfers control to the operating system's page fault handler.

How Page Faults Occur

Page faults are not errors; they are a normal part of virtual memory operation. They occur when a program accesses a page that is not currently in physical memory. The three main scenarios are:

  • Demand paging: The program references a page that has never been loaded. The OS loads it from disk (e.g., from the executable file or a memory-mapped file).
  • Swapping: The page was previously in memory but was moved to the swap area on disk to free memory for other processes.
  • Copy-on-write: A page is shared between processes (e.g., after fork()), and one process attempts to write to it. The OS creates a private copy.

Additionally, a page fault can be a protection fault, such as accessing a page with insufficient permissions (e.g., writing to a read-only page). The OS treats this as an invalid access and typically terminates the process with a segmentation fault.

Step-by-Step: The Page Fault Handler

When a page fault occurs, the CPU saves the current state and jumps to the OS's exception handler. The handler performs these steps:

  1. Determine the faulting address: The CPU provides the virtual address that caused the fault (stored in a control register like CR2 on x86).
  2. Validate the address: The OS checks if the address is within the process's valid address space (e.g., in the heap, stack, or a mapped file). If not, it sends a segmentation fault signal to the process.
  3. Check the page table entry: The OS examines the PTE to see why the page is not present. It may be a demand-zero page, a file-backed page, or a swapped-out page.
  4. Find a free physical frame: The OS selects a physical page frame. If none are free, it may evict a victim page using a replacement algorithm (e.g., LRU, clock).
  5. Load the page: For file-backed pages, the OS issues a disk read to load the content. For demand-zero pages, it zeroes the frame. For swapped-out pages, it reads from the swap area.
  6. Update the page table: The PTE is set to point to the new physical frame, and the present bit is set. The OS also updates any metadata (e.g., modified bit).
  7. Resume the process: The OS returns from the exception handler, and the CPU retries the instruction that caused the fault. This time the translation succeeds.

During the disk I/O, the process is typically put to sleep (blocked) so the CPU can run other processes. This is why page faults are expensive—they can take milliseconds, while a memory access takes nanoseconds.

Page Replacement and Thrashing

When physical memory is full, the OS must choose a page to evict. Common algorithms include:

  • Least Recently Used (LRU): Evicts the page not accessed for the longest time. Hardware support is often limited, so approximations like the clock algorithm are used.
  • Clock (Second Chance): A circular list of pages with a reference bit. The OS cycles through, clearing bits and evicting the first page with a cleared bit.
  • Working Set Model: Keeps track of pages actively used to avoid evicting pages a process will need soon.

If the system spends more time paging than executing, it is thrashing. This happens when the combined working sets of all processes exceed physical memory. Symptoms include high disk I/O and near-zero CPU utilization. The OS may reduce the degree of multiprogramming by swapping out entire processes.

One common misconception is that a page fault always means a disk access. That is false for minor faults, such as when the page is already in memory but not mapped to the process (e.g., after fork with copy-on-write). Only major faults involve disk I/O. Another misconception is that page faults are always bad. They are a normal part of demand paging; without them, the OS would have to pre-load all pages, wasting memory and slowing startup.

Another question is whether a page fault can happen during the page fault handler itself. Yes, the kernel's own code and data can fault, but the kernel uses special mechanisms like atomic page tables and pre-allocated memory for its handler to avoid recursive faults.

Performance Impact and Optimization

Page faults are a major factor in system performance. Each major fault involves a disk seek and transfer, which is orders of magnitude slower than RAM. To reduce faults, OSes use:

  • Prefetching: Loading pages that are likely to be needed soon (e.g., sequential file access).
  • Clustering: Reading multiple pages at once to amortize disk latency.
  • Memory-mapped files: Using the same page cache for file I/O, so repeated accesses don't cause new faults.
  • Huge pages: Using larger page sizes (e.g., 2 MB) to reduce TLB misses and page table overhead.

Applications can also influence behavior by using mlock() to pin pages in memory, though this is usually reserved for real-time or high-security scenarios.

Frequently Asked Questions

What is the difference between a minor and major page fault?

A minor fault occurs when the page is already in physical memory but not mapped in the process's page table (e.g., a copy-on-write page). No disk I/O is needed. A major fault requires reading the page from disk, which is much slower.

Can a page fault be avoided entirely?

No, demand paging relies on page faults to load pages lazily. Pre-loading all pages would defeat the purpose of virtual memory and waste resources. However, you can reduce faults by using memory-mapped files, increasing memory, or using smaller working sets.

What happens if the OS cannot find a free page frame?

It evicts a page using a replacement algorithm. If the page has been modified, it must be written back to disk before being reused. This is called a dirty page. The eviction process itself can cause additional I/O.

Why does a page fault cause a process to slow down?

Because the process must wait for disk I/O to complete. During that time, the OS may schedule other processes, but the faulting process is blocked. The latency of a disk read is typically 5–10 milliseconds, versus nanoseconds for memory access.

How does the OS know if a page fault is valid?

It checks the virtual address against the process's address space layout. If the address is in a mapped region (e.g., heap, stack, or file mapping), the fault is valid. If not, it's a segmentation fault and the process is terminated.

What is the role of the TLB in page faults?

The TLB (translation lookaside buffer) caches recent virtual-to-physical translations. A TLB miss is not a page fault; it just means the translation is not cached. The MMU then walks the page table. A page fault only occurs if the page table entry says the page is not present.