Sub topic 14
Thrashing and the Working-Set Model
Prerequisites
This chapter assumes you know the following:
- Demand paging and page faults, including how a fault is serviced.
- The effective access time (EAT) formula with page faults.
- FIFO, LRU and Optimal page replacement.
- Global versus local replacement and how frames are allocated to processes.
What thrashing is
A process thrashes when it spends more time servicing page faults than executing instructions. The same happens to a whole system when the combined demand of all its processes exceeds the physical frames available.
A thrashing process has too few frames for the pages it is actively using. Each fault evicts a page that is needed again almost at once, so that page faults again. The CPU sits idle while the paging disk is saturated.
The cost is huge because of the gap in access times. Take main memory at 200 ns and a fault service time of 8 ms:
EAT = (1 − p) × 200 ns + p × 8,000,000 ns
To keep the slowdown under 10%, EAT must be at most 220 ns:
220 = 200 + p × (8,000,000 − 200), so p ≈ 2.5 × 10⁻⁶
That is fewer than one fault per 400,000 memory accesses. A thrashing process faults on a large fraction of its accesses, so its speed drops by orders of magnitude.
Why the system spirals
Early operating systems made thrashing worse by reacting to a symptom. The scheduler saw low CPU utilization and concluded that too few processes were running. It admitted more processes, which took frames away from the existing ones. Those processes faulted more, so utilization fell further.
Plot CPU utilization against the degree of multiprogramming (the number of processes in memory). Utilization rises at first, peaks, then collapses once thrashing starts. The right response past the peak is to reduce the degree of multiprogramming, not raise it.
Thrashing is much worse with global replacement. A faulting process steals frames from others, and those processes then fault and steal frames in turn. With local replacement, a thrashing process cannot take frames from others. It still thrashes itself, and its long disk queue slows everyone else's faults.
Locality of reference
Demand paging works because programs exhibit locality: over a short interval they touch only a small set of pages. Two kinds are commonly distinguished:
- Temporal locality: a page used recently is likely to be used again soon, as with a loop variable.
- Spatial locality: pages near a recently used page are likely to be used soon, as with sequential array access.
A program moves from one locality to another, for example from one function and its data to the next. Localities may overlap.
Thrashing occurs when the allocated frames are fewer than the size of the current locality. The process never keeps its whole locality resident, so it keeps faulting. Giving it enough frames for its locality stops the faulting until the next locality change.
The working-set model
Peter Denning's working-set model turns locality into a measurable quantity.
The working-set window Δ is a fixed number of the most recent page references. The working set WS(t, Δ) of a process at time t is the set of distinct pages referenced in the last Δ references. Its size is the working-set size (WSS).
Three properties matter:
- If Δ is too small, the window misses part of the locality, and the process will fault a lot.
- If Δ is too large, the window spans several localities, and the WSS overestimates the real need.
- If Δ is infinite, the working set is every page the process ever touched.
Let WSSᵢ be the working-set size of process i. The total frame demand is:
D = Σ WSSᵢ
Let m be the number of available frames. The OS policy is:
- If D ≤ m, memory is sufficient, so the OS may admit another process.
- If D > m, thrashing will occur, so the OS suspends a process. Its pages are swapped out and its frames are redistributed.
Because the policy refuses to run a process unless its entire working set fits, it prevents thrashing while keeping the degree of multiprogramming as high as memory allows.
Computing a working set
The function below follows the definition directly. The window ends at index t and includes the reference at t.
def working_set(refs, t, delta):
start = max(0, t - delta + 1)
return set(refs[start:t + 1])
refs = [1, 2, 1, 3, 2, 1, 4, 4, 4, 4, 5, 5, 5, 6]
delta = 4
for t in (3, 5, 9, 13):
ws = working_set(refs, t, delta)
print(f"t={t} WS={sorted(ws)} size={len(ws)}")The output is:
t=3 WS=[1, 2, 3] size=3
t=5 WS=[1, 2, 3] size=3
t=9 WS=[4] size=1
t=13 WS=[5, 6] size=2The first locality is {1, 2, 3}. By t = 9 the program has moved to page 4 alone, so the WSS falls to 1. The working set grows and shrinks as the process changes localities, which is why the OS must keep recomputing it.
A worked demand check
Suppose five processes have working-set sizes 10, 20, 15, 30 and 25, and the system has 90 frames.
- D = 10 + 20 + 15 + 30 + 25 = 100.
- D = 100 > m = 90, so the system will thrash.
- Suspending the process with WSS 10 gives D = 90. Now D ≤ m, so every remaining process can keep its whole working set.
Suspending the process with WSS 30 would also work, since D = 70. It would leave 20 frames unused, which is worse for utilization. GATE questions usually ask whether thrashing occurs, or how many processes must be suspended. For that second question, remove the smallest set of processes that brings D down to m, taking the largest first.
Tracking the working set in practice
True working-set tracking would update the set on every memory reference, which is far too costly. Real systems approximate it using a reference bit and a timer interrupt:
- Choose Δ as, say, 10,000 references, and a timer that fires every 5,000.
- Keep two history bits per page in memory, in addition to the hardware reference bit.
- On each timer interrupt, shift the reference bit into the history bits and clear the reference bit.
- On a fault, a page is considered in the working set if any of the history bits or the current reference bit is 1.
This is imprecise, since it cannot tell where in the last 5,000 references a use occurred. Adding more history bits and more frequent interrupts improves accuracy, at the cost of more overhead.
Page-fault frequency control
The working-set model is accurate but costly. Page-fault frequency (PFF) control is a simpler, more direct approach. It watches each process's fault rate and sets an upper and a lower bound:
- If the rate rises above the upper bound, the process needs more frames, so the OS gives it one. If no free frame exists, the OS suspends a process and reuses its frames.
- If the rate falls below the lower bound, the process has more frames than it needs, so the OS takes one away.
PFF is a feedback loop on the symptom itself, with no need to compute working sets. It still keeps each process's allocation near what its locality requires.
Other remedies
| Remedy | Effect |
|---|---|
| Reduce the degree of multiprogramming | Gives each remaining process more frames. This is the standard cure. |
| Add physical memory | Raises m, so D ≤ m holds for more processes. |
| Use local replacement | Stops one process stealing frames from another, though the thrashing process still suffers. |
| Use working-set or PFF allocation | Matches each process's frames to its locality. |
| Improve program locality | Better data layout and loop order shrink the WSS. |
Choosing a better replacement algorithm helps only a little. If the frames are fewer than the locality, even Optimal faults heavily.
Common errors
- Raising multiprogramming on low CPU utilization. Low utilization is ambiguous. Check the paging rate first, because the cause may be thrashing, not too few processes.
- Treating a high fault count as proof of thrashing. A cold start faults heavily as it loads its first pages, and that is normal. Thrashing means sustained faulting after the locality should be resident.
- Confusing the working set with all pages the process uses. It is only the pages in the last Δ references.
- Thinking local replacement prevents thrashing. It contains the damage to one process. It does not remove the cause.
- Mixing up Δ and WSS. Δ is the window length, which the OS chooses. WSS is the number of distinct pages found in that window, and it varies over time.
- Forgetting that WSS cannot exceed Δ. A window of Δ references holds at most Δ distinct pages.
Exercises
Exercise
A process has the reference string 3, 1, 3, 2, 4, 4, 1, 2, 2, 5. Using window Δ = 5, what is the working set at the last reference (the 10th), and what is its size?
Show answer
The window holds the last 5 references: 4, 1, 2, 2, 5. The distinct pages are {1, 2, 4, 5}, so the working set has size 4. The two 2s count once.
Exercise
A system has 120 frames and four processes with working-set sizes 40, 35, 30 and 25. Does the system thrash? What is the smallest number of processes to suspend so that D ≤ m, and which one?
Show answer
D = 40 + 35 + 30 + 25 = 130, which exceeds m = 120, so the system will thrash. Suspending one process is enough if it frees at least 10 frames. Suspending the process with WSS 25 gives D = 105, so the answer is one process. Suspending the WSS 40 process gives D = 90, which also works but leaves more frames unused.
Exercise
The OS observes 15% CPU utilization and a nearly saturated paging disk. A new scheduler version admits more processes to raise utilization. Explain what will happen and what the correct action is.
Show answer
Admitting more processes lowers the frames per process, so page faults increase further and the disk queue grows longer. CPU utilization falls even more, and the system thrashes deeper. The correct action is to reduce the degree of multiprogramming by suspending some processes, so that the remaining ones can keep their working sets resident.