ADVERTISEMENT
Responsive Ad Unit • Systems Architecture & Low-Level Engineering

Module 13 — Page Faults & Demand Paging

Learning Objectives

By the end of this module, you will understand:

  • What is a page fault?
  • Why page faults occur
  • Minor vs Major page faults
  • Demand Paging
  • Lazy Allocation
  • Copy-on-Write (COW)
  • Swapping
  • Page Replacement
  • Thrashing
  • Memory Overcommit
  • Linux page fault handling
  • Performance considerations

1. What is a Page Fault?

A page fault occurs when the CPU tries to access a virtual memory page that cannot be used immediately.
This does not necessarily mean an error.
A page fault simply means:

"The requested virtual page is not currently available for direct access."

The operating system decides how to handle the situation.

Basic Flow

CPU
↓
Virtual Address
↓
MMU
↓
Page Table
↓
Page Not Immediately Accessible
↓
Page Fault
↓
Operating System
↓
Handle Fault
↓
Resume Program

2. Why Do Page Faults Occur?

Several situations can trigger a page fault.

Case 1 — Page Not Loaded Yet

Program accesses memory for the first time.

Example

int *arr = malloc(1000000);
arr[0] = 10;

Memory is reserved.
Physical memory may not yet be allocated.
First access causes

Page Fault
↓
Allocate Physical Page
↓
Continue Execution

Case 2 — Page Swapped Out

The required page exists on disk instead of RAM.

Virtual Page
↓
Disk
↓
Page Fault
↓
Load into RAM

Case 3 — Permission Violation

Program attempts

const int x = 10;
x = 20;

or
tries to write into a read-only page.

Result

Page Fault
↓
Permission Check
↓
Process Terminated

Case 4 — Invalid Address

Program accesses memory that does not belong to it.

int *p = NULL;
*p = 5;

Result

Page Fault
↓
Invalid Mapping
↓
Segmentation Fault

3. Minor Page Fault

Minor page faults are relatively inexpensive.
The page already exists in memory.
The operating system only needs to update page tables or establish the mapping.

Example

Page already in RAM
↓
Mapping Created
↓
Continue

No disk access.

4. Major Page Fault

Major page faults are much slower.
The page is not present in RAM.
Operating system must

Read Page
↓
Disk
↓
RAM
↓
Update Page Table
↓
Resume Process

Disk access makes major page faults significantly more expensive.

Comparison

Minor Page Fault Major Page Fault
No Disk Access Disk Access Required
Fast Slow
Page Already Exists Page Must Be Loaded

5. Demand Paging

Modern operating systems do not load every page immediately.
Instead
Pages are loaded
only when actually accessed.
This is called
Demand Paging.

Example

Program size
500 MB

Actually used
25 MB

Without demand paging
Load 500 MB

With demand paging
Load Only Needed Pages

This saves memory and reduces startup time.

Demand Paging Flow

Program Starts
↓
Virtual Memory Reserved
↓
First Access
↓
Page Fault
↓
Load Page
↓
Continue

6. Lazy Allocation

Closely related to demand paging.

Example

malloc(1 GB)

Operating system may reserve
1 GB Virtual Address Space
without immediately allocating
1 GB Physical RAM

Physical pages are allocated only when accessed.

7. Copy-on-Write (COW)

Copy-on-Write avoids unnecessary memory copying.

Example

Process A
↓
Page
↓
Data

Create Process B using fork().
Instead of copying memory

Process A
↓
Shared Page
↑
Process B

Both processes share the same physical page.
If one process modifies it

Write Attempt
↓
Page Fault
↓
Create Private Copy
↓
Continue

Only then is a new physical page allocated.

Why Copy-on-Write?

Without COW

fork()
↓
Copy Entire Memory

Very slow.

With COW

fork()
↓
Share Pages
↓
Copy Only When Modified

Much faster.

8. Swapping

Suppose RAM becomes full.
Operating system may move inactive pages
from RAM
to disk.

RAM
↓
Disk
↓
Free RAM

Later
If needed again

Disk
↓
RAM
↓
Continue

This is called
Swapping.

9. Page Replacement

Suppose RAM is full.
A new page must be loaded.
Which page should be removed?
Operating systems use
Page Replacement Algorithms.

Common algorithms:

  • FIFO
  • LRU
  • Clock
  • LFU (less common)

FIFO

First page loaded
↓
First page removed
Simple
But not always optimal.

LRU (Least Recently Used)

Remove
the page that has not been used for the longest time.

Example

Recently Used
↓
Keep
Oldest
↓
Replace

Widely used as a concept, though practical systems often use approximations.

Clock Algorithm

Uses a circular list.
Each page has a reference bit.
Recently used pages get another chance.
Efficient approximation of LRU.

10. Thrashing

Suppose
RAM
4 GB

Applications require
20 GB

Operating system constantly

Load Page
↓
Remove Page
↓
Load Page
↓
Remove Page

CPU spends more time managing memory than executing programs.
This is called
Thrashing.

Symptoms

  • Very slow system
  • High disk activity
  • Low CPU utilization
  • Frequent page faults

11. Memory Overcommit

Many operating systems allow programs to reserve more virtual memory than physical RAM.

Example

RAM
8 GB

Applications reserve
20 GB

This works because most applications never use all reserved memory simultaneously.

12. Linux Page Fault Handling (Simplified)

