Operating Systems
Virtual Memory
Virtual memory is a reliable numerical topic: simulate page-replacement algorithms on a reference string, spot Belady’s anomaly, and compute effective access time with page faults.
GATE CSE virtual memory — demand paging, page faults, effective access time, FIFO, LRU, Optimal and Clock page replacement with worked traces, Belady’s anomaly, thrashing, working set and frame allocation, with samples, flashcards and drills.
Learning outcomes
- Explain demand paging and the steps of servicing a page fault
- Count page faults for FIFO, LRU and Optimal replacement on a given reference string and frame count
- Recognise Belady’s anomaly and know which algorithms are stack algorithms
- Compute effective access time with a page-fault rate and explain thrashing and the working-set model
Demand paging and page faults
- VIRTUAL MEMORY lets a process run although only part of it is in physical memory, so the logical address space can be larger than physical memory. It is implemented by DEMAND PAGING: a page is brought into memory only when it is referenced (lazy swapper = pager).
- Each page-table entry has a VALID-INVALID bit: valid means the page is in memory and legal; invalid means either the page is not in memory or is illegal. Referencing an invalid page causes a PAGE FAULT (a trap to the OS).
- Servicing a page fault: (1) trap to the OS; (2) check the internal table to see whether the reference was legal (illegal → terminate); (3) find a free frame (or choose a victim by a replacement algorithm); (4) schedule a disk read of the page into the frame; (5) update the page table (valid bit, frame); (6) restart the faulting instruction. The process is blocked during the disk read.
- Pure demand paging starts a process with no pages in memory; the number of initial faults depends on locality. Each page-fault service involves a disk read (millisecond range) and, if the victim page is DIRTY (modified), also a write-back, doubling the I/O. The DIRTY (modify) bit avoids writing clean pages back.
- EFFECTIVE ACCESS TIME with page-fault probability p: EAT = (1 - p) × t_mem + p × t_fault, where t_fault is the page-fault service time (with possible write-back). Because t_fault is about a million times t_mem, p must be tiny: for t_mem = 200 ns and t_fault = 8 ms, a 10% slowdown needs p < 2.5 × 10^-6.
- COPY-ON-WRITE (COW): after fork() parent and child share pages marked read-only; a page is copied only when one of them writes, making fork fast. Memory-mapped files use the same mechanism.
- Locality of reference (temporal and spatial) is why demand paging works: programs spend most of the time in a small set of pages.
- TLB and demand paging: a TLB miss followed by a valid page-table entry costs one extra memory access; a page fault costs a disk access plus the restart.
- Dirty victims: if a fraction d of victim pages is dirty and a clean fault takes t_c while a dirty one takes t_d (write-back + read), the average service time is (1 - d) × t_c + d × t_d. With t_c = 8 ms, t_d = 20 ms and d = 0.7: 0.3 × 8 + 0.7 × 20 = 16.4 ms.
- Page faults are a TRAP (synchronous exception): the faulting instruction is restarted after the page is loaded, so the hardware must be able to undo partial effects (for example block-move instructions that overlap pages).
- Free-frame pool and page buffering: the OS keeps a pool of free frames and writes dirty pages in the background, so a fault does not need to wait for a write-back. Pre-paging brings in pages that will probably be needed to cut the initial burst of faults.
- Valid vs resident: valid-invalid bit = 1 means the page is legal AND in memory; 0 means either illegal or legal-but-on-disk. The OS distinguishes them using another table (process control block / disk map).
Formulas to remember
-
EAT = (1 - p) × t_mem + p × t_pagefault -
Page faults ≥ number of distinct pages referenced (compulsory misses)
Worked example
Problem. Memory access time is 200 ns and average page-fault service time is 8 ms. If one access in 1000 faults, what is the effective access time?
Solution. p = 0.001. EAT = 0.999 × 200 + 0.001 × 8,000,000 = 199.8 + 8000 = 8199.8 ns ≈ 8.2 microseconds, i.e. the program slows down by a factor of about 41.
Page-replacement algorithms with worked traces
- When no frame is free, a VICTIM page is chosen. Goal: lowest page-fault rate. The algorithms are evaluated on a REFERENCE STRING for a given number of frames; initial loads of empty frames count as faults.
- FIFO replaces the oldest page in memory (a queue). Simple but it can evict a heavily used page. Optimal (OPT, Belady’s MIN) replaces the page that will not be used for the longest time in the future: the minimum possible faults, but unimplementable (used as a benchmark).
- LRU replaces the page not used for the longest time in the past (OPT looking backwards). Implemented with counters or a stack; true LRU needs hardware support, so approximations are used: reference bit, second chance / CLOCK, additional-reference-bits, enhanced second chance (reference, dirty).
- CLOCK (second chance): frames form a circular list with a hand and a reference bit. The hand advances; if the bit is 1 it is cleared and the page gets a second chance; if 0 the page is replaced. If all bits are 1, it degenerates to FIFO. LFU/MFU use usage counts.
- BELADY’S ANOMALY: for some algorithms (notably FIFO) giving a process MORE frames can produce MORE page faults. Stack algorithms (LRU, OPT) satisfy the inclusion property (the set of pages in n frames is a subset of those in n + 1 frames) and never show the anomaly.
- Reference string 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 with 3 frames: FIFO = 15 faults, LRU = 12 faults, OPT = 9 faults.
- Belady’s string 1,2,3,4,1,2,5,1,2,3,4,5: FIFO with 3 frames = 9 faults, FIFO with 4 frames = 10 faults (anomaly). LRU gives 10 faults with 3 frames and 8 with 4 frames (no anomaly).
- A cyclic reference string over k+1 pages with k frames makes LRU and FIFO fault on EVERY reference (the page evicted is exactly the page needed next); this is why large loops defeat LRU.
- Trace for 2,3,2,1,5,2,4,5,3,2,5,2 with 3 frames. FIFO faults on 2,3,1,5,2,4,3,5,2 = 9. LRU faults on 2,3,1,5,4,3,2 = 7. OPT faults on 2,3,1,5,4,2 = 6. The ordering OPT ≤ LRU ≤ FIFO is typical but not guaranteed: on Belady’s string with 3 frames LRU (10) is worse than FIFO (9).
- LRU with 3 frames, step by step: after 2,3,2,1 the frames hold {2,3,1}; reference 5 evicts 3 (least recently used) → {2,1,5}; 2 hits; 4 evicts 1 → {2,5,4}; 5 hits; 3 evicts 2 → {5,4,3}; 2 evicts 4 → {5,3,2}; 5 and 2 hit.
- OPT rule: for every page in memory find the NEXT use in the remaining string and evict the one used farthest away (or never again). When two pages are never used again, either may be evicted, but the fault count is the same.
- Second-chance (CLOCK) example: frames hold pages with reference bits; on a fault the hand skips pages with bit 1 (clearing them) and replaces the first page with bit 0. If every bit is 1 the hand makes a full turn, clears all bits and then behaves like FIFO.
- Counting recipe: for each reference write H or F; initial loads count as faults; the answer to “how many page faults” includes the first reference to every page. A string with d distinct pages has at least d faults for any algorithm.
| Algorithm | Victim | Belady’s anomaly? | Faults on 20-ref classic (3 frames) |
|---|---|---|---|
| FIFO | oldest loaded | Yes | 15 |
| LRU | least recently used | No | 12 |
| Optimal | used farthest in the future | No | 9 |
| Clock | first page with reference bit 0 | Can behave like FIFO | — |
Worked example
Problem. Reference string 1,2,3,4,1,2,5,1,2,3,4,5 — count the page faults using FIFO with 3 frames and with 4 frames.
Solution. FIFO, 3 frames: faults on 1,2,3,4,1,2,5, then 3,4 (1 and 2 hit, last 5 hits) → 9 faults. FIFO, 4 frames: 1,2,3,4 fault (4); 1,2 hit; 5 (evict 1), 1 (evict 2), 2 (evict 3), 3 (evict 4), 4 (evict 5), 5 (evict 1) fault → 4 + 6 = 10 faults. More frames, more faults: Belady’s anomaly.
Page-fault counting patterns and Belady’s table
- Belady’s string 1,2,3,4,1,2,5,1,2,3,4,5: with 3 frames FIFO = 9, LRU = 10, OPT = 7; with 4 frames FIFO = 10, LRU = 8, OPT = 6. Only FIFO gets worse with more frames, which is the anomaly; LRU and OPT improve (stack algorithms).
- OPT with 3 frames on that string: 1,2,3 fault; 4 evicts 3 (next use farthest); 1 and 2 hit; 5 evicts 4; 1 and 2 hit; 3 evicts 1 or 2 (not used again); 4 evicts 3 or 2; 5 hits. Faults at 1,2,3,4,5,3,4 = 7.
- Array traversal: an int array[128][128] stored row-major with one 128-word row per page and only one frame for data. Looping row by row touches each page once: 128 faults. Looping column by column touches a different page on every access: 128 × 128 = 16,384 faults (if fewer than 128 frames are available).
- Sequential scan of N distinct pages with any number of frames ≥ 1 and no reuse: exactly N faults. Repeated cyclic scan of k + 1 pages with k frames under FIFO or LRU: a fault on every single reference.
- Minimum frames per process is fixed by the instruction set: an instruction that may reference up to 3 memory operands, each possibly indirect through several levels, can need many frames at once; fewer frames than that make the instruction impossible to complete.
- GLOBAL replacement can steal frames from other processes (a high-priority process may take frames from a low-priority one); LOCAL replacement keeps each process in its allocation. Global gives better overall throughput; local gives predictable per-process fault rates.
| Frames | FIFO | LRU | OPT |
|---|---|---|---|
| 3 | 9 | 10 | 7 |
| 4 | 10 | 8 | 6 |
Worked example
Problem. Memory access takes 100 ns. A page fault takes 8 ms if the victim is clean and 20 ms if dirty; 70% of victims are dirty and the page-fault rate is 10^-4. Find the effective access time.
Solution. Average fault service time = 0.3 × 8 ms + 0.7 × 20 ms = 2.4 + 14 = 16.4 ms = 16,400,000 ns. EAT = (1 - 10^-4) × 100 + 10^-4 × 16,400,000 = 99.99 + 1640 = 1739.99 ns ≈ 1.74 microseconds, about 17.4 times the raw memory time.
Thrashing, working set and frame allocation
- THRASHING: a process (or system) spends more time paging than executing. It happens when the processes do not have enough frames for their active pages; page faults rise, CPU utilization drops, and the scheduler (wrongly) admits more processes, making things worse.
- LOCALITY MODEL: a locality is a set of pages used together (a function, its data). Thrashing occurs when the sum of the localities exceeds the physical memory.
- WORKING-SET model: the working set WS(t, Δ) is the set of distinct pages referenced in the most recent Δ references. Let WSS_i be its size for process i; total demand D = sum(WSS_i). If D > number of frames, thrashing occurs and a process should be suspended. Choice of Δ: too small misses the locality, too large overlaps several localities.
- PAGE-FAULT FREQUENCY (PFF) control: set upper and lower bounds on the fault rate; above the upper bound allocate more frames, below the lower bound take frames away, and suspend a process if no frames are free.
- FRAME ALLOCATION: each process needs a minimum number of frames set by the instruction set architecture (the largest number of pages one instruction can touch). Equal allocation divides frames equally; PROPORTIONAL allocation gives process i frames a_i = (s_i / S) × m for size s_i, total size S and m frames; priority-based allocation favours important processes.
- GLOBAL replacement picks a victim from all frames (better throughput, but a process’s fault rate depends on others); LOCAL replacement picks only from the process’s own frames (more predictable).
- Page size trade-off: small pages → less internal fragmentation and better locality but larger page tables and more I/O operations; large pages → fewer faults per byte transferred and smaller tables but more fragmentation. The overhead is minimised near page size p = sqrt(2 × s × e) for process size s and PTE size e.
- TLB reach = (number of TLB entries) × (page size); larger pages or multiple page sizes increase reach.
- Proportional allocation example: m = 62 frames, process sizes 10 and 127 pages, S = 137. a1 = 10/137 × 62 ≈ 4.5 → 4 frames, a2 = 127/137 × 62 ≈ 57.5 → 57 frames (round down, keep at least the minimum).
- Working-set numerical: with Δ = 4 and string 2,6,1,5,7,7,7,7,5,1 the working set at the last reference is {7,5,1} (size 3). If three processes have WSS 30, 40 and 50 and only 100 frames exist, D = 120 > 100 and one process must be suspended; suspending the process with WSS 50 leaves D = 70 ≤ 100.
- Why utilization drops: the paging device queue grows, the CPU waits for it, the OS sees low CPU utilization and admits more processes, which steal frames and fault more. The cure is to REDUCE the degree of multiprogramming (swap out), not to increase it.
- Other remedies: local replacement (a thrashing process cannot steal from others), working-set prepaging, faster paging device, and more physical memory. Large pages reduce faults for sequential access but increase internal fragmentation.
Formulas to remember
-
D = sum(WSS_i); thrashing if D > m (total frames) -
proportional allocation: a_i = (s_i / S) × m -
optimal page size ≈ sqrt(2 × s × e) -
TLB reach = entries × page size
Worked example
Problem. The reference string 2,6,1,5,7,7,7,7,5,1 is observed and Δ = 4. What is the working set at the end of the string?
Solution. The last four references are 7, 7, 5, 1, so WS = {7, 5, 1} and the working-set size is 3.
Exam tips
- Always show the frame contents after every reference; mark hits, count faults including the first loads of empty frames.
- LRU on a trace: look BACKWARDS for the least recently used page; OPT: look FORWARDS for the page used farthest in the future.
- Belady’s anomaly is for FIFO (not LRU or OPT); use 1,2,3,4,1,2,5,1,2,3,4,5 as the memory aid (9 then 10).
- EAT = (1 - p) × t_mem + p × t_fault; convert units (ms to ns) before substituting.
- Thrashing: low CPU utilization with high paging; the cure is a lower degree of multiprogramming, working-set or PFF control.
10 GATE-style MCQs for Virtual Memory with full explanations. Attempt each question before revealing the answer.
The reference string 1,2,3,4,1,2,5,1,2,3,4,5 is run with FIFO replacement and 3 frames. The number of page faults is:
Show answer & explanation
Answer: B
Faults occur on 1,2,3,4,1,2,5,3,4 (the references to 1, 2 after 5 hit and the final 5 hits) = 9.
The same string 1,2,3,4,1,2,5,1,2,3,4,5 with FIFO and 4 frames gives:
Show answer & explanation
Answer: C
FIFO with 4 frames gives 10 faults, more than the 9 with 3 frames: Belady’s anomaly.
The string 1,2,3,4,1,2,5,1,2,3,4,5 with LRU and 3 frames gives how many page faults?
Show answer & explanation
Answer: C
Faults at 1,2,3,4,1,2,5,3,4,5 (the hits are the 1 and 2 right after 5) = 10 faults.
For the reference string 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 and 3 frames, the number of page faults under the Optimal algorithm is:
Show answer & explanation
Answer: A
The standard result is OPT = 9, LRU = 12 and FIFO = 15 faults.
Memory access takes 100 ns, page-fault service takes 10 ms and the page-fault rate is 10^-6. The effective access time is approximately:
Show answer & explanation
Answer: B
EAT = (1 - 10^-6) × 100 + 10^-6 × 10^7 ≈ 100 + 10 = 110 ns.
Which of the following page-replacement algorithms does NOT suffer from Belady’s anomaly?
Show answer & explanation
Answer: B
LRU (and OPT) are stack algorithms with the inclusion property and cannot show the anomaly.
A system shows low CPU utilization and very high paging activity. The most appropriate remedy is to:
Show answer & explanation
Answer: B
This is thrashing; suspending some processes gives the others enough frames for their working sets.
A process has the reference string 2,6,1,5,7,7,7,7,5,1 and the working-set window is 4. The size of the working set at the end is:
Show answer & explanation
Answer: B
The last four references are 7,7,5,1; distinct pages are {7,5,1}, so the size is 3.
The second-chance (clock) algorithm approximates which algorithm using a reference bit?
Show answer & explanation
Answer: B
The reference bit records recent use, so a recently used page gets a second chance — an approximation of LRU built on a FIFO circular queue.
A process with 4 frames references pages cyclically 1,2,3,4,5,1,2,3,4,5 under LRU. The number of page faults is:
Show answer & explanation
Answer: D
With 5 pages in a loop and 4 frames, LRU evicts exactly the page needed next, so every reference faults: 10 faults.
Extra page-specific practice beyond the GATE samples. Attempt all 8, then reveal answers to check your reasoning.
Reference string 2,3,2,1,5,2,4,5,3,2,5,2 with FIFO and 3 frames. The number of page faults is:
Show answer & explanation
Answer: C) 9
Faults at 2,3,1,5,2,4,3,5,2 = 9 (hits at the second 2, the second 5 and the 2 after 3).
The same string 2,3,2,1,5,2,4,5,3,2,5,2 with LRU and 3 frames gives:
Show answer & explanation
Answer: C) 7 faults
Faults occur at 2,3,1,5,4,3,2 = 7.
Memory access time is 200 ns and page-fault service time is 8 ms. If the page-fault rate is 1/1000, the effective access time is approximately:
Show answer & explanation
Answer: B) 8.2 µs
EAT = 0.999 × 200 + 0.001 × 8,000,000 = 199.8 + 8000 ≈ 8200 ns = 8.2 µs.
A process references 5 distinct pages in a long string. The minimum possible number of page faults with any replacement algorithm and enough frames is:
Show answer & explanation
Answer: C) 5
Each distinct page must be brought in at least once (compulsory misses).
A process has size s = 1 MB and each page-table entry is e = 8 bytes. The page size that minimises total overhead (page table + internal fragmentation) is about:
Show answer & explanation
Answer: C) 4 KB
p = sqrt(2 × s × e) = sqrt(2 × 2^20 × 8) = sqrt(2^24) = 2^12 = 4096 bytes.
Which of the following is NOT a method to control thrashing?
Show answer & explanation
Answer: D) increasing the page size to 1 byte
A 1-byte page makes no sense; the three other methods all limit the processes competing for frames.
In the clock page-replacement algorithm, the hand finds a page whose reference bit is 1. The algorithm:
Show answer & explanation
Answer: B) clears the bit and moves to the next page
A set reference bit gives the page a second chance: the bit is cleared and the hand advances.
The property that a page set for n frames is always a subset of the set for n + 1 frames (inclusion) holds for:
Show answer & explanation
Answer: B) LRU
LRU and OPT are stack algorithms with the inclusion property, so more frames never gives more faults.
Topic FAQs
What is Belady’s anomaly?
Belady’s anomaly is the surprising increase in page faults when a process is given more frames. It occurs with FIFO (the classic string 1,2,3,4,1,2,5,1,2,3,4,5 gives 9 faults with 3 frames and 10 with 4), but never with stack algorithms such as LRU and OPT.
How do I count page faults quickly?
Write the frame contents in a table after each reference. A reference is a hit only if the page is already in a frame. The first references to an empty frame are faults. For LRU look at past use, for OPT at future use, for FIFO at load order.
What causes thrashing?
Too many processes with too few frames each: processes keep faulting, the CPU waits for the disk, and the OS may admit even more processes. Reduce multiprogramming or use working-set / PFF allocation.
Why is page-fault service time so important in effective access time?
A fault costs milliseconds while a normal access costs nanoseconds, so even a tiny fault rate dominates the EAT. That is why exam questions ask for the maximum tolerable fault rate.
Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.