Chapter 9: Virtual Memory
Multiple Choice · Introduction
Question 1: Total Larger Than RAM
On a server with 16 GB of RAM, the virtual sizes of the running processes add up to 41 GB, and the system runs without trouble. Which basic idea makes this possible?
C: Virtual memory separates the logical address space from physical memory. The valid bit in the page table says which pages are in RAM; the others are brought in when they are accessed. Compression (zswap, zram) helps but does not enlarge RAM; processes also stay in memory at the same time, and each has its own page table.
Multiple Choice · Demand Paging
Question 2: Writing to Address 0
The line int *p = NULL; *p = 5; runs in a C program. The operating system treats page 0 of the address space as belonging to no region (VMA). What happens?
B: The MMU sees the invalid bit in the entry and raises a trap. The kernel first checks whether the reference lies in a valid region: page 0 is deliberately left empty, so this is not the “valid but not in memory” case; it is an invalid reference. A would be right only for a page in a valid region; the TLB is a translation cache, not a data store.
Output Question · Demand Paging
Question 3: Address Translation and Page Fault
The page size is 1 KB (1024 bytes). The process’s page table is shown at the side: page 1 is in a valid region but swapped out, and page 3 belongs to no region. The virtual addresses 300, 2100, 1500, 4500, 3200 are accessed in order. For each, what are the page, the offset, and the physical address (or what happens)?
Show answer
| virtual | page | offset | physical address / result |
|---|---|---|---|
| 300 | 0 | 300 | 5 × 1024 + 300 = 5,420 |
| 2,100 | 2 | 52 | 9 × 1024 + 52 = 9,268 |
| 1,500 | 1 | 476 | page fault (loaded from swap) |
| 4,500 | 4 | 404 | 7 × 1024 + 404 = 7,572 |
| 3,200 | 3 | 128 | SIGSEGV (no region) |
Page = ⌊address / 1024⌋, offset = address mod 1024 and does not change in translation; physical address = frame × 1024 + offset.
1500: page 1 is not in memory → major page fault; the page is read in and the access is repeated. 3200: page 3 is in no region → the process is terminated.
Output Question · Performance
Question 4: Effective Access Time
A memory access takes 100 ns, and the total service time of a page fault (trap + read from HDD + restart) is 8 ms.
- If one access in a thousand faults (p = 0.001), what is the effective access time? How many times slower is memory?
- For the slowdown not to exceed 10%, what must p be at most?
- If swap moves to an NVMe SSD and the fault service time drops to 50 µs, what is the effective access time in (a)?
Show answer
EAT = (1 − p) × ma + p × fault time; make the units equal: 8 ms = 8,000,000 ns.
(a) 0.999 × 100 + 0.001 × 8,000,000 = 99.9 + 8,000 = 8,099.9 ns (about 8.1 µs): memory becomes about 81 times slower.
(b) 110 > 100 + 7,999,900 p → p < 10 / 7,999,900 = 1.25 × 10⁻⁶: fewer than one fault in about 800,000 accesses.
(c) 99.9 + 0.001 × 50,000 = 149.9 ns: the slowdown drops to 50%. Fast storage shrinks the penalty, but memory is still hundreds of times faster; keeping the fault rate low is still the job of the replacement algorithm.
Multiple Choice · Copy-on-Write
Question 5: How Many Pages Are Copied?
After fork(), the parent and child share 300 pages by copy-on-write. First the child reads 10 pages and writes to pages A and B. Then the parent writes to pages A and C. How many pages are copied in total?
A: The child’s writes to A and B cause two protection faults and two copies. When the parent writes to A, the reference count of A’s original frame is now 1: no copy is needed, the page is just made writable. C is still shared (reference count 2) → the third copy. Reads never cause a copy (D is wrong); B assumes every write copies.
Multiple Choice · Memory-Mapped Files
Question 6: Writing with MAP_PRIVATE
A process maps the file data.bin with mmap(…, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0), writes p[0] = 'X', and calls munmap. At the same time another process has the file mapped with MAP_SHARED. Which statement is correct?
D: With a MAP_PRIVATE mapping, the first write causes a protection fault and the kernel makes a private copy of the page (copy-on-write). The change exists only in this process’s address space and vanishes at munmap. The behavior that reaches the file and everyone mapping it is MAP_SHARED (A and B describe that).
Step-by-Step Trace · Page Replacement
Question 7: Page Faults with LRU
With three empty frames and the reference string 4 2 5 2 3 4 2 5 4 3 4 1, how many page faults does LRU cause? Which page is evicted at each fault?
Three frames are empty; the reference string is 4 2 5 2 3 4 2 5 4 3 4 1. last[k] is the value of t at which the page in the frame was last used.
Reference 1, page 4: fault; it is loaded into the empty frame F0.
Reference 2, page 2: fault; it is loaded into the empty frame F1.
Reference 3, page 5: fault; it is loaded into the empty frame F2.
Reference 4, page 2: in memory → hit; only last[F1] = 3.
Reference 5, page 3: fault. Last uses (4→0, 2→3, 5→2); the smallest, 0, belongs to 4, which is evicted and 3 takes its place.
Reference 6, page 4: fault. Last uses (3→4, 2→3, 5→2); the smallest, 2, belongs to 5, which is evicted and 4 takes its place.
Reference 7, page 2: in memory → hit; only last[F1] = 6.
Reference 8, page 5: fault. Last uses (3→4, 2→6, 4→5); the smallest, 4, belongs to 3, which is evicted and 5 takes its place.
Reference 9, page 4: in memory → hit; only last[F2] = 8.
Reference 10, page 3: fault. Last uses (5→7, 2→6, 4→8); the smallest, 6, belongs to 2, which is evicted and 3 takes its place.
Reference 11, page 4: in memory → hit; only last[F2] = 10.
Reference 12, page 1: fault. Last uses (5→7, 3→9, 4→10); the smallest, 7, belongs to 5, which is evicted and 1 takes its place. Total: 8 faults.
for (t = 0; t < 12; t++) { p = ref[t]; k = find(p); /* -1 if absent */ if (k < 0) { /* page fault */ faults++; if (used < 3) k = used++; else k = oldest(last); /* LRU */ frame[k] = p; } last[k] = t; /* last use */ }
Output Question · Page Replacement
Question 8: Same String: FIFO and OPT
With the same string (4 2 5 2 3 4 2 5 4 3 4 1) and three empty frames, how many page faults do FIFO and OPT cause? Together with the LRU result, rank the three algorithms.
In OPT, if there is a tie (neither page is ever used again), the page in the lower-numbered frame is evicted.
Show answer
FIFO: 10 faults
| ref | 4 | 2 | 5 | 2 | 3 | 4 | 2 | 5 | 4 | 3 | 4 | 1 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| F0 | 4 | 4 | 4 | 4 | 3 | 3 | 3 | 5 | 5 | 5 | 5 | 1 |
| F1 | 2 | 2 | 2 | 2 | 4 | 4 | 4 | 4 | 3 | 3 | 3 | |
| F2 | 5 | 5 | 5 | 5 | 2 | 2 | 2 | 2 | 4 | 4 | ||
| fault | ● | ● | ● | ● | ● | ● | ● | ● | ● | ● |
OPT: 6 faults
| ref | 4 | 2 | 5 | 2 | 3 | 4 | 2 | 5 | 4 | 3 | 4 | 1 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| F0 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 4 | 1 |
| F1 | 2 | 2 | 2 | 2 | 2 | 2 | 5 | 5 | 5 | 5 | 5 | |
| F2 | 5 | 5 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | ||
| fault | ● | ● | ● | ● | ● | ● |
Red: loaded by a fault, green: hit. Ranking: OPT (6) < LRU (8) < FIFO (10). At the 6th reference FIFO evicts 2, which was used two steps earlier, only because it was loaded first, and the 7th reference faults (the same happens to 4 at the 10th reference). OPT is a lower bound.
Multiple Choice · Page Replacement
Question 9: More Frames, More Faults?
The reference string is 3 4 5 6 3 4 7 3 4 5 6 7. With 3 empty frames, FIFO causes 9 page faults on this string. If the number of frames is raised to 4, how many faults does FIFO cause?
C: This is the slides’ string 1 2 3 4 1 2 5 1 2 3 4 5 with renamed pages: Belady’s anomaly. With 4 frames, 3 and 4 hit, but when 7 arrives the oldest page, 3, is evicted; then 3, 4, 5, 6 and 7 each ask for a just-evicted page → 10 faults. LRU and OPT are stack algorithms: the set held with n frames is a subset of the set held with n + 1, so extra frames cannot add faults (LRU: 10 → 8).
Step-by-Step Trace · Page Replacement
Question 10: The Clock Algorithm
With three empty frames and the string 2 3 2 1 5 2 4 5 3 2 5 2, how does the clock (second-chance) algorithm work? The R bit of a loaded or accessed page becomes 1; the hand moves only on a fault.
Three frames in a circular list; the R of empty frames counts as 0, and the hand points to F0. String: 2 3 2 1 5 2 4 5 3 2 5 2.
Reference 1, page 2: fault. The hand stops at the empty F0 and 2 is loaded.
Reference 2, page 3: fault. The hand stops at the empty F1 and 3 is loaded.
Reference 3, page 2: in memory → hit; R ← 1, the hand does not move.
Reference 4, page 1: fault. The hand stops at the empty F2 and 1 is loaded.
Reference 5, page 5: fault. The hand clears R ← 0 for 2, 3, 1 (all with R = 1) and moves on; 2 in F0 (R = 0) is evicted and 5 takes its place; the hand advances by one.
Reference 6, page 2: fault. 3 in F1 (R = 0) is evicted and 2 takes its place; the hand advances by one.
Reference 7, page 4: fault. 1 in F2 (R = 0) is evicted and 4 takes its place; the hand advances by one.
Reference 8, page 5: in memory → hit; R ← 1, the hand does not move.
Reference 9, page 3: fault. The hand clears R ← 0 for 5, 2, 4 (all with R = 1) and moves on; 5 in F0 (R = 0) is evicted and 3 takes its place; the hand advances by one.
Reference 10, page 2: in memory → hit; R ← 1, the hand does not move.
Reference 11, page 5: fault. The hand clears R ← 0 for 2 and moves on; 4 in F2 (R = 0) is evicted and 5 takes its place; the hand advances by one.
Reference 12, page 2: in memory → hit; R ← 1, the hand does not move. Total: 8 faults (LRU 7, FIFO 9).
for (t = 0; t < 12; t++) { k = find(ref[t]); if (k >= 0) { R[k] = 1; continue; } faults++; while (R[hand] == 1) { /* second chance */ R[hand] = 0; hand = (hand + 1) % 3; } frame[hand] = ref[t]; /* victim's slot */ R[hand] = 1; hand = (hand + 1) % 3; }
Multiple Choice · Page Replacement
Question 11: Which Page Does NRU Evict?
In the NRU algorithm, the (R, M) bits of four pages are shown at the side. These values have accumulated since the clock interrupt last cleared the R bits. At a page fault, which page does NRU evict?
B: The classes are 0 = (0, 0), 1 = (0, 1), 2 = (1, 0), 3 = (1, 1). There is no (0, 0) page, so the lowest non-empty class is 1: L. Writing a dirty page is expensive, but evicting a clean page that was used recently is worse; in NRU the R bit is the primary criterion. Class 1 arises when a clock tick clears the R bit of a modified page.
Output Question · Page Replacement
Question 12: Aging Counters
The 8-bit aging counters of four pages (0–3) start at 0. The R bits seen at five clock ticks are shown below. At each tick the counter is shifted right by one bit and R is inserted at the left. What are the counters after the 5th tick, and which page does aging evict? Which page would NFU (counter += R) evict?
| tick | s0 | s1 | s2 | s3 |
|---|---|---|---|---|
| 0 | 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 0 | 1 |
| 2 | 1 | 0 | 1 | 0 |
| 3 | 0 | 1 | 1 | 0 |
| 4 | 0 | 1 | 0 | 1 |
Show answer
| page | counter | value | NFU |
|---|---|---|---|
| 0 | 00111000 | 56 | 3 |
| 1 | 11000000 | 192 | 2 |
| 2 | 01101000 | 104 | 3 |
| 3 | 10011000 | 152 | 3 |
The latest tick is the most significant bit: page 0’s R bits arrived as 1, 1, 1, 0, 0, so its two newest bits are 0 → 00111000 = 56, the smallest value. Aging evicts page 0.
NFU counts the whole history equally: page 1 was used only twice (2) → NFU would evict page 1, yet page 1 is the only page used in the last two ticks. That is why aging approximates LRU better than NFU does.
Output Question · Working Set
Question 13: Finding the Working Set
A process’s page references (starting at time 1) are shown below. The window is Δ = 5 references. What are W(7, Δ), W(10, Δ) and W(15, Δ)? How many frames does each need?
Show answer
W(t, Δ) = the distinct pages among the last Δ references ending at t.
- W(7): references 3–7 = 4 2 5 1 1 → {1, 2, 4, 5}, 4 frames.
- W(10): references 6–10 = 1 1 3 5 5 → {1, 3, 5}, 3 frames.
- W(15): references 11–15 = 6 7 6 6 2 → {2, 6, 7}, 3 frames.
W(10) and W(15) have the same size but no page in common: the process has moved to a new locality. If Δ is too small, the locality is missed; if too large, several localities overlap.
Output Question · Frame Allocation
Question 14: Equal and Proportional Allocation
Three processes have virtual sizes of 15, 45 and 140 pages; there are 64 frames to distribute. How many frames do equal allocation and proportional allocation (rounding down) give each process? How many frames are left over? Which is more balanced, and why?
Show answer
| P1 (15) | P2 (45) | P3 (140) | left over | |
|---|---|---|---|---|
| equal ⌊64 / 3⌋ | 21 | 21 | 21 | 1 |
| proportional | 4 | 14 | 44 | 2 |
Proportional: aᵢ = sᵢ / S × m, S = 200 → 15 / 200 × 64 = 4.8 → 4; 45 / 200 × 64 = 14.4 → 14; 140 / 200 × 64 = 44.8 → 44.
Equal allocation gives the 15-page P1 more frames than it needs and the 140-page P3 too few. Proportional allocation distributes by size; priority can be taken into account too. As a lower bound, each process should get as many frames as the number of pages a single instruction may touch at once.
Multiple Choice · Thrashing
Question 15: What Does the Monitor Show?
On a server’s monitor, CPU utilization is 7%, the swap disk is 100% busy, and the page-fault rate is very high. The long-term scheduler, seeing “CPU idle,” keeps admitting new jobs to the queue. What is the situation and the right response?
D: The sum of the processes’ working sets exceeds memory (Σ |WSᵢ| > m): an evicted page is needed again at once, processes spend their time waiting for pages, and the CPU sits idle. Adding new jobs shrinks every process’s frame share even further (C makes things worse). The remedy is to lower the degree of multiprogramming or add RAM; PFF makes this decision automatically.
Output Question · Design Issues
Question 16: Loop Order and Page Faults
The page size is 4 KB and an int is 4 bytes. The array starts at a page boundary and is initially not in memory; 32 frames are allocated for the array, and replacement is LRU (code and variables are in separate frames). How many page faults do fill1 and fill2 cause?
int a[64][1024]; /* 64 rows × 4 KB */ void fill1(void) { for (int i = 0; i < 64; i++) for (int j = 0; j < 1024; j++) a[i][j] = 0; } void fill2(void) { for (int j = 0; j < 1024; j++) for (int i = 0; i < 64; i++) a[i][j] = 0; }
Show answer
One row is 1024 × 4 B = 4 KB = exactly one page; the array is 64 pages and does not fit in 32 frames.
fill1: goes row by row; the first element of each row is a fault and the remaining 1023 elements hit → 64 faults.
fill2: the inner loop moves to a different row, that is, a different page, at every step. One column touches 64 pages, and LRU keeps only the last 32; the next column starts again at page 0, which has long been evicted. Every access faults: 64 × 1024 = 65,536 faults.
The same job, a 1024-fold difference. C stores arrays in row-major order; the inner loop should walk the last index.
Multiple Choice · Design Issues
Question 17: TLB Reach and Huge Pages
A database’s hot data is 48 MB. The CPU’s second-level TLB has 1024 entries. With 4 KB pages, what is the TLB reach, and what changes if the pages are converted to 2 MB huge pages?
A: TLB reach = number of entries × page size: 1024 × 4 KB = 4 MB, 1024 × 2 MB = 2 GB. 48 MB of hot data does not fit in the reach with 4 KB pages, so most accesses become TLB misses and page-table walks. Adding entries is expensive; enlarging the page is more effective. The price is internal fragmentation and the need to find a large contiguous region.
Classic Question · Design Issues
Question 18: Does Prepaging Pay Off?
For a process returning from suspension, s = 40 pages of its working set are prepaged, and α = 0.6 of them are actually used. Bringing in one page by an individual page fault takes 5 ms; reading all 40 pages in one go takes 30 ms.
- What is the I/O time without and with prepaging?
- Below what value of α does prepaging do harm?
- What is the cost that is not measured in time?
Show answer
(a) Pages used: α × s = 24. Without prepaging: 24 faults × 5 ms = 120 ms. With prepaging: one bulk read, 30 ms; a gain of 90 ms.
(b) Gain 40α × 5 ms, cost 30 ms: 200α > 30 → α > 0.15. The threshold is low in this example because the bulk read is cheap; as the cost of the bulk read approaches that of individual faults, the threshold rises.
(c) The (1 − α) × s = 16 unnecessary pages occupy frames and may cause other pages to be evicted. Readahead and madvise(MADV_WILLNEED) in Linux and the Prefetcher in Windows use the same idea.
Multiple Choice · Design Issues
Question 19: Page Pinning
A process gave a buffer for a disk read and blocked while the DMA transfer is in progress. If the buffer’s page is evicted meanwhile and the frame is given to another process, what happens and what is the remedy?
C: The device writes to a physical address; if the frame has changed hands, the data goes into the wrong process’s memory. The remedy is to pin the page for the duration of the I/O (pinning, mlock, pin_user_pages) or to do the I/O through a kernel buffer. An IOMMU lets devices use translated addresses as well.
Step-by-Step Trace · Kernel Memory
Question 20: The Buddy System
In a 256 KB region, a buddy allocator handles the following requests in order. How do the blocks and free lists change at each step? What is the total internal fragmentation?
The kernel has 256 KB of physically contiguous memory. Offsets are in KB; freeN is the list of offsets of free N KB blocks. Each request is rounded up to a power of two.
A = 40 KB → a 64 KB block is needed. Splits: 256 → 2 × 128, 128 → 2 × 64; A takes the 64 KB at offset 0 (internal fragmentation 24 KB).
B = 20 KB → a 32 KB block is needed. Splits: 64 → 2 × 32; B takes the 32 KB at offset 64 (internal fragmentation 12 KB).
C = 100 KB → 128 KB. A free 128 KB block is ready in the list (offset 128): no split is needed (internal fragmentation 28 KB).
D = 30 KB → 32 KB. A free 32 KB block is ready in the list (offset 96): no split is needed (2 KB). Total internal fragmentation 24 + 12 + 28 + 2 = 66 KB.
B is released. Its buddy (offset 64 ⊕ 32 = 96) is in use → no merge; the free 32 KB block goes into the list.
D is released. Its buddy (offset 96 ⊕ 32 = 64) is free → they merge: 64 KB. The new block’s buddy is in use, so merging stops.
A is released. Its buddy (offset 0 ⊕ 64 = 64) is free → they merge: 128 KB. The new block’s buddy is in use, so merging stops.
A = alloc(40); /* KB */
B = alloc(20);
C = alloc(100);
D = alloc(30);
release(B);
release(D);
release(A);Step-by-Step Trace · Kernel Memory
Question 21: Buddy: A 512 KiB Memory
A 512 KiB memory is managed with the buddy system. A = 45 K, B = 100 K, C = 20 K and D = 60 K are requested in that order; then B, C and A are released. Which block does each request get? What blocks make up the memory at the end?
512 KiB is a single free block. Blocks come only in power-of-two sizes; every request is rounded up.
A = 45 K → 64 K. 512 → 256 + 256 → 128 + 128 → 64 + 64 are split; A takes the first 64 (internal fragmentation 19 K).
B = 100 K → 128 K. The smallest suitable free block is 128–256; no split is needed (internal fragmentation 28 K).
C = 20 K → 32 K. The free 64–128 is split in two, and C takes 64–96 (12 K).
D = 60 K → 64 K. 256–512 is split: 128 + 128 → 64 + 64; D takes 256–320 (4 K). Total internal fragmentation 63 K.
B is released. Its buddy 0–128 is split (inside A and C) → no merge.
C is released. Its buddy 96–128 is free → 64–128 (64 K). Its buddy at 0–64 holds A → stop.
A is released: 0–64 + 64–128 → 0–128, + 128–256 → 0–256. Its buddy 256–512 is split (D) → stop.
request(n): b = smallest size 2^k >= n; while (no free block of size b) split a larger free block in two; hand out the block; release(block): while (buddy is free and the same size) merge with the buddy; /* address ^ size */
Multiple Choice · Real Systems
Question 22: When Free Pages Run Low
In Linux, a zone’s free-page count first falls below the low watermark, then below min. What happens, in that order?
C: Below low, kswapd wakes up and reclaims pages in the background (active / inactive lists, generations in MGLRU). Below min, the process that is allocating does direct reclaim itself: latency rises. The OOM killer steps in only if reclaim produces nothing at all; zswap does not turn swap off, it is a compressed cache in front of swap.