1. Introduction
Virtual memory allows programs to be larger than physical memory, but an important question arises:
When should a page be loaded into memory?
Loading all pages at once defeats the purpose of virtual memory because it wastes memory and increases startup time.
Modern operating systems solve this problem using Demand Paging.
Demand paging is a memory management technique in which pages are loaded into memory only when they are actually needed during execution.
2. What is Demand Paging?
Demand paging is a memory management scheme where a page is brought into main memory only when it is first referenced by a process.
Key Idea
Page Not Loaded
↓
Page Accessed
↓
Load Page into Memory
A page remains on disk until it is actually required.
3. Why Demand Paging is Needed
3.1 Efficient Memory Utilization
Many parts of a program may never execute.
Loading them wastes RAM.
3.2 Faster Program Startup
Programs can begin execution without loading the entire address space.
3.3 Support for Large Programs
Programs larger than RAM can execute because only active pages are loaded.
3.4 Better Multiprogramming
More processes can coexist in memory.
4. Basic Concept
A process is divided into pages.
Initially:
Some pages may be loaded
Many pages remain on disk
Whenever the CPU accesses a page:
If page is in memory
Access Page
↓
Continue Execution
If page is not in memory
Access Page
↓
Page Fault
↓
Load Page
↓
Continue Execution
5. Components of Demand Paging
5.1 Page Table
Stores page-to-frame mappings.
Each page table entry contains a:
Frame Number
Valid/Invalid Bit
Protection Information
5.2 Valid/Invalid Bit
The most important field for demand paging.
Valid Bit = 1
Page Present in Memory
Invalid Bit = 0
Page Located on Disk
6. Role of the Valid/Invalid Bit
Suppose a process accesses Page 3.
Page Table:
| Page | Frame | Valid Bit |
|---|---|---|
| 0 | 5 | 1 |
| 1 | 2 | 1 |
| 2 | 8 | 1 |
| 3 | - | 0 |
Since Page 3 has:
Valid Bit = 0
the MMU generates a page fault.
7. Backing Store
Pages not currently present in RAM are stored in a special disk area called the Backing Store.
Characteristics:
Large capacity
Slower than RAM
Managed by operating system
Concept
RAM
↓
Active Pages
Disk
↓
Inactive Pages
8. Working of Demand Paging
Step 1: CPU Generates Logical Address
Example:
Logical Address
(Page Number, Offset)
Example:
(Page 5, Offset 120)
Step 2: MMU Consults Page Table
The page table entry for Page 5 is examined.
Step 3: Check Valid Bit
Case 1: Valid Bit = 1
Page is present in RAM.
Translate Address
↓
Access Memory
↓
Continue Execution
Case 2: Valid Bit = 0
Page not present.
Page Fault Generated
9. What is a Page Fault?
A page fault is an exception generated when a process attempts to access a page that is not currently in main memory.
Definition
A page fault occurs when the referenced page is absent from physical memory.
Important Insight
A page fault is not an error.
It is a normal and expected event in demand paging systems.
10. Page Fault Handling
When a page fault occurs, control transfers to the operating system.
Step 1: Trap to Operating System
Hardware detects missing page.
Page Fault
↓
OS Invoked
Step 2: Validate Access
The OS checks whether the reference is legal.
Legal Reference
Continue handling.
Illegal Reference
Terminate Process
11. Locate Required Page
The operating system determines where the page is stored on disk.
Page Number
↓
Disk Location
12. Find a Free Frame
The operating system searches for an available frame.
If Free Frame Exists
Use it.
If No Free Frame Exists
A page replacement algorithm selects a victim page.
Victim Page
↓
Remove from RAM
↓
Free Frame Created
13. Load Page into Memory
The required page is read from disk into the selected frame.
Disk
↓
Frame in RAM
14. Update Page Table
The page table is modified.
Changes:
Frame Number updated
Valid Bit set to 1
Example:
| Page | Frame | Valid |
|---|---|---|
| 5 | 12 | 1 |
15. Restart Instruction
The faulting instruction is restarted.
Page Loaded
↓
Instruction Re-executed
↓
Execution Continues
16. Complete Page Fault Flow
CPU References Page
↓
Page Table Lookup
↓
Valid Bit = 0
↓
Page Fault
↓
Trap to OS
↓
Locate Page on Disk
↓
Find Free Frame
↓
Load Page into RAM
↓
Update Page Table
↓
Restart Instruction
17. Types of Page Faults
17.1 Minor Page Fault
The page is already present in memory but mappings need adjustment.
Characteristics:
No disk access
Fast handling
17.2 Major Page Fault
The page must be fetched from disk.
Characteristics:
Requires disk I/O
Much slower
18. Demand Paging Example
Suppose a process contains:
Page 0
Page 1
Page 2
Page 3
Page 4
Initially RAM contains:
Page 0
Page 1
Process accesses:
Page 3
Result:
Page 3 Not Present
↓
Page Fault
↓
Load Page 3
↓
Continue Execution
19. Performance of Demand Paging
The most important factor is the Page Fault Rate.
Let:
p = Probability of Page Fault
Then:
0 ≤ p ≤ 1
20. Effective Access Time (EAT)
The average memory access time is:
Where:
MAT = Memory Access Time
PF_Time = Page Fault Service Time
Key Observation
Since disk access is extremely slow:
Even a small increase in page fault rate causes a significant performance drop.
21. Why Page Faults Are Expensive
Normal Memory Access:
Nanoseconds
Disk Access:
Milliseconds
Since:
1 ms = 1,000,000 ns
Page faults are millions of times slower than normal memory accesses.
22. Advantages of Demand Paging
22.1 Reduced Memory Usage
Only required pages occupy RAM.
22.2 Faster Program Startup
No need to load entire process.
22.3 Better Multiprogramming
More processes fit in memory.
22.4 Support for Large Applications
Programs larger than RAM can execute.
22.5 Efficient Resource Utilization
Unused pages remain on disk.
23. Disadvantages of Demand Paging
23.1 Page Fault Overhead
Handling faults requires expensive disk operations.
23.2 Increased Complexity
Requires:
MMU
Page Tables
Fault Handlers
Replacement Algorithms
23.3 Unpredictable Delays
Page faults may occur during execution.
23.4 Thrashing Risk
Excessive page faults can severely degrade performance.
24. Demand Paging and Thrashing
If too many page faults occur:
Page Fault
↓
Load Page
↓
Replace Page
↓
Another Page Fault
↓
Load Again
↓
Repeat
The system spends more time swapping pages than executing instructions.
This condition is called Thrashing.
25. Demand Paging vs Swapping
| Feature | Demand Paging | Swapping |
|---|---|---|
| Unit Transferred | Page | Entire Process |
| Memory Usage | Efficient | Less Efficient |
| Overhead | Lower | Higher |
| Flexibility | High | Low |
| Modern Usage | Common | Rare |
26. Real-World Analogy
Think of streaming a movie.
Traditional loading:
Download Entire Movie
↓
Watch
Demand paging:
Watch Beginning
↓
Download Next Part Only When Needed
↓
Continue Watching
Only the required portions are loaded at any given time.
One-Line Conclusion
Demand paging loads pages only when they are needed, reducing memory usage and enabling virtual memory, but at the cost of page fault handling overhead.