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 Type | Action |
|---|---|
| Demand Zero | Allocate a new frame, fill with zeros |
| Demand Fill | Read the page from the executable file on disk |
| Swap | Read 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:
- Allocate a new page frame.
- Copy the contents of the shared page to the new frame.
- Update the writing process's page table to point to the new frame.
- Set the new page as read-write.
- 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
| Type | Description | Disk I/O? |
|---|---|---|
| Minor Fault | Page reclaimed from free list or COW copy | No |
| Major Fault | Page loaded from swap or executable file | Yes |
- 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).