Demand paging in Operating Systems - Time & Space Complexity
When using demand paging, the system loads pages only when needed. We want to understand how the time to access memory changes as the number of pages grows.
How does the number of page accesses affect the total time spent loading pages?
Analyze the time complexity of the following simplified demand paging process.
for each memory_access in program:
if page not in memory:
load page from disk
access data in page
This code checks if a page is in memory for each access and loads it if missing, then accesses the data.
Look for repeated actions that affect time.
- Primary operation: Checking and loading pages for each memory access.
- How many times: Once per memory access in the program.
As the number of memory accesses increases, the system may need to load more pages, which takes extra time.
| Input Size (memory accesses) | Approx. Operations (page checks and loads) |
|---|---|
| 10 | Up to 10 page checks and some page loads |
| 100 | Up to 100 page checks and more page loads |
| 1000 | Up to 1000 page checks and many page loads |
Pattern observation: The time grows roughly in direct proportion to the number of memory accesses, since each access may cause a page load.
Time Complexity: O(n)
This means the time to handle memory accesses grows linearly with the number of accesses, because each access may trigger a page load.
[X] Wrong: "Demand paging always takes constant time per access because pages are loaded only once."
[OK] Correct: The first time a page is accessed, loading it from disk takes extra time, so the total time depends on how many unique pages are accessed, not just the number of accesses.
Understanding how demand paging affects time helps you explain how operating systems manage memory efficiently. This skill shows you can think about how system design impacts performance.
What if the program accesses pages in a repeating pattern that fits entirely in memory? How would the time complexity change?
Practice
Solution
Step 1: Understand demand paging concept
Demand paging means loading pages only when the program tries to use them, not before.Step 2: Compare options with concept
To load pages into memory only when they are needed matches this idea, while others describe different or incorrect behaviors.Final Answer:
To load pages into memory only when they are needed -> Option AQuick Check:
Demand paging = load pages on demand [OK]
- Thinking all pages load before execution
- Confusing demand paging with increasing memory size
- Assuming demand paging prevents all page faults
Solution
Step 1: Define page fault in demand paging
A page fault occurs when a program accesses a page that is not in physical memory.Step 2: Match definition with options
When a program tries to access a page not currently in memory correctly describes this event; others describe unrelated events.Final Answer:
When a program tries to access a page not currently in memory -> Option DQuick Check:
Page fault = access missing page [OK]
- Confusing page fault with program completion
- Thinking page fault happens during shutdown
- Believing page fault means all pages loaded
Solution
Step 1: Identify initial memory state and page accesses
No pages are in memory initially. Access order is 2, 4, 2, 5, 4.Step 2: Determine which accesses cause page faults
First access to page 2 causes a fault (not in memory). Then page 4 causes a fault. The second access to page 2 is in memory, no fault. Access to page 5 causes a fault. Last access to page 4 is in memory, no fault.Final Answer:
Pages 2, 4, and 5 cause page faults -> Option BQuick Check:
First access to new pages = page fault [OK]
- Assuming repeated pages cause faults
- Thinking pages preload automatically
- Ignoring initial empty memory state
Solution
Step 1: Understand causes of frequent page faults
Frequent page faults happen when a program accesses many pages not currently in memory.Step 2: Evaluate options for likely cause
The program accesses many pages not yet loaded matches this cause. CPU speed or disk disconnection do not directly cause page faults. A small program fitting in memory would cause fewer faults.Final Answer:
The program accesses many pages not yet loaded -> Option AQuick Check:
Many new page accesses = frequent faults [OK]
- Blaming CPU speed for page faults
- Confusing hardware issues with paging faults
- Assuming small programs cause many faults
Solution
Step 1: Understand least recently used (LRU) page replacement
LRU removes the page not used for the longest time, keeping frequently used pages in memory.Step 2: Identify benefit of LRU in demand paging
This reduces page faults by avoiding removal of pages likely to be used soon.Final Answer:
It reduces the number of page faults by keeping frequently used pages in memory -> Option CQuick Check:
LRU keeps frequent pages, reduces faults [OK]
- Thinking LRU increases memory size
- Believing LRU prevents all faults
- Confusing LRU with preloading all pages
Ready to prove it?
Pass the test and this pattern moves unknown
