0
0
Operating Systemsknowledge~6 mins

Demand paging in Operating Systems - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine trying to use a huge book but only carrying the pages you need right now instead of the whole book. Computers face a similar problem when running programs that are too big to fit all at once in memory. Demand paging solves this by loading only the parts of a program that are needed at the moment, saving memory and speeding up the system.
Explanation
Basic idea of demand paging
Demand paging means the computer loads pages of a program into memory only when the program tries to use them. Instead of loading the entire program at once, it waits until a page is needed. This helps save memory and reduces the time to start a program.
Demand paging loads program pages only when they are actually needed.
Page fault and handling
When a program tries to access a page not currently in memory, a page fault occurs. The operating system then pauses the program, finds the needed page on disk, loads it into memory, and resumes the program. This process happens automatically and is usually very fast.
A page fault triggers loading the missing page from disk into memory.
Advantages of demand paging
Demand paging saves memory by not loading unused pages. It also allows programs larger than physical memory to run by loading only parts at a time. This makes the system more efficient and responsive, especially when running many programs.
Demand paging improves memory use and allows large programs to run efficiently.
Potential drawbacks
If many page faults happen quickly, the system spends too much time loading pages, causing delays called thrashing. Also, accessing pages on disk is slower than memory, so the first access to a page is slower than later ones.
Too many page faults can slow down the system due to frequent disk access.
Real World Analogy

Imagine a chef who only takes ingredients from the pantry when needed for a recipe step, instead of bringing all ingredients to the counter at once. This saves space and keeps the kitchen tidy. If the chef suddenly needs an ingredient not on the counter, they pause cooking to fetch it.

Basic idea of demand paging → Chef taking only needed ingredients when cooking
Page fault and handling → Chef pausing to fetch an ingredient not on the counter
Advantages of demand paging → Saving kitchen space and keeping it organized by not crowding all ingredients
Potential drawbacks → Delays when chef frequently stops to fetch ingredients
Diagram
Diagram
┌───────────────┐       ┌───────────────┐
│ Program tries │       │ Page in memory?│
│ to access a   │──────▶│   Yes ───────▶│
│ page         │       │   Continue    │
└───────────────┘       └───────────────┘
          │ No
          ▼
┌───────────────┐
│ Page fault    │
│ occurs       │
└───────────────┘
          │
          ▼
┌───────────────┐       ┌───────────────┐
│ OS loads page │──────▶│ Resume program│
│ from disk    │       │ with new page │
└───────────────┘       └───────────────┘
Flowchart showing how demand paging checks for a page in memory, handles page faults by loading from disk, and resumes the program.
Key Facts
Demand pagingA memory management technique that loads pages only when they are needed by a program.
Page faultAn event triggered when a program accesses a page not currently in memory.
ThrashingA condition where excessive page faults cause the system to spend most time loading pages, slowing performance.
PageA fixed-size block of memory used as the basic unit of data for paging.
Common Confusions
Demand paging means the entire program is loaded into memory at once.
Demand paging means the entire program is loaded into memory at once. Demand paging loads only the pages that the program needs at the moment, not the whole program.
Page faults are errors that crash the program.
Page faults are errors that crash the program. Page faults are normal events that the operating system handles automatically to load missing pages.
Summary
Demand paging loads program parts only when needed, saving memory and startup time.
Page faults occur when a needed page is missing and trigger loading it from disk.
Too many page faults can slow the system, a problem called thrashing.