CPU
↓
Page Fault
↓
Kernel
↓
Check Mapping
↓
Allocate or Load Page
↓
Update Page Table
↓
Resume Process

If the access is invalid

Kernel
↓
Terminate Process
↓
Segmentation Fault

13. Performance Considerations

Good programs:

  • Access memory sequentially
  • Reuse data
  • Reduce unnecessary allocations
  • Avoid excessive page faults

Poor memory access patterns can significantly reduce performance.

Real Example

Suppose
Program allocates
2 GB

Touches only
5 MB

Demand paging loads only those accessed pages.
Result:

  • Lower RAM usage
  • Faster startup
  • Better overall efficiency

Interview Questions

Beginner

  • What is a page fault?
  • Why do page faults occur?
  • Difference between minor and major page faults?
  • What is demand paging?

Intermediate

  • Explain Copy-on-Write.
  • What is lazy allocation?
  • What is swapping?
  • Why is demand paging beneficial?

Senior

  • Explain the complete page fault handling sequence.
  • Why is fork() efficient with Copy-on-Write?
  • What is thrashing?
  • Compare FIFO, LRU, and Clock algorithms.
  • How does memory overcommit work?

Common Mistakes

  • ❌ Thinking every page fault is an error. Many page faults are expected and handled normally.
  • ❌ Assuming malloc() immediately allocates physical RAM. It often reserves virtual address space first.
  • ❌ Believing Copy-on-Write duplicates memory immediately. Copying happens only when a shared page is modified.
  • ❌ Thinking swapping and paging are identical. Paging manages fixed-size memory pages; swapping generally refers to moving pages between RAM and secondary storage.

Summary

Page faults are a normal part of virtual memory systems. They enable demand paging, lazy allocation, and Copy-on-Write, allowing operating systems to use physical memory efficiently while providing large virtual address spaces. Proper page replacement policies and careful memory management help maintain system performance and prevent excessive paging or thrashing.

Mini Quiz

  1. What is a page fault?
  2. What is the difference between a minor and major page fault?
  3. Why is demand paging useful?
  4. What is lazy allocation?
  5. How does Copy-on-Write improve performance?
  6. What is swapping?
  7. Why are page replacement algorithms needed?
  8. What is thrashing?
  9. Why do operating systems allow memory overcommit?
  10. Why isn't every page fault considered an error?
Knowledge Check

Concept Mastery Assessment — Module 12

Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.

Question 1 of 12
What is demand paging in virtual memory operating systems?
Explanation
Demand paging defers loading virtual pages into physical RAM frames until an instruction attempts to access an unmapped page, triggering a page fault.
Question 2 of 12
What hardware exception triggers demand paging when a process accesses a page with Present bit = 0?
Explanation
When an address translation encounters Present = 0 in the PTE, the MMU halts execution and triggers a Page Fault exception (vector 14 on x86).
Question 3 of 12
What is the critical distinction between a 'minor' (soft) page fault and a 'major' (hard) page fault in Linux?
Explanation
Minor page faults occur when the physical page is already resident in RAM (e.g., shared library cache or COW). Major faults require synchronous disk reads.
Question 4 of 12
Which theoretical page replacement algorithm has the lowest possible page-fault rate on all reference strings?
Explanation
The Optimal algorithm (OPT) replaces the page that will not be used for the longest period of time in the future, serving as an unrealizable benchmark.
Question 5 of 12
What is Belady's Anomaly in operating systems page replacement?
Explanation
Belady's anomaly occurs in FIFO replacement where granting more physical frames leads to more page faults for certain reference strings.
Question 6 of 12
How does the Clock (Second-Chance) page replacement algorithm approximate LRU in hardware/software?
Explanation
The Clock algorithm uses a circular hand inspecting page reference bits. If bit is 1, it is cleared to 0; if bit is already 0, the page is evicted.
Question 7 of 12
What is 'thrashing' in an operating system virtual memory subsystem?
Explanation
Thrashing happens when the total working sets of active processes exceed available physical RAM, resulting in perpetual page swapping and system stall.
Question 8 of 12
What does Peter Denning's 'Working Set Model' define?
Explanation
The working set W(t, delta) is the collection of pages accessed by a process in the preceding delta time units. If RAM cannot hold it, thrashing ensues.
Question 9 of 12
What happens if a process accesses a virtual memory address that is completely outside any valid virtual memory area (VMA)?
Explanation
Accessing an unallocated address causes the kernel page fault handler to discover no matching struct vm_area_struct, resulting in SIGSEGV.
Question 10 of 12
What is the purpose of the Linux 'kswapd' kernel daemon?
Explanation
kswapd is the background page-out daemon that monitors memory pressure watermarks and reclaims pages to maintain a reserve of free frames.
Question 11 of 12
What is Linux zswap / zram technology?
Explanation
zswap/zram intercepts swap candidates and compresses them into a dynamically allocated RAM pool, reducing flash wear and disk swap latency.
Question 12 of 12
What is the 'OOM Killer' (Out-Of-Memory Killer) in Linux?
Explanation
When the system enters extreme memory starvation, the OOM Killer scores processes and terminates selected tasks (via SIGKILL) to save the kernel.
0 / 12 (0%)
Great job! Review the concepts above and proceed when ready.
ADVERTISEMENT
Responsive Ad Unit • Technical Courses & Certifications