FIFO, Optimal and LRU
FIFO, Optimal and LRU
Recorded by Dr. Krithika L.B. Slot m4-page-replacement
Choosing a victim page when memory is full, Belady's anomaly, why too little memory makes a system thrash, and how the working-set model prevents it.
By the end of this session you should be able to:
The modify (dirty) bit halves the cost when the victim is clean — no write-back needed. Algorithms are evaluated by running them on a reference string (the sequence of page numbers referenced) and counting page faults. All examples below use the textbook string with 3 frames:
7 0 1 2 0 3 0 4 2 3 0 3 0 3 2 1 2 0 1 7 0 1
Replace the page that has been in memory the longest (a FIFO queue of pages). Easy, but the oldest page may be in heavy use. Result: 15 faults.
You would expect more frames → fewer faults. Not always for FIFO! Reference string 1 2 3 4 1 2 5 1 2 3 4 5:
3 frames → 9 faults; 4 frames → 10 faults. Try it in the simulator by changing Frames. Optimal and LRU never show this anomaly (they are stack algorithms).
Replace the page that will not be used for the longest time in the future. It gives the lowest possible fault rate — 9 faults here — but it needs future knowledge, so it is used only as a benchmark for other algorithms.
Use the recent past as an approximation of the near future: replace the page that has not been used for the longest time. Result: 12 faults — between FIFO and OPT. LRU is considered good and is widely approximated.
Each page-table entry gets a time-of-use field, copied from a logical clock on every reference. Replace the page with the smallest time — needs a search.
Keep a doubly-linked stack of page numbers; on reference, move the page to the top. The bottom is the LRU page — no search, but every reference updates pointers.
Real hardware offers only a reference bit; the second-chance (clock) algorithm gives a page with its bit set another round before evicting it.
| Algorithm | Faults (3 frames) | Needs | Belady's anomaly? |
|---|---|---|---|
| FIFO | 15 | Load order | Yes |
| Optimal | 9 | Future knowledge | No |
| LRU | 12 | Time of last use | No |
If a process does not have enough frames for the pages it is actively using, it page-faults, replaces a page it needs right away, faults again, and so on. A process that spends more time paging than executing is thrashing.
CPU utilisation is low because processes are waiting for the paging disk.
The long-term scheduler sees low utilisation and admits more processes (raises the degree of multiprogramming).
New processes take frames from running ones → even more faults → the paging device's queue grows.
CPU utilisation drops further, the OS adds yet more processes… throughput collapses. The cure is to give processes enough frames, or reduce the degree of multiprogramming.
A locality is a set of pages that are actively used together (a function's code, its local variables, some globals). A process moves from locality to locality. Thrashing happens when the sum of the localities exceeds total memory.
The working-set model uses a window of the most recent Δ references. The working set WS(t, Δ) is the set of pages referenced in that window; its size WSSi approximates process i's locality.
References … 2 6 1 5 7 7 7 7 5 1 | 6 2 3 4 1 2 3 4 4 4 3 4 3 4 4 4 …
At the end of the first 10 references: WS = {1, 2, 5, 6, 7} (size 5). Later, in a window of 3s and 4s: WS = {3, 4} (size 2).
Total demand D = Σ WSSi. If D > m (available frames) → thrashing is coming: the OS suspends a process and gives its frames to the others. If D is well below m, another process can be started.
Too small → does not cover the whole locality. Too large → overlaps several localities. Δ = ∞ → the entire program. An alternative is page-fault frequency (PFF): if a process's fault rate is above an upper bound, give it a frame; below a lower bound, take one away.
FIFO, Optimal and LRU
Recorded by Dr. Krithika L.B. Slot m4-page-replacement
Thrashing and the working set
Recorded by Dr. Krithika L.B. Slot m4-thrashing
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 0FIFO: 1,2,3 (3 faults), 1 hit, 4 replaces 1 (4), 5 replaces 2 (5) → 5. LRU: 1,2,3 (3), 1 hit, 4 replaces 2 (4), 5 replaces 3 (5) → 5.
It needs to know the future reference string.
For some algorithms (FIFO), increasing the number of frames can increase the number of page faults.
Each new process takes frames from others, raising every process's fault rate and the paging queue.
D > m → suspend (swap out) one or more processes until D ≤ m.
Virtual memory treats the disk as an extension of RAM. The other big use of the disk is to store files that outlive any process. Next: the file-system interface.
Continue to 4.7 — File concepts and access methods →