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
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
Case 2 — Page Swapped Out
The required page exists on disk instead of RAM.
Case 3 — Permission Violation
Program attempts
const int x = 10;
x = 20;
or
tries to write into a read-only page.
Result
Case 4 — Invalid Address
Program accesses memory that does not belong to it.
int *p = NULL;
*p = 5;
Result
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
No disk access.
4. Major Page Fault
Major page faults are much slower.
The page is not present in RAM.
Operating system must
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 size500 MB
Actually used25 MB
Without demand pagingLoad 500 MB
With demand pagingLoad Only Needed Pages
This saves memory and reduces startup time.
Demand Paging Flow
6. Lazy Allocation
Closely related to demand paging.
Example
malloc(1 GB)
Operating system may reserve1 GB Virtual Address Space
without immediately allocating1 GB Physical RAM
Physical pages are allocated only when accessed.
7. Copy-on-Write (COW)
Copy-on-Write avoids unnecessary memory copying.
Example
Create Process B using fork().
Instead of copying memory
Both processes share the same physical page.
If one process modifies it
Only then is a new physical page allocated.
Why Copy-on-Write?
Without COW
Very slow.
With COW
Much faster.
8. Swapping
Suppose RAM becomes full.
Operating system may move inactive pages
from RAM
to disk.
Later
If needed again
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
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
RAM4 GB
Applications require20 GB
Operating system constantly
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
RAM8 GB
Applications reserve20 GB
This works because most applications never use all reserved memory simultaneously.
12. Linux Page Fault Handling (Simplified)
If the access is invalid
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 allocates2 GB
Touches only5 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
- What is a page fault?
- What is the difference between a minor and major page fault?
- Why is demand paging useful?
- What is lazy allocation?
- How does Copy-on-Write improve performance?
- What is swapping?
- Why are page replacement algorithms needed?
- What is thrashing?
- Why do operating systems allow memory overcommit?
- Why isn't every page fault considered an error?
Concept Mastery Assessment — Module 12
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.