Discover what is demand paging in operating systems — its working, advantages, disadvantages, and real-life examples explained simply for beginners.
When you start learning about memory management in operating systems, one of the first concepts that can sound tricky yet fascinating is demand paging.
So, what is demand paging, and why does it matter in modern computing?
What is Demand Paging?
Demand paging is a memory management technique used by operating systems to load pages into physical memory only when they are needed during program execution.
In simpler terms, instead of loading the entire program into RAM at once, the operating system loads only the required parts (pages) on demand.
This approach helps in saving memory, reducing load time, and allowing multiple programs to run efficiently at the same time.
Think of it like this:
When you open a large book, you don’t read every page right away — you open one page at a time as you go.
That’s exactly how demand paging works with program pages.
How Demand Paging Works
Here’s the step-by-step process of how demand paging functions inside the system:
- Program Execution Begins:
When a process starts, only a small part of it (like the first few pages) is loaded into memory. - Page Fault Occurs:
If the CPU tries to access a page that isn’t in physical memory, the operating system detects a page fault. - Page Brought from Secondary Storage:
The required page is then fetched from secondary storage (usually the hard disk) into RAM. - Page Table Update:
The OS updates the page table to mark that the page is now available in memory. - Execution Resumes:
The CPU resumes the process as if the page had been there all along.
This entire process happens so fast that the user usually doesn’t notice it.
Components Involved in Demand Paging
To understand what is demand paging completely, you should know about its core components:
- Page Table: Keeps track of whether a page is in memory or on disk.
- Valid/Invalid Bit: Each page table entry contains a bit indicating if the page is valid (in memory) or invalid (on disk).
- Secondary Storage: Stores the pages that are not currently in RAM.
- Page Fault Handler: Handles the process of fetching the missing page when a page fault occurs.
Advantages of Demand Paging
Let’s look at the benefits that make demand paging a popular approach in operating systems:
Efficient Memory Usage:
Only necessary pages are loaded, which saves RAM space.
Faster Startup Time:
Programs can start running immediately without loading the entire code into memory.
Better Multiprogramming:
Since memory is used efficiently, more processes can run simultaneously.
Reduced I/O Overhead:
Only needed pages are transferred between disk and memory.
Disadvantages of Demand Paging
While demand paging is powerful, it’s not perfect. Here are some of its downsides:
Page Fault Overhead:
Each page fault causes a delay because the OS needs to fetch the missing page from disk.
Thrashing:
If too many page faults occur, the system may spend most of its time swapping pages instead of executing programs.
Complex Implementation:
Maintaining page tables and handling faults increases the complexity of the OS.
Real-Life Example of Demand Paging
Imagine you’re using a video editing app.
When you open it, the operating system doesn’t load all the tools, filters, and resources into memory at once.
Instead, it loads them on demand — when you click on a specific feature.
This helps your system stay fast and responsive, even for heavy applications.
That’s demand paging in action — efficient, smart, and user-friendly.
C Program: Demand Paging Simulation
#include
#include
#include
#define TOTAL_PAGES 10 // Total pages in program
#define FRAME_SIZE 4 // Number of pages that can be in memory at once
// Function to simulate loading a page into memory
void loadPage(int pageNumber, int memory[], bool inMemory[]) {
printf("Page fault: Loading page %d into memory.\n", pageNumber);
// Find an empty frame or replace first page (FIFO replacement)
bool loaded = false;
for (int i = 0; i < FRAME_SIZE; i++) {
if (memory[i] == -1) {
memory[i] = pageNumber;
inMemory[pageNumber] = true;
loaded = true;
break;
}
}
if (!loaded) {
printf("Memory full! Replacing page %d with page %d.\n", memory[0], pageNumber);
inMemory[memory[0]] = false;
memory[0] = pageNumber;
inMemory[pageNumber] = true;
}
}
// Function to simulate accessing a page
void accessPage(int pageNumber, int memory[], bool inMemory[]) {
printf("\nAccessing page %d...\n", pageNumber);
if (inMemory[pageNumber]) {
printf("Page %d is already in memory. No page fault.\n", pageNumber);
} else {
loadPage(pageNumber, memory, inMemory);
}
}
int main() {
int memory[FRAME_SIZE];
bool inMemory[TOTAL_PAGES];
// Initialize memory and inMemory flags
for (int i = 0; i < FRAME_SIZE; i++) memory[i] = -1;
for (int i = 0; i < TOTAL_PAGES; i++) inMemory[i] = false;
// Simulated sequence of page accesses
int pageRequests[] = {0, 2, 1, 3, 0, 4, 2, 5, 1, 0};
int numRequests = sizeof(pageRequests) / sizeof(pageRequests[0]);
for (int i = 0; i < numRequests; i++) {
accessPage(pageRequests[i], memory, inMemory);
printf("Current Memory: ");
for (int j = 0; j < FRAME_SIZE; j++) {
if (memory[j] != -1) printf("%d ", memory[j]);
else printf("_ ");
}
printf("\n");
}
return 0;
}
Explanation of This Code
- This simulates demand paging using page requests.
memory[]array represents the physical memory frames.inMemory[]keeps track of which pages are currently in memory.- Whenever a page is accessed:
- If it’s already in memory → no page fault.
- If it’s not in memory → page fault occurs, and it is loaded into memory.
- Uses FIFO (First-In-First-Out) page replacement for simplicity.
Example Output of Demand Paging
Accessing page 0...
Page fault: Loading page 0 into memory.
Current Memory: 0 _ _ _
Accessing page 2...
Page fault: Loading page 2 into memory.
Current Memory: 0 2 _ _
Accessing page 1...
Page fault: Loading page 1 into memory.
Current Memory: 0 2 1 _
Accessing page 3...
Page fault: Loading page 3 into memory.
Current Memory: 0 2 1 3
Accessing page 0...
Page 0 is already in memory. No page fault.
Current Memory: 0 2 1 3
...
Difference Between Demand Paging and Pre-Paging
| Feature | Demand Paging | Pre-Paging |
|---|---|---|
| Loading Strategy | Loads pages only when needed | Loads pages in advance |
| Memory Usage | More efficient | Might load unnecessary pages |
| Performance | Can cause delays due to page faults | Reduces page faults but may waste memory |
| Best Use Case | When working sets are small | When page access patterns are predictable |
Why Demand Paging Matters
In modern systems like Windows, Linux, and QNX, demand paging ensures that applications run efficiently without exhausting system memory.
It also plays a crucial role in virtual memory management, allowing systems to run large applications even with limited physical RAM.
If you want to dive deeper into how operating systems handle process memory efficiently, you can explore this guide on differences between process and thread — it helps you understand how processes and threads share memory space.
Summary of Demand Paging
- Demand paging is a memory management technique where pages are loaded only when needed.
- It helps optimize RAM usage and improve multitasking performance.
- However, too many page faults can slow down the system (thrashing).
- It’s widely used in modern operating systems to balance performance and efficiency.
Demand Paging Key Takeaway
The next time someone asks you, “What is demand paging?”, you can confidently say —
“It’s a technique where the operating system loads program pages into memory only when they’re required, improving efficiency and enabling smooth multitasking.”
Frequently Asked Questions (FAQ) on What is Demand Paging
Q1. What is Demand Paging in an Operating System?
Answer:
Demand paging is a memory management technique where the operating system loads program pages into physical memory only when they are needed. It helps reduce memory usage and improves system performance by avoiding loading the entire program at once.
Q2. What is the main purpose of Demand Paging?
Answer:
The main purpose of demand paging is to optimize memory usage and reduce program startup time. It allows the operating system to execute large programs efficiently even with limited RAM.
Q3. What causes a Page Fault in Demand Paging?
Answer:
A page fault occurs when the CPU tries to access a page that is not currently in main memory. The operating system then retrieves that page from secondary storage and loads it into RAM.
Q4. What are the advantages of Demand Paging?
Answer:
Some key advantages include:
- Efficient use of memory
- Faster program start-up
- Better multiprogramming support
- Reduced I/O operations
Q5. What are the disadvantages of Demand Paging?
Answer:
The disadvantages are:
- Increased page fault overhead
- Risk of system thrashing if too many page faults occur
- More complex memory management logic
Q6. What is the difference between Demand Paging and Pre-Paging?
Answer:
In demand paging, pages are loaded only when required, whereas in pre-paging, the OS predicts and loads multiple pages in advance. Demand paging saves memory, while pre-paging aims to reduce future page faults.
Q7. Where is Demand Paging used in real life?
Answer:
Demand paging is widely used in modern operating systems like Windows, Linux, macOS, and QNX, allowing them to handle large applications smoothly without consuming excessive physical memory.
Q8. How does Demand Paging improve system performance?
Answer:
By loading only the necessary pages, demand paging reduces memory pressure and improves CPU utilization, enabling multiple applications to run efficiently at the same time.
Q9. What is a Valid/Invalid bit in Demand Paging?
Answer:
Each entry in a page table has a valid/invalid bit that tells whether a page is currently in memory (valid) or stored on disk (invalid). This helps the OS detect when a page fault occurs.
Q10. Can Demand Paging cause Thrashing
Answer:
Yes. If too many page faults happen in a short time, the system may enter a state called thrashing, where it spends most of its time swapping pages instead of executing processes.
Mr. Raj Kumar is a highly experienced Technical Content Engineer with 7 years of dedicated expertise in the intricate field of embedded systems. At Embedded Prep, Raj is at the forefront of creating and curating high-quality technical content designed to educate and empower aspiring and seasoned professionals in the embedded domain.
Throughout his career, Raj has honed a unique skill set that bridges the gap between deep technical understanding and effective communication. His work encompasses a wide range of educational materials, including in-depth tutorials, practical guides, course modules, and insightful articles focused on embedded hardware and software solutions. He possesses a strong grasp of embedded architectures, microcontrollers, real-time operating systems (RTOS), firmware development, and various communication protocols relevant to the embedded industry.
Raj is adept at collaborating closely with subject matter experts, engineers, and instructional designers to ensure the accuracy, completeness, and pedagogical effectiveness of the content. His meticulous attention to detail and commitment to clarity are instrumental in transforming complex embedded concepts into easily digestible and engaging learning experiences. At Embedded Prep, he plays a crucial role in building a robust knowledge base that helps learners master the complexities of embedded technologies.












