100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is a Page Fault?

Page faults explained — minor vs major faults, the MMU trap, page tables, and thrashing — with an interview-ready answer and code example.

mediumQ10 of 224 in Operating Systems Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

A page fault is a hardware trap raised by the MMU when a running program accesses a virtual memory page that is not currently mapped to a physical frame, forcing the operating system to step in before execution can continue.

Virtual memory lets a process address more memory than physically exists by keeping only the working set of pages in RAM and the rest on disk or unallocated. When the CPU touches an address whose page-table entry is marked invalid, the MMU raises an exception and the OS page-fault handler runs: it locates the page (in the swap file, the backing executable, or nowhere yet), finds or evicts a physical frame, loads the data if needed, updates the page table, and resumes the instruction. A minor fault just needs a fresh frame (e.g. first touch of a zero-filled or already-cached page); a major fault requires a slow disk read. Too many major faults in a row is called thrashing, where the system spends more time paging than executing.

  • Lets processes use more memory than physically installed
  • Enables lazy loading — pages are fetched only when touched
  • Supports copy-on-write and shared memory efficiently
  • Isolates processes since each has its own page table

AI Mentor Explanation

A batsman calls for a specific bat from the dressing room only when he actually needs it, rather than carrying every bat onto the field. If that bat is not already in the players’ area, an attendant has to run back to the store room to fetch it, which pauses play briefly. That pause is the page fault — the request stalls until the needed item is physically brought to where it is used.

Step-by-Step Explanation

  1. Step 1

    Access

    CPU generates a virtual address whose page-table entry is marked not-present or invalid.

  2. Step 2

    Trap

    The MMU raises a page-fault exception, transferring control to the OS fault handler.

  3. Step 3

    Resolve

    The OS finds a free frame (or evicts one), loads the page from disk/backing store if needed, and updates the page table.

  4. Step 4

    Resume

    The faulting instruction is restarted and now succeeds because the page is resident and mapped.

What Interviewer Expects

  • Clear distinction between virtual and physical memory
  • Minor vs major fault distinction
  • The role of the page table and MMU in triggering the trap
  • Awareness that excessive faulting leads to thrashing

Common Mistakes

  • Calling every page fault an error or crash
  • Confusing a page fault with a segmentation fault
  • Ignoring the difference between minor and major faults
  • Not mentioning that the instruction is retried after the fault is resolved

Best Answer (HR Friendly)

A page fault happens when a program tries to use a piece of memory that is not currently loaded into RAM. The operating system pauses that program just long enough to fetch the missing piece, update its records, and let the program carry on as if nothing happened.

Code Example

Touching unmapped memory triggers a fault under the hood
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    /* malloc reserves virtual address space, but pages are not
       backed by physical frames until first touched. */
    size_t n = 1024 * 1024; /* 1M longs, several pages */
    long *buf = malloc(n * sizeof(long));
    if (!buf) return 1;

    /* First write to each page causes a minor page fault the
       first time that page is touched; the OS maps a fresh
       zero-filled frame and the write completes. */
    for (size_t i = 0; i < n; i += 4096 / sizeof(long)) {
        buf[i] = (long)i;   /* may fault here on first touch */
    }

    printf("buf[0]=%ld\n", buf[0]);
    free(buf);
    return 0;
}

Follow-up Questions

  • What is the difference between a minor and a major page fault?
  • What is thrashing and how does the OS detect it?
  • How does the page replacement algorithm decide which page to evict?
  • What is the difference between a page fault and a segmentation fault?

MCQ Practice

1. A page fault is raised by which component?

The memory management unit detects the invalid page-table entry and raises the trap.

2. A major page fault differs from a minor one because it requires?

A major fault means the page must be read from slower backing storage such as disk.

3. Excessive page faulting that degrades performance is called?

Thrashing occurs when the system spends more time servicing faults than doing useful work.

Flash Cards

What triggers a page fault?Accessing a virtual page with no valid mapping to a physical frame.

Minor vs major fault?Minor: frame allocated without disk I/O. Major: page read from disk.

What happens after the fault handler runs?The faulting instruction is restarted and now succeeds.

What is thrashing?Excessive major faulting where the system spends more time paging than executing.

1 / 4

Continue Learning