Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Session 4.6: Page replacement (FIFO, Optimal, LRU), thrashing and the working set

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.

Module 4 1.5 hours Theory CO4
← Before
4.5 Virtual memory
Demand paging
You are here
4.6 Page replacement (FIFO, Optimal, LRU), thrashing and the working set
Module 4 · Memory and File Management
Next →
4.7 File concepts
Files and access methods

Learning objectives

By the end of this session you should be able to:

  • Describe the page-replacement steps and the role of the dirty bit.
  • Count page faults for FIFO, Optimal and LRU on a reference string.
  • Demonstrate Belady's anomaly with FIFO.
  • Explain the cause and cycle of thrashing.
  • Compute working sets for a window Δ and use them to control multiprogramming.

Lecture notes

1. Basic page replacement

  1. Find the location of the desired page on disk.
  2. Find a free frame. If there is none, use a page-replacement algorithm to select a victim frame; if the victim is dirty (its modify bit is set), write it to disk first.
  3. Read the desired page into the freed frame; update the page tables.
  4. Restart the process.

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

2. FIFO

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.

Simulator — FIFOEdit the inputs, press Run, then Step or Play.
Belady's anomaly

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).

3. Optimal (OPT / MIN)

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.

Simulator — OptimalEdit the inputs, press Run, then Step or Play.

4. Least Recently Used (LRU)

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.

Simulator — LRUEdit the inputs, press Run, then Step or Play.
Counter implementation

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.

Stack implementation

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.

Approximations

Real hardware offers only a reference bit; the second-chance (clock) algorithm gives a page with its bit set another round before evicting it.

AlgorithmFaults (3 frames)NeedsBelady's anomaly?
FIFO15Load orderYes
Optimal9Future knowledgeNo
LRU12Time of last useNo

5. Thrashing

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.

thrashing degree of multiprogramming → CPU utilisation

6. Locality and the working-set model

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.

Working-set example, Δ = 10

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.

Choosing Δ

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.

VideoBytes

FIFO, Optimal and LRU

Video coming soon

FIFO, Optimal and LRU

Recorded by Dr. Krithika L.B. Slot m4-page-replacement

Thrashing and the working set

Video coming soon

Thrashing and the working set

Recorded by Dr. Krithika L.B. Slot m4-thrashing

Interactive lessons

Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.

Lab connection

Check your understanding

Answer each question in your own words first, then reveal the model answer.

Revealed 0
Reference string 1 2 3 1 4 5 with 3 frames: faults under FIFO and LRU?

FIFO: 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.

Why is the Optimal algorithm not used in practice?

It needs to know the future reference string.

What is Belady's anomaly?

For some algorithms (FIFO), increasing the number of frames can increase the number of page faults.

Why does adding processes make thrashing worse?

Each new process takes frames from others, raising every process's fault rate and the paging queue.

D = 60 frames of total working sets, m = 50 frames. What should the OS do?

D > m → suspend (swap out) one or more processes until D ≤ m.

Key takeaways

  • FIFO (15), OPT (9, benchmark), LRU (12) on the textbook string; FIFO suffers Belady's anomaly.
  • Thrashing: more time paging than executing; caused by too many processes for too few frames.
  • Working set (window Δ) approximates locality; keep Σ WSS ≤ available frames.

Where this leads

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 →