Virtual memory — exam-style exercises with page faults, FIFO, LRU and optimal traces and working sets (and the three faults I did not count in the mock exam)
In the IC2 mock exam I got an LRU trace with 3 frames. I did it carefully, counted 6 faults and left very pleased with myself. It was 9. The reason? I counted the first three accesses as hits because “the frames were empty and the page came in without evicting anyone”. Coming in without a victim does not stop it being a fault: the page was not in RAM and had to be brought from disk. These are the compulsory faults, and here you are going to count them every time.
This article is the practice for the virtual memory and page replacement theory. If you are coming from Topic 9, memory management, here we close the memory block: page faults, replacement algorithms, Belady’s anomaly, working set and thrashing, with questions in every exam format. Before you dive in, the virtual memory guided practice has the traces solved step by step.
Table of Contents
How to use this article
- Do the quiz without looking at the theory. Each question has its own “Check” button and an explanation: read it even when you get it right, because sometimes it explains why the other options fail.
- Traces, on paper. Draw the table (one column per reference, one row per frame) before filling in the boxes. If you get it wrong, compare your table with the trace in the explanation.
- Open questions, in writing. Write your answer and then compare it with the model answer and with the list “what the marker looks for”.
- Revise with the cheat sheet at the end before the exam.
Multiple-answer questions subtract like in the exam: each wrong tick takes marks away. If you are not sure about an option, better not to tick it.
To check a trace with another string or more frames, the page replacement simulator does it step by step with FIFO, LRU and the optimal algorithm.
Multiple-choice quiz
31 questions: single choice, multiple choice, true or false, gaps, matching, ordering and short answer. They go from the definition of virtual memory to PFF, through every replacement algorithm.
What does virtual memory mainly make possible?
- Running a process even if only part of its pages are in main memory
- Giving each process the whole RAM of the computer for itself
- Letting the programmer decide which parts go to RAM and which to disk
- Accessing the disk as fast as RAM
Solution
Answer: Running a process even if only part of its pages are in main memory
With virtual memory the logical space of a process can be larger than the RAM it has: secondary storage acts as an extension of main memory and the OS moves pages between the two without the program noticing.
About virtual memory, say whether each statement is true or false:
- A process can have some pages in RAM and others on disk at the same time.
- To start running, a process has to be fully loaded into RAM.
- Main memory is shared among the processes that are running.
- The OS always tries to give each process all the free RAM there is.
- It is the programmer who marks in the code which pages stay on disk.
Solution
- T — A process can have some pages in RAM and others on disk at the same time.
- F — To start running, a process has to be fully loaded into RAM.
- T — Main memory is shared among the processes that are running.
- F — The OS always tries to give each process all the free RAM there is.
- F — It is the programmer who marks in the code which pages stay on disk.
Virtual memory exists precisely so that the process does not have to be whole in RAM. The OS shares the frames among all processes (it does not give all the RAM to one) and decides itself which pages are in and which are out; the programmer does not deal with that.
With demand paging, when is a page read from disk?
- When the process references it and it is not in RAM (page fault)
- When the process is created, all at once
- When the TLB misses
- Every so often, in the order the CPU is going to ask for them
Solution
Answer: When the process references it and it is not in RAM (page fault)
On demand = a page is not loaded until it is needed. The first reference to each page causes a page fault and that is when the OS brings it from disk. In the gcc string in the article, pages 0 and 6 are never read.
Which bit of a page table entry says whether the page is in main memory?
Solution
Answer: Valid bit
The valid bit (v/i, or present bit): with v (1) the page is in a frame; with i (0) it is on disk and accessing it causes a page fault.
Put in order what happens when gcc asks for a page that is not in RAM and there is a free frame:
- The instruction that caused the fault is restarted
- The OS takes a free frame
- The OS checks the reference is valid and locates the page on disk
- The page is read from disk into that frame
- The MMU sees bit i in the page table and raises a trap
- The entry is updated: frame number and valid bit set to v
Solution
Correct order:
- The MMU sees bit i in the page table and raises a trap
- The OS checks the reference is valid and locates the page on disk
- The OS takes a free frame
- The page is read from disk into that frame
- The entry is updated: frame number and valid bit set to v
- The instruction that caused the fault is restarted
First the trap fires (the MMU cannot translate), the OS finds the page, puts it into a free frame, records the frame with v = 1 and runs the instruction again, which now does find the page. If there were no free frame, a victim would have to be chosen before reading.
Now there are no free frames. Put the steps to serve the fault in order:
- The new page is read into the frame that has become free
- If the victim has M = 1, it is written to disk
- The replacement algorithm chooses a victim page
- The new page’s entry is updated and the instruction is restarted
- The victim is marked as invalid in its page table
- The page fault happens
- The OS sees there is no free frame left
Solution
Correct order:
- The page fault happens
- The OS sees there is no free frame left
- The replacement algorithm chooses a victim page
- If the victim has M = 1, it is written to disk
- The victim is marked as invalid in its page table
- The new page is read into the frame that has become free
- The new page’s entry is updated and the instruction is restarted
The difference from the previous case is in the middle: choose a victim, save it if it is dirty and mark it as i (so its owner faults if it asks for it again). After that it goes on as before: read, update and restart.
The OS is going to evict a page from a frame. What tells it whether it must write it to disk first?
- The modified bit (M)
- The reference bit (R)
- The valid bit
- The protection bits
Solution
Answer: The modified bit (M)
The hardware sets M = 1 as soon as the page is written (a “dirty page”). A clean victim (M = 0) can simply be overwritten because the disk already has the same copy; a dirty one costs one more write.
What is the technique called in which the OS, when it has nothing to do, writes dirty pages to disk and sets M = 0?
Solution
Answer: Precleaning
Precleaning: it uses idle moments so that, when a victim is needed, it is more likely to be clean and the fault costs a single disk access.
Tick the ones that are page replacement algorithms:
- LRU
- Second chance
- NFU
- Optimal (OPT)
- Round Robin
- SRTF
- Best fit
- TLB
Solution
Answer: LRU · Second chance · NFU · Optimal (OPT)
Page replacement: FIFO, optimal, LRU and its approximations (second chance or clock, NRU, NFU, LFU, aging, WSClock). Be careful not to mix them up with CPU ones (FCFS, SJF, SRTF, RR) or with the fits of Topic 9.
Match each algorithm with the page it chooses as the victim:
| FIFO | |
| Optimal | |
| LRU | |
| LFU |
Definitions:
- The one with the highest use counter
- The one that has gone unused the longest
- The one used most recently
- The one with the lowest use counter
- The one that will be used again the latest
- The one that has been loaded in memory the longest
Solution
- FIFO → The one that has been loaded in memory the longest
- Optimal → The one that will be used again the latest
- LRU → The one that has gone unused the longest
- LFU → The one with the lowest use counter
FIFO looks at arrival, LRU at last use, the optimal at next use and LFU at how many times it has been used (it evicts the least used, not the most).
Why does no real OS implement the optimal algorithm?
- It needs to know which pages will be asked for in the future
- Because it causes more faults than LRU
- Because it suffers Belady’s anomaly
- Because it needs a modified bit per page
Solution
Answer: It needs to know which pages will be asked for in the future
The optimal evicts the page that will be referenced again the latest, and that requires knowing the future reference string. It is used as a benchmark to measure how close a real algorithm gets.
About Belady’s anomaly:
- It means that, with more frames, an algorithm can give more page faults.
- FIFO can suffer it.
- LRU can suffer it if the string is long enough.
- The optimal algorithm suffers it with 4 frames or more.
Solution
- T — It means that, with more frames, an algorithm can give more page faults.
- T — FIFO can suffer it.
- F — LRU can suffer it if the string is long enough.
- F — The optimal algorithm suffers it with 4 frames or more.
The anomaly appears in FIFO. LRU and the optimal are stack algorithms: what they have in memory with n frames is always contained in what they would have with n + 1, so one more frame never makes things worse. In the article, FIFO goes from 9 to 10 faults when going from 3 to 4 frames.
What do you need to implement LRU exactly?
- To record on every access when each page was used (counter or stack), and that is very expensive
- Just the modified bit
- A queue with the order of arrival
- To know future references
Solution
Answer: To record on every access when each page was used (counter or stack), and that is very expensive
Exact LRU needs, on every access, to store an instant (counter) or move the page to the top of a stack. Doing it in software would be extremely slow and in hardware it is expensive: that is why approximations with the R bit are used.
In the second chance algorithm, the hand reaches a page with R = 1. What happens?
- R is set to 0 and the hand moves to the next one
- It is chosen as the victim because it is the oldest
- It is written to disk and stays in memory
- M is set to 0
Solution
Answer: R is set to 0 and the hand moves to the next one
It is FIFO with one extra question: “have you been used since last time?”. If R = 1, it is forgiven (R becomes 0) and the hand moves on; the victim is the first page it finds with R = 0.
In NRU, which class is the victim preferably taken from?
- R = 0 and M = 0
- R = 0 and M = 1
- R = 1 and M = 0
- R = 1 and M = 1
Solution
Answer: R = 0 and M = 0
NRU groups pages into four classes by (R, M) and picks at random within the lowest non-empty class: 0 (0, 0), 1 (0, 1), 2 (1, 0), 3 (1, 1).
Match each LRU approximation with what it keeps per page:
| NFU | |
| Aging | |
| LFU | |
| Clock |
Definitions:
- The exact time of each access
- A register that shifts and receives the R bit from the left
- Only the R bit and a hand that goes round the frames
- A counter that adds the R bit at regular intervals
- A counter of the number of references
Solution
- NFU → A counter that adds the R bit at regular intervals
- Aging → A register that shifts and receives the R bit from the left
- LFU → A counter of the number of references
- Clock → Only the R bit and a hand that goes round the frames
NFU adds R to a counter every tick (it never forgets). Aging shifts the register to the right and puts R in from the left, so recent use weighs more. LFU counts references. The clock only needs R. Storing the exact time of each access would be exact LRU, not an approximation.
With aging and 4-bit registers, after a tick the pages are A = 0110, B = 1001 and C = 0011. Which is the victim?
- C
- B
- A
- Any of the three: they tie
Solution
Answer: C
The leftmost bit is the latest tick, so the register is read as a number: A = 6, B = 9, C = 3. The one with the lowest value is evicted: C.
What does WSClock need per page or overall?
- The reference bit
- The instant of last use (U)
- The time window Δ
- The number of times it has been written
- A reference counter
- The order of arrival of all the pages
Solution
Answer: The reference bit · The instant of last use (U) · The time window Δ
WSClock combines the clock (circular list and R bit) with the working set: for each page it stores when it was last used (U) and compares it with the window Δ.
What is the name for processes’ tendency to concentrate their accesses on a few areas of code and data for a while?
Solution
Answer: Locality
Locality (of reference). Thanks to it virtual memory works: at any moment a process only needs a few pages. Careful, it is not “segmentation”.
Fill in the gaps:
A process goes through different ____ while it runs. If the current one is entirely in RAM, it will hardly have any ____. The pages of its current locality form its ____.
Words:
- localities
- free frames
- working set
- page faults
- segments
- TLB hits
Solution
A process goes through different localities while it runs. If the current one is entirely in RAM, it will hardly have any page faults. The pages of its current locality form its working set.
The working set is the way to measure the current locality: the distinct pages referenced in the last window Δ.
The virtual clock is now 50, the window is Δ = 8 and a page was last used at U = 41. Is it in the working set?
- No: 50 − 41 = 9 and 9 > 8
- Yes: 41 < 50
- Yes: 50 − 41 = 9 ≤ 8
- It cannot be known without the M bit
Solution
Answer: No: 50 − 41 = 9 and 9 > 8
A page is in the WS if clock − U ≤ Δ. Here 50 − 41 = 9 > 8, so it has fallen outside the window.
Complete the definition of thrashing:
Thrashing appears when there are chained ____ because there are not enough ____ for all the processes; the system crawls because it spends its time ____.
Words:
- segmenting
- processes
- frames
- paging
- hits
- page faults
Solution
Thrashing appears when there are chained page faults because there are not enough frames for all the processes; the system crawls because it spends its time paging.
Each process evicts pages that another (or itself) needs right away; the CPU is left waiting for the disk and its use plummets.
If the working set sizes of all processes are added up (D) and the result is larger than the number of available frames, what should the OS do?
- Suspend some process and share out its frames
- Admit more processes to make use of the CPU
- Swap FIFO for LRU
- Nothing: it is normal
Solution
Answer: Suspend some process and share out its frames
With D > m thrashing is guaranteed. The way out is to lower the degree of multiprogramming (the medium-term scheduler suspends processes) until D fits.
With PFF (page fault frequency), a process’s fault rate falls below the lower limit. What is done?
- Frames are taken away from it
- It is given more frames
- The process is suspended
- Its replacement algorithm is changed
Solution
Answer: Frames are taken away from it
PFF sets a band: above the upper limit it is given frames; below the lower one, they are taken away. If there are no frames to give, some process is suspended.
Which features of a program usually increase page faults?
- Linked structures (lists, trees) spread across memory
- Interpreted languages with run-time type checking
- Very large functions
- Walking through an array in the order it is stored
- Using the stack for local variables
- Compiled languages with static types
Solution
Answer: Linked structures (lists, trees) spread across memory · Interpreted languages with run-time type checking · Very large functions
Whatever breaks locality makes faults soar: jumping between linked nodes, the extra code of an interpreter or huge functions that take many pages. Contiguous data and things walked in order help.
With the string 1, 2, 3, 4, 2, 1, 5 and 3 frames, what is the minimum number of faults, whatever algorithm you use?
- 5
- 3
- 7
- 4
Solution
Answer: 5
The compulsory faults (first reference to each page) cannot be avoided by anyone: here 1, 2, 3, 4 and 5 are 5 distinct pages. No algorithm, not even the optimal, goes below that.
In a trace with 3 frames holding 4, 8 and 6, page 9 arrives. The next references are 6, 4, 6, 8. What does the optimal algorithm evict?
- 8
- 6
- 4
- 9
Solution
Answer: 8
You look ahead: 6 comes back at position 1, 4 at position 2 and 8 at position 4. The victim is the one that comes back the latest: 8.
Same frames (4, 8, 6). The last uses were: 4 at t = 10, 8 at t = 12 and 6 at t = 7. Page 9 arrives. What does LRU evict?
- 6
- 8
- 4
- It depends on when each one came in
Solution
Answer: 6
LRU looks for the oldest last use: 7 < 10 < 12, so page 6 leaves.
About the cost of a page fault:
- Reading a page from disk is thousands of times slower than a RAM access.
- With a dirty victim, serving the fault costs two disk operations.
- A 1 % page fault rate is barely noticeable in the effective access time.
- For virtual memory to go unnoticed, the fault rate has to be tiny.
Solution
- T — Reading a page from disk is thousands of times slower than a RAM access.
- T — With a dirty victim, serving the fault costs two disk operations.
- F — A 1 % page fault rate is barely noticeable in the effective access time.
- T — For virtual memory to go unnoticed, the fault rate has to be tiny.
With 100 ns per access and 8 ms per fault, one fault every 1,000 accesses (0.1 %) already makes memory 81 times slower; 1 % would be a disaster. That is why p has to be in the order of one per million.
What is the area of the disk called where the OS keeps the pages that do not fit in RAM?
Solution
Answer: Swap area
The swap area: a partition or a file on disk that the OS uses as backing for virtual memory.
A process has 3 frames and its working set, in this phase, has 5 pages. What is most likely?
- That it faults a lot: its locality does not fit
- That it never faults: LRU solves it
- That Belady’s anomaly gives it fewer faults
- That the OS takes frames away from it
Solution
Answer: That it faults a lot: its locality does not fit
If |WS| > frames given, the process cannot keep its locality inside and faults non-stop: it is the seed of thrashing. The sensible thing would be to give it at least 5 frames (or suspend it).
In the matching ones, confusing LRU with LFU. LRU looks at when it was last used (recently); LFU, at how many times it has been used (frequently).
Traces and calculations
Before filling in the tables, the method that works best for me so I do not get lost in a trace:
- Write the string in a row and, below it, one row per frame.
- On each reference, ask yourself first: is it already in some frame? If it is, it is a hit: copy the previous column (and, in LRU, note that it has just been used).
- If it is not, it is a fault. If there is an empty frame, it goes there. If not, apply the algorithm’s rule: FIFO looks at who came in first, LRU at who has gone unused the longest and the optimal at who will take longest to come back (looking ahead in the string).
- Write the victim below the column. At the end, count the F’s: there must be at least as many as distinct pages.
In LRU, it helps to write next to each frame the instant of its last use and cross it out every time it is used again. In the optimal, underline in the string the next appearance of each page that is in memory.
Five exercises: one string with the three algorithms, a Belady’s anomaly, the effective access time with page faults and two on the working set.
onedrive asks for its pages in this order: 0, 3, 0, 1, 4, 0, 3, 2, 0, 4, 1, 3, and it has 3 frames (empty at the start). For each algorithm, work out the faults, the hits and which page leaves on the first replacement:
| Algorithm | Faults | Hits | First victim |
|---|---|---|---|
| FIFO | |||
| LRU | |||
| Optimal |
Solution
| Algorithm | Faults | Hits | First victim |
|---|---|---|---|
| FIFO | 10 | 2 | 0 |
| LRU | 9 | 3 | 3 |
| Optimal | 7 | 5 | 1 |
Trace (F = fault, · = hit, the victim in brackets):
FIFO: 0F 3F 0· 1F 4F(0) 0F(3) 3F(1) 2F(4) 0· 4F(0) 1F(3) 3F(2)
LRU: 0F 3F 0· 1F 4F(3) 0· 3F(1) 2F(4) 0· 4F(3) 1F(2) 3F(0)
Optimal: 0F 3F 0· 1F 4F(1) 0· 3· 2F(3) 0· 4· 1F(0) 3F(1)
The first replacement comes at t = 4 (page 4) with 0, 3 and 1 in memory. FIFO evicts 0 because it came in first, even though it has just been used at t = 2; LRU evicts 3, the one unused for the longest (t = 1); the optimal evicts 1, which does not come back until t = 10 (0 comes back at t = 5 and 3 at t = 6). All three have the 5 compulsory faults (pages 0, 1, 2, 3 and 4).
With FIFO and the string 2, 6, 1, 4, 2, 6, 0, 2, 6, 1, 4, 0, work out the faults with 3 and with 4 frames and say whether Belady’s anomaly appears (write yes or no):
| Frames | Faults | Hits |
|---|---|---|
| 3 | ||
| 4 | ||
| Belady’s anomaly? |
Solution
| Frames | Faults | Hits |
|---|---|---|
| 3 | 9 | 3 |
| 4 | 10 | 2 |
| Belady’s anomaly? | yes |
Fault (F) and hit (·) marks:
3 frames: F F F F F F F . . F F . → 9 faults
4 frames: F F F F . . F F F F F F → 10 faults
One more frame gives one more fault: that is Belady’s anomaly. With 4 frames, FIFO still has 2 and 6 inside at t = 6 and evicts 2 when 0 arrives, just before 2 is asked for again; with 3 frames that order of departures changes and it hits 2 and 6 at t = 7 and t = 8.
A memory access costs 100 ns. Serving a fault costs 5 ms if the victim is clean and 10 ms if it is dirty. Work out the effective access time (in ns) in each case and, in the last one, how many accesses there must be at least per fault so that the EAT stays at or below 110 ns (clean victims):
| Case | Result |
|---|---|
| p = 0.001, clean victims (ns) | |
| p = 0.001, half of them dirty (ns) | |
| p = 0.000001, clean victims (ns) | |
| Accesses per fault, at least |
Solution
| Case | Result |
|---|---|
| p = 0.001, clean victims (ns) | 5099.9 |
| p = 0.001, half of them dirty (ns) | 7599.9 |
| p = 0.000001, clean victims (ns) | 105 |
| Accesses per fault, at least | 499990 |
EAT = (1 − p) · 100 + p · service, all in ns (5 ms = 5,000,000 ns).
Case 1: 0.999 · 100 + 0.001 · 5,000,000 = 99.9 + 5,000 = 5,099.9 ns.
Case 2: if half of the victims are dirty, the average service is (5 + 10) / 2 = 7.5 ms: 99.9 + 7,500 = 7,599.9 ns.
Case 3: 0.999999 · 100 + 0.000001 · 5,000,000 = 99.9999 + 5 ≈ 105 ns.
Case 4: 100 + p · (5,000,000 − 100) ≤ 110 → p ≤ 10 / 4,999,900, that is, at most one fault every 499,990 accesses.
The virtual campus references 5, 2, 5, 7, 2, 5, 1, 3, 1, 3, 5, 1 (t = 0, 1, 2…). With a window Δ = 3, work out |WS| at each instant. The other processes need 6 frames between them and there are 8 free: is there thrashing at that instant? (yes/no)
| t | References in the window | |WS| | Thrashing? |
|---|---|---|---|
| 2 | 5, 2, 5 | ||
| 4 | 5, 7, 2 | ||
| 7 | 5, 1, 3 | ||
| 11 | 3, 5, 1 |
Solution
| t | References in the window | |WS| | Thrashing? |
|---|---|---|---|
| 2 | 5, 2, 5 | 2 | no |
| 4 | 5, 7, 2 | 3 | yes |
| 7 | 5, 1, 3 | 3 | yes |
| 11 | 3, 5, 1 | 3 | yes |
The working set is the distinct pages of the last 3 references:
WS(t = 2) = {2, 5} → |WS| = 2; D = 2 + 6 = 8 ≤ 8
WS(t = 4) = {2, 5, 7} → |WS| = 3; D = 3 + 6 = 9 > 8
WS(t = 7) = {1, 3, 5} → |WS| = 3; D = 3 + 6 = 9 > 8
WS(t = 11) = {1, 3, 5} → |WS| = 3; D = 3 + 6 = 9 > 8
At t = 2 page 5 repeats in the window (5, 2, 5) and only counts once: they fit. At the other instants D = 9 > 8: not everyone fits and there is thrashing.
A process’s virtual clock is 60 and the window is Δ = 10. For each page, work out clock − U and say whether it is in the working set (yes/no):
| Page | Last use (U) | clock − U | In the WS? |
|---|---|---|---|
| A | 55 | ||
| B | 49 | ||
| C | 50 | ||
| D | 38 | ||
| E | 60 |
Solution
| Page | Last use (U) | clock − U | In the WS? |
|---|---|---|---|
| A | 55 | 5 | yes |
| B | 49 | 11 | no |
| C | 50 | 10 | yes |
| D | 38 | 22 | no |
| E | 60 | 0 | yes |
A page is in the WS if clock − U ≤ Δ:
A: 60 − 55 = 5 ≤ 10 → inside
B: 60 − 49 = 11 > 10 → outside
C: 60 − 50 = 10 ≤ 10 → inside
D: 60 − 38 = 22 > 10 → outside
E: 60 − 60 = 0 ≤ 10 → inside
C is right on the edge (10 ≤ 10) and does count. B and D are the victim candidates in WSClock.
In the effective access time, mixing units. Convert milliseconds to nanoseconds (5 ms = 5,000,000 ns) before multiplying by p; otherwise the result comes out a thousand or a million times smaller.
In the working set tables, cross out the repeated pages in the window before counting. |WS| is the number of distinct pages.
Open questions
Four questions like the ones in the written part. Write your answer in the box and then open the model answer.
Explain what virtual memory is and what demand paging consists of. What does the system gain from them and what price does it pay?
Solution
Model answer:
Virtual memory is the technique that lets a process run even though it is not entirely in main memory: secondary storage (the swap area on disk) acts as an extension of RAM and the OS moves pages between the two transparently to the programmer. That way the logical space of a process can be larger than physical RAM and more processes fit in memory at once.
Demand paging is the usual way to achieve it: a page is not read from disk until the process references it. The page table has a valid bit; if it is i, the MMU raises a trap (page fault) and the OS brings the page in at that moment. Pages that are never used are never loaded.
What is gained: a higher degree of multiprogramming, faster start-up and programs bigger than RAM. The price: each page fault costs milliseconds (thousands of times more than a RAM access), so the fault rate has to be very low, and a replacement algorithm is needed when there are no free frames left.
What the marker looks for:
- Definition: the process does not have to be whole in RAM and the disk acts as an extension
- Transparent to the programmer; logical space larger than RAM
- Demand paging: lazy loading, valid bit and page fault
- Advantages: more processes in memory, fast start-up
- Cost: the page fault is very expensive and forces pages to be replaced
The key is to separate the two ideas: virtual memory is the goal (not having to fit whole) and demand paging is the mechanism (loading on first use).
Describe the steps the OS follows to serve a page fault when there are no free frames. Explain the role of the valid, reference and modified bits.
Solution
Model answer:
1) The process references a page whose entry has the valid bit set to i; the MMU cannot translate and raises a trap to the kernel. 2) The OS checks that the address is legal and locates the page in the swap area. 3) As there are no free frames, the replacement algorithm chooses a victim. 4) If the victim has the modified bit set to 1, it is written to disk; if it is 0, the copy on disk is still valid and nothing is written. 5) The victim is marked as invalid in its owner’s page table. 6) The new page is read into the freed frame. 7) Its entry is updated (frame number and valid bit set to v) and the instruction that faulted is restarted.
The valid bit says whether the page is in RAM (it is the one that causes the fault). The reference bit is set to 1 by the hardware on every access and is used by the LRU approximations (clock, NRU, aging) to know what has been used recently. The modified bit says whether the page was written: a dirty victim costs two disk operations and a clean one, one, which is why algorithms prefer clean ones and the OS does precleaning.
What the marker looks for:
- Trap because of the valid bit and check of the reference
- Choice of victim by the replacement algorithm
- Victim written only if M = 1
- Invalidate the victim, read the new page, update the table and restart the instruction
- Role of R in the LRU approximations and of M in the cost
Order matters: the victim is saved (if dirty) before the new page is read into its frame, and the instruction is restarted at the end, not continued.
Compare the FIFO, optimal and LRU algorithms. What is Belady’s anomaly and which one does it affect? Explain how the clock algorithm approximates LRU (you can use pseudocode).
Solution
Model answer:
FIFO evicts the page that has been in memory the longest: it is easy (a queue) but it ignores use, so it can evict a heavily used page. The optimal evicts the one that will be referenced again the latest: it gives the minimum possible number of faults, but it needs to know the future and only serves as a benchmark. LRU evicts the one that has gone unused the longest: it relies on locality (the recent past predicts the near future) and gets close to the optimal, but implementing it exactly requires recording something on every access (counters or a stack).
Belady’s anomaly is that giving a process more frames increases the number of faults. It happens to FIFO; LRU and the optimal are stack algorithms (what is there with n frames is contained in what would be there with n + 1) and do not suffer it.
The clock (second chance) is FIFO plus the reference bit. The frames form a circular list and a hand goes round them:
while true: if R[hand] == 1 then R[hand] = 0 and hand = next; otherwise victim = hand, hand = next and stop
A page used since the last pass escapes once; if it is still unused, it goes on the next round. That way it resembles LRU using a single bit per page.
What the marker looks for:
- Criterion of each algorithm (arrival, next use, last use)
- The optimal cannot be implemented; it serves as a benchmark
- Exact LRU is expensive: counters or a stack
- Belady’s anomaly: more frames, more faults; it affects FIFO, not LRU or the optimal
- Clock: circular list, R bit set to 0 and move on; victim with R = 0
A numerical example helps a lot: with gcc’s string from the article and 3 frames, FIFO gives 11 faults, LRU 9 and the optimal 7.
Define locality and working set. What is thrashing, why does it happen and how can the OS avoid it?
Solution
Model answer:
Locality is the tendency of a process to concentrate its accesses, for a while, on a few pages of code and data (a loop, a function, a data structure). The working set WS(t, Δ) is the set of distinct pages referenced in the last Δ references: a way of measuring the current locality. With a last-use field U, a page is in the WS if clock − U ≤ Δ.
Thrashing happens when processes do not have enough frames for their working set: each fault evicts a page that will be needed right away, faults chain together, everyone waits for the disk and CPU use collapses. It usually appears when the degree of multiprogramming rises too much: if the sum of the |WS| of all processes (D) exceeds the available frames, thrashing is certain.
To avoid it, the OS can use the working set model (give each process at least |WS| frames and suspend processes if D does not fit) or the page fault frequency (PFF): if a process’s fault rate goes above an upper limit it is given frames, if it drops below a lower limit some are taken away and, if there are no frames to give, some process is suspended.
What the marker looks for:
- Locality well defined
- Working set with the window Δ (and clock − U ≤ Δ)
- Thrashing: chained faults for lack of frames and an idle CPU
- Cause: high degree of multiprogramming, D > frames
- Solutions: WS model, PFF with its two limits, suspending processes
The key is the chain: locality → working set → if it does not fit, thrashing. With that, the solutions follow on their own.
The whole topic in one drawing
If you only have a minute before going into the exam, look at this:
The six cards follow the order of the topic: virtual memory causes page faults, faults force pages to be replaced, exact replacement (LRU) is approximated with the R bit, and everything works as long as the working sets fit; when they do not, thrashing appears.
Cheat sheet: virtual memory
The essentials in 30 seconds
- Virtual memory: the process does not have to be whole in RAM; the rest, in the swap area.
- Demand paging: a page is read the first time it is asked for (valid bit set to i → page fault).
- Page fault: trap → check → (victim, write it if M = 1) → read → v = 1 → restart the instruction.
- FIFO the oldest · optimal the one that comes back latest · LRU the one unused the longest.
- Belady: with FIFO, more frames can give more faults. LRU and the optimal do not suffer it.
- WS(t, Δ): distinct pages of the last Δ references; inside if clock − U ≤ Δ.
- Thrashing: Σ |WS| > frames → chained faults, idle CPU. Solution: WS, PFF or suspend processes.
I want… / How / Result
| I want… | How | Result |
|---|---|---|
| to know if a page is in RAM | valid bit of its entry | v = in RAM; i = on disk (fault) |
| to know if the victim must be written | M bit | M = 1 → 2 I/Os; M = 0 → 1 I/O |
| the effective access time | (1 − p) · access + p · service, all in ns | 100 ns, 8 ms, p = 0.001 → 8,099.9 ns |
| the minimum possible number of faults | number of distinct pages in the string | compulsory faults |
| FIFO’s victim | the one that came in first | leaves even if heavily used |
| LRU’s victim | the oldest last use | based on the recent past |
| the optimal’s victim | the furthest next appearance (or none) | fewer faults than anyone; not implementable |
| the clock’s victim | first with R = 0; the ones with R = 1 are set to 0 | second chance |
| NRU’s victim | lowest non-empty class: (R, M) = (0,0) < (0,1) < (1,0) < (1,1) | at random within the class |
| aging’s victim | register shifted right with R from the left | the one with the lowest value |
| to know if a page is in the WS | clock − U ≤ Δ | yes / no |
| to detect thrashing | D = Σ |WS| versus free frames | D > frames → thrashing |
| to adjust frames with PFF | fault rate versus the two limits | above: give frames; below: take them away |
Exercise templates
Trace (FIFO, 3 frames) — the first part of the onedrive string in the quiz:
| ref | 0 | 3 | 0 | 1 | 4 | 0 |
|---|---|---|---|---|---|---|
| frame A | 0 | 0 | 0 | 0 | 4 | 4 |
| frame B | 3 | 3 | 3 | 3 | 0 | |
| frame C | 1 | 1 | 1 | |||
| fault? | F | F | · | F | F | F |
| out | 0 | 3 |
Effective time: EAT = 0.999 · 100 + 0.001 · 5,000,000 = 99.9 + 5,000 = 5,099.9 ns.
Working set (Δ = 3): window 5, 2, 5 → WS = {2, 5} → |WS| = 2 (page 5 counts once).
Clock − U: clock = 60, Δ = 10, U = 50 → 60 − 50 = 10 ≤ 10 → inside (the edge counts).
Symptom / Likely cause
| Symptom | Likely cause |
|---|---|
| I get fewer faults than distinct pages | you have not counted the compulsory faults (empty frames) |
| My FIFO and my LRU give the same everywhere | in LRU you are not updating the last use on hits |
| The EAT comes out at a few nanoseconds with p = 0.001 | you did not convert ms to ns |
| With more frames I get more faults | if it is FIFO, it may be Belady’s anomaly (not a mistake) |
| My |WS| is larger than Δ | you are counting repeated pages: the WS is a set |
| In the optimal I hesitate between two victims | neither comes back: a tie, either is fine (say so in the exam) |
| Everything is slow with the CPU almost idle | thrashing: Σ |WS| does not fit in RAM |
Before handing in, ask yourself
- Have I counted every first reference as a fault, even if there were empty frames?
- Have I applied the rule of the algorithm the question asks for (arrival, last use or next use) and not another one?
- Are all units the same (ns or ms) before working out the EAT?
- In the working set, have I counted distinct pages and used ≤ (not <) with Δ?
With this you close Topic 5 of IC2, memory. The next one, and the last of the IC2 theory, is files: how they are organised, how they are accessed and how disk space is shared out. See you there!
Keep goingAll Operating systems articlesDid this article help you? Here is how you can support SergioLearns →
Ready for the exam?IC2 theory mock exam — first midterm, second midterm or the full exam, with your real mark and a review of every mistake (as if you were in the exam room)


