Sub topic 14
Thrashing and the Working-Set Model
Assume you know paging, page faults, page replacement, and basic process states. This chapter explains what happens when processes do not have enough physical frames and how an operating system can detect and control that condition.
Thrashing
Thrashing is a condition in which a process spends more time handling page faults than executing useful instructions. The process repeatedly loads pages into memory and evicts pages that it will soon need again.
Thrashing usually occurs when the total number of frames allocated to active processes is smaller than the pages they are actively using. Adding more processes can make the problem worse because each process receives fewer frames.
Typical symptoms include:
- A very high page-fault rate.
- Low CPU utilization, because processes wait for disk operations.
- Heavy disk activity.
- Little useful progress by processes.
- A scheduler that admits more processes because the CPU appears idle, further increasing the problem.
The Working-Set Model
A process's working set is the set of pages that it has referenced during a recent time interval. Let Δ be the length of that interval. The working set at time t is written as:
W(t, Δ)
The operating system estimates this set from recent page references. If a process has referenced pages 2, 5, 7, and 8 during the interval, its working-set size is 4, even if it referenced some of those pages many times.
| Term | Meaning | Column | Column |
|---|---|---|---|
| `Δ` | Number of recent memory references examined | ||
| `W(t, Δ)` | Pages referenced during that interval | ||
| ` | W(t, Δ) | ` | Number of frames the process needs to keep its recent locality |
A locality is a group of pages that a process uses repeatedly during a period. For example, a loop may repeatedly access its instructions, an array, and a few variables. If the allocated frames can contain this locality, the process has few faults. If they cannot, the process may thrash.
The system can estimate total demand as:
D = Σ |W_i(t, Δ)|
where D is the total number of frames needed by all active processes. If D is greater than the number of available frames, the system is oversubscribed. It should reduce the degree of multiprogramming, allocate more frames to some processes, or suspend a process.
Detecting and Controlling Thrashing
The working-set model is predictive: it estimates how many frames each process needs. Another method is page-fault frequency (PFF). PFF measures how often a process faults.
- If the fault rate is above an upper threshold, give the process more frames.
- If the fault rate is below a lower threshold, reclaim some frames.
- If no frames are available, suspend a process rather than allowing every process to thrash.
Global replacement lets a process take frames from other processes. This can spread the damage. Local replacement restricts eviction to the process's own frames, giving more predictable behavior. Either policy still needs enough total frames.
def needs_more_frames(recent_references, window, high_limit):
working_set = set(recent_references[-window:])
return len(working_set) > high_limit
references = [1, 2, 3, 2, 1, 4, 5, 4]
print(needs_more_frames(references, 5, 3)) # TrueThe function approximates a working set by taking the distinct pages among the most recent references. Real systems use hardware reference information and periodic sampling rather than scanning every reference in software.
Exercise
Three processes have working-set sizes of 4, 6, and 5 frames. The system has 14 usable frames. What should the operating system conclude?
Show answer
The total demand is 15 frames, which exceeds the 14 available frames. The system is at risk of thrashing and should reduce demand, such as by suspending a process or changing frame allocation.