Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Page Fault Handler

Lesson 32 of 32 in the free Design of Unix Operating System notes on Siksha Sarovar, written by Rohit Jangra.

Introduction

A page fault occurs when a process tries to access a page that is not currently in physical memory (validity fault) or tries an illegal access (protection fault). The kernel's page fault handler intercepts these events and takes appropriate action.

Types of Page Faults

---

1. Validity Fault (Major Page Fault)

  • Occurs when the valid bit (V) = 0 in the page table entry.
  • The page exists but is not in physical memory — it is on the swap device or in the executable file.
  • The fault handler must load the page into memory from disk.

2. Protection Fault

  • Occurs when the process tries to access a page in a way that violates its permissions.
  • Examples:
  • Writing to a read-only page (e.g., text segment).
  • Accessing a page marked for copy-on-write.
  • The action depends on whether the fault is recoverable:
  • Copy-on-Write: The kernel makes a private copy of the page → fault is resolved.
  • Illegal Access: The kernel sends a SIGSEGV (segmentation fault) signal to the process.

---

Validity Fault Handler Algorithm

When a process accesses a page with V = 0:

Step 1: Check if the page address is legal.

  • Is the virtual address within the process's valid address range?
  • If no → send SIGSEGV to the process (segmentation fault).
  • If yes → continue.

Step 2: Check if the page frame is on the free list (page reclaim).

  • The page may have been recently freed by the page stealer but the frame hasn't been reused yet.
  • If the frame is still on the free list with valid data → reclaim it (remove from free list, restore the page table entry, set V = 1).
  • No disk I/O needed! This is very fast.

Step 3: Page is not reclaimable — load from disk.

  • Determine the page source from the disk block descriptor:
Source TypeAction
Demand ZeroAllocate a new frame, fill with zeros
Demand FillRead the page from the executable file on disk
SwapRead the page from the swap device
  • 3a: Allocate a free page frame.
  • If no free frames → invoke the page stealer to free some frames, then retry.
  • 3b: Initiate disk read — the process sleeps while waiting for I/O.
  • 3c: When I/O completes:
  • Update the page table entry: set the physical frame number, set V = 1.
  • Wake up the process.

Step 4: Resume the faulting instruction.

  • The hardware re-executes the instruction that caused the fault.
  • This time, the page is in memory, so no fault occurs.

---

Validity Fault Handler Flowchart

Page Fault (V = 0)
      │
      ▼
Is address valid?
  No ──► SIGSEGV (kill process)
  Yes ─► Is page frame on free list? (Page Reclaim)
            Yes ──► Reclaim frame, set V=1, resume
            No  ──► What type of page?
                      │
                ┌─────┼──────────┐
                ▼     ▼          ▼
          Demand   Demand     From
           Zero     Fill      Swap
              │     │          │
              ▼     ▼          ▼
         Alloc    Read from   Read from
         frame,   executable  swap device
         zero it   file
              │     │          │
              └─────┼──────────┘
                    ▼
            Update PTE (V=1, frame #)
                    │
                    ▼
            Resume instruction

---

Protection Fault Handler

When a process makes an access that violates page permissions:

Step 1: Determine the type of protection fault.

Case A — Copy-on-Write (COW):

  • After fork(), parent and child share the same physical pages.
  • Pages are marked read-only with the copy-on-write bit set.
  • When either process tries to write, a protection fault occurs:
  1. Allocate a new page frame.
  2. Copy the contents of the shared page to the new frame.
  3. Update the writing process's page table to point to the new frame.
  4. Set the new page as read-write.
  5. If the original page's reference count drops to 1, the remaining process can also get read-write access.
Before write (COW):
Parent PTE → Frame 50 (R-only, COW) ◄─── Shared
Child  PTE → Frame 50 (R-only, COW) ◄───┘

Child writes → Protection Fault!

After COW handling:
Parent PTE → Frame 50 (RW)    ← Original
Child  PTE → Frame 99 (RW)    ← New copy

Case B — Illegal Access:

  • The process tries to write to a genuinely read-only page (e.g., text segment) or access a page it has no permission to access.
  • The kernel sends SIGSEGV → the process is terminated (core dump).

---

Minor vs Major Page Faults

TypeDescriptionDisk I/O?
Minor FaultPage reclaimed from free list or COW copyNo
Major FaultPage loaded from swap or executable fileYes
  • Minor faults are fast (microseconds) — no disk I/O.
  • Major faults are slow (milliseconds) — require disk read.
  • Performance depends on minimizing major faults.

Summary

  • Validity faults occur when a page is not in memory (V=0) — the handler loads it from disk.
  • Protection faults occur on illegal access — handled by COW or SIGSEGV.
  • Page reclaim avoids disk I/O by recovering recently freed pages.
  • Pages can come from three sources: demand zero, executable file, or swap.
  • Copy-on-Write delays copying shared pages until a write actually occurs.
  • Minor faults (no I/O) are much faster than major faults (disk I/O).