Thrashing and the working set
What happens when a process does not have enough frames: thrashing and the collapse of CPU utilization, locality, the working-set model and its use to control the degree of multiprogramming, the page-fault-frequency scheme, and the allocation of frames among processes.
Why it exists
If a process does not have enough frames for the pages it is actively using, it faults, replaces a page it needs again almost at once, faults again, and spends nearly all its time waiting for the disk. This is thrashing. It is not just slow: it can make the whole system collapse, because the operating system sees low CPU utilization and responds by admitting more processes, which take frames from the others and make things worse. The cure is to give each process enough frames for its working set, and to reduce the number of processes when there are not enough frames for all. GATE asks for working-set sizes from a reference string, for the condition that avoids thrashing, and for the allocation of frames.
Prerequisites
You need the fault rate, page replacement and the idea that the disk is much slower than memory from page replacement and virtual memory.
Precise definitions
Thrashing. A process (or the system) is thrashing when it spends more time paging than executing: a very high page-fault rate with little useful work. The CPU is mostly idle, waiting for the disk.
The vicious circle. A scheduler that watches CPU utilization sees it fall when processes wait for page faults, concludes that the degree of multiprogramming is too low, and admits more processes. Each new process takes frames from the others (with global replacement), so they fault more, utilization falls further, and more processes are added. CPU utilization rises with the degree of multiprogramming up to a point and then collapses.
Locality. A locality is the set of pages a process uses together over a period (a procedure, its local data, the array it is scanning). A program moves from one locality to another as it runs, and localities may overlap. A process thrashes if its current locality does not fit in its allocated frames. Demand paging works because locality makes the fault rate low when the locality fits.
Working-set model. Fix a window (a number of the most recent page references). The working set of a process at time is the set of distinct pages in the most recent references. The working-set size is .
- If is too small it does not cover the locality; if too large it spans several localities; if infinite it includes the whole program.
- always (there are only references in the window), and the number of distinct pages of the program.
- The working set changes by at most one page per reference (the new page may enter, the one leaving the window may leave).
Using the working set. If is the working-set size of process , the total demand for frames is
If (the number of available frames), thrashing will occur: the operating system suspends (swaps out) one or more processes until , which lowers the degree of multiprogramming. If it can start another process. This keeps as many processes as possible running without thrashing. The difficulty is keeping track of the working sets; it is approximated with a periodic timer interrupt and the reference bit (pages whose bit was set in the last interval are in the working set).
Page-fault-frequency (PFF) scheme. Measure each process's fault rate directly. Set an upper bound and a lower bound: if a process's rate exceeds the upper bound, it needs more frames: give it one (or, if none are free, suspend a process); if the rate falls below the lower bound, it has too many: take a frame away. This keeps the rates in a band. It is simpler than tracking working sets.
Allocation of frames. Each process needs a minimum number of frames (set by the instruction set architecture: an instruction that can refer to several pages needs them all resident). The rest is divided:
- Equal allocation: frames to each of processes.
- Proportional allocation: process of size gets frames, with (rounded, and at least the minimum). Sometimes based on priority instead of, or as well as, size.
Global versus local replacement. With global replacement a process may take a frame from another; with local replacement it replaces only among its own. Local replacement gives predictable behaviour for each process and confines thrashing to the thrashing process, but may leave frames unused; global replacement gives better throughput but lets one process disturb the others.
How it works
The animation computes the working set with along the reference string 1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6. At each reference the window is the last four references and the working set is the set of distinct pages in it. Watch the working-set size: it grows to 4 as new pages arrive (1, 2, 3, 4 give sizes 1, 2, 3, 4) and falls to 3 when a page repeats inside the window (the second 2 at reference 5). A process whose allocation is below its current working-set size thrashes; one with 4 frames is safe throughout this string.
The working set with window 4: start
Algorithm simulated, not typedWorking-set model, delta = 4.
reference string (green: hit, amber: page fault)
Step 0
The WORKING SET at time t is the set of distinct pages referenced in the most recent 4 references (the window of size delta = 4). Its size is the working-set size WSS. A process should be given at least WSS frames; if the frames available are fewer it will fault constantly (thrash).
Worked examples
EX 1Working-set sizes and the demand for frames
EasyA program prints the working-set size of the string for , tests whether the demand of four processes fits in 40 frames, and computes the allocations for three processes.
#include <stdio.h>
static int wss(const int *refs, int t, int delta) {
int count = 0;
for (int i = (t - delta + 1 < 0 ? 0 : t - delta + 1); i <= t; i++) {
int seen = 0;
for (int j = (t - delta + 1 < 0 ? 0 : t - delta + 1); j < i; j++) if (refs[j] == refs[i]) seen = 1;
if (!seen) count++;
}
return count;
}
int main(void) {
int refs[20] = {1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6};
printf("working-set size with window 4 after each reference:");
for (int t = 0; t < 20; t++) printf(" %d", wss(refs, t, 4));
printf("\n");
/* thrashing check: total demand D = sum of working-set sizes against the m frames available */
int w[4] = {6, 10, 14, 20}, m = 40, d = 0;
for (int i = 0; i < 4; i++) d += w[i];
printf("demand D = %d pages, frames m = %d: %s\n", d, m, d > m ? "D > m, the system thrashes: suspend a process" : "D <= m, no thrashing");
int suspend = 3; /* suspend the process with the largest working set */
printf("after suspending the process with WSS %d: D = %d <= %d\n", w[suspend], d - w[suspend], m);
/* proportional allocation of m = 60 frames to processes of 15, 30 and 105 pages */
int size[3] = {15, 30, 105}, total = 150, frames = 60;
printf("proportional allocation of %d frames:", frames);
for (int i = 0; i < 3; i++) printf(" %d", size[i] * frames / total);
printf("\nequal allocation:");
for (int i = 0; i < 3; i++) printf(" %d", frames / 3);
printf("\n");
return 0;
}working-set size with window 4 after each reference: 1 2 3 4 3 4 4 4 4 4 3 3 4 4 3 4 4 3 3 4
demand D = 50 pages, frames m = 40: D > m, the system thrashes: suspend a process
after suspending the process with WSS 20: D = 30 <= 40
proportional allocation of 60 frames: 6 12 42
equal allocation: 20 20 20Reading it. The working set never exceeds the window (4). For four processes with working sets 6, 10, 14 and 20 the demand is ; only 40 frames exist, so the system will thrash. Suspending the largest process brings down to 30, which fits (and leaves 10 frames: another small process could be started). With proportional allocation the 60 frames go 6, 12, 42 to processes of 15, 30 and 105 pages ( frame per page); equal allocation would give 20 each, starving the large process and wasting frames on the small one.
EX 2Computing a working set by hand
EasyFor the references 2 6 1 5 7 7 7 7 5 1 6 2 with : at the 9th reference the last five references are 7 7 7 7 5, so and . At the 5th reference the window is 2 6 1 5 7, so (the maximum possible for ). The working set shrinks while a page is used repeatedly: the program has settled into a small locality (the page 7 loop). When a program changes locality the working set grows briefly as the new pages arrive and the old ones age out of the window.
EX 3Enough frames for the working set?
MediumWith the string above, a process with 4 frames and LRU faults far less than with 3 (10 faults for 4 frames against 15 for 3): its working-set size reaches 4 during the run, so with fewer than 4 frames it cannot hold its locality and keeps faulting. This is the practical meaning of "give each process at least its working set". The effect is not a cliff for every string (here the 3-frame fault rate is high but the string is short), but with real programs, whose localities are larger and more stable, the fault rate drops sharply once the allocation reaches the working-set size and rises sharply below it.
EX 4Page-fault frequency in action
MediumSuppose the upper bound is 10 faults per second and the lower bound 2. Process A shows 15 faults per second: above the upper bound, so it is given an extra frame. Process B shows 1 fault per second: below the lower bound, so one of its frames is taken away. If no free frames exist and several processes are above the upper bound, the operating system suspends a process (like the working-set method) so that the others can get more. The PFF method needs no window and no reference-bit tracking, only a count of faults per process.
EX 5Which statement is true?
GATE level"Thrashing occurs when the degree of multiprogramming is too high." True, and more precisely when the total demand for frames exceeds the frames available. "Adding a faster CPU cures thrashing." False: the CPU is idle waiting for the disk, and a faster CPU just waits faster. "Local replacement prevents thrashing of other processes." It confines the effect to the process that thrashes, which keeps the others running normally. "Increasing the page size always reduces thrashing." No: it can reduce the number of faults for sequential access but also reduces the number of pages that fit in memory. "If , suspend a process." True; this is the working-set policy. "The working-set window should be as large as possible." No: a window that spans several localities overestimates the demand.
Cheat box
Cheat box
- Thrashing: more time paging than computing; CPU utilization collapses as multiprogramming increases beyond a point.
- Cause: a process's locality does not fit in its frames; the OS reacts by admitting more processes.
- Working set : distinct pages in the last references; ; .
- : suspend a process. : may start another.
- PFF: raise the allocation above the upper bound, lower it below the lower bound.
- Equal: . Proportional: . Local replacement confines thrashing; global shares frames.
Traps and GATE patterns
Connections
Builds on
Across subjects
No cross-subject links yet.
Thrashing is what happens when page replacement has too few frames to work with; its cost comes from the page-fault service time of virtual memory. The decision to suspend a process is made by the medium-term scheduler (CPU scheduling), moving the process out of the Ready and Running states (process states).
PYQs
2 previous year questions from your book. I re-solved each one myself (0 by running or simulating, 2 by reasoning only); the book's key is shown next to my answer. All PYQs
A virtual memory system uses First In First Out (FIFO) page replacement policy and allocates a fixed number of frames to a process. Consider the following statements: P: Increasing the number of page frames allocated to a process sometimes increases the page fault rate. Q: Some programs do not exhibit locality of reference. Which one of the following is TRUE?
- (a)Both P and Q are true, and Q is the reason for P.
- (b)Both P and Q are true, but Q is not the reason for P.
- (c)P is false, but Q is true.
- (d)Both P and Q are false.
Increasing the RAM of a computer typically improves performance because:
- (a)Virtual memory increases
- (b)Larger RAMs are faster
- (c)Fewer page faults occur
- (d)Fewer segmentation faults occur OPERATING SYSTEMS
Self-check
Question 1
References 3 1 3 3 2 4 4 4 1 3 with . What is at the 7th reference?
Show answerHide
The last four references up to the 7th are 3 2 4 4 (positions 4 to 7): , so .
Question 2
Three processes have working-set sizes 8, 12 and 15 and 30 frames are available. Will the system thrash? What if a fourth process with is added?
Show answerHide
: it will thrash already; suspend a process (the one with 15 leaves ). The fourth process makes : worse.
Question 3
Proportional allocation of 100 frames to processes of 10 and 90 pages. How many frames each?
Show answerHide
and . (Equal allocation would give 50 each, too many for the first, too few for the second.)
Question 4
Why does a faster CPU not help a thrashing system?
Show answerHide
The processes are waiting for the disk, not for the CPU; the CPU is already idle most of the time.
Revision card
One-screen revision
- Thrashing: process lacks frames for its locality; faults dominate; admitting more processes makes it worse.
- : distinct pages in the last references; ; means suspend.
- PFF: rate above upper bound, add a frame; below lower bound, remove one.
- Equal ; proportional . Local replacement confines thrashing.
References
- ·Silberschatz, Galvin, Gagne, Operating System Concepts, Chapter 10 Virtual Memory (allocation of frames, thrashing, working-set model, page-fault frequency)
- ·Tanenbaum and Bos, Modern Operating Systems, Chapter 3 Memory Management (design issues for paging systems: the working set model, local versus global allocation)
- ·Stallings, Operating Systems: Internals and Design Principles, Chapter 8 Virtual Memory (resident set management)
Verification
The working-set sizes in the animation (window 4 on the page's 20-reference string) are recomputed by brute force in scripts/enum-checks/os_vm.py and re-derived window by window in tests/os-vm-traces.test.ts, which also checks that the size never exceeds the window and changes by at most one page per reference; the C program on this page (npm run verify:c) prints the same sizes, the demand-against-frames test and the equal and proportional allocations. The claim that LRU with at least as many frames as the largest working set never does worse than with one frame fewer is checked on the page string. The qualitative statements (CPU utilization collapses as the degree of multiprogramming rises beyond a point; the page-fault-frequency scheme keeps the rate between two bounds) are textbook explanations and are not simulated here. Window sizes, thresholds and frame counts are illustrative. Textbook chapter titles are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand. No defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)