Thrashing in os starts when the CPU is spending more time to serve the page faults than executing the process. Thrashing can have severe performance issues on the system. There are two major ways to manage Thrashing while keeping CPU utilization as high as possible.
Scope:
- We will go through the concept of thrashing in os.
- We will learn about the Locality principle and how this is used in the Working-Set
Model to control thrashing in os for example. - We will learn the Page-Fault Frequency(PFF) approach to regulate thrashing with more insights.
Thrashing in Operating System:
In layman terms, Thrashing means the CPU will spend more time on serving Page Faults than executing the pages. To get more insights about Thrashing, refer to this article on Scaler Topics.
Techniques to handle Thrashing in Operating System:
1. Working-Set Model:
Before starting a discussion about this approach we should be aware of “Locality of reference”. Locality means a batch of pages that are actively used by the CPU simultaneously. On top of that, Locality of reference means a strategy of the CPU to use a batch of pages frequently over a short period.
To handle thrashing in Operating Systems This approach uses the concept of fixed-size sliding windows and assumptions of locality principle. Now as per the name itself, Working-Set is a set of active pages(without duplicate memory references) within a specified time window. Here Δ (delta) is that specified time window. If a page is under the consideration of CPU means it must be in the Working Set. On the other hand, if it is not being used then, it should be dropped from the Working Set.
Let's assume we have a sequence of pages demanded by the CPU over the
period as follows. Assume here our sliding window size is 5 and t1 and t2 are the intervals.
...2 6 5 1 2 | 2 1 2 3 4 | 4 9 5 1...
t1 t2
In the above example at “t1”, the Working-Set should be {2,6,5,1}. now once execution reaches point t2 then we can observe that Page Number 5 and 6 are not used by CPU recently and on the contrary CPU requested Page Number 3 and 4 recently so as per the algorithm Page references of 5 and 6 must be removed from Working-Set and Page reference for 3 and 4 should be added into Working-Set. so we can say that at “t2”, the Working-Set should be {2,1,3,4}.
Once Δ (delta) is selected, the Working-Set Model is easy to operate. The Operating System monitors the Working-Set for each process and all processes together. Then there are two possible scenarios, first, if the size of the Working-Set is lesser than the total no of the available frame then Operation System will initiate one more process to increase CPU utilization. On the flip side if the size of the Working-Set is greater than the total no of the available frame then it causes Thrashing in Operating System due to more page faults. In this case, the Operating System will suspend one of the processes and distribute the released frame in the remaining processes in the Working-Set.
2. Page-Fault Frequency:
The Working-Set Model seems a burdensome way to regulate Thrashing in Operating Systems. Another approach is straightforward compared to the Working-Set Model. Thrashing is ultimately a high Page-Fault rate and our ultimate purpose is to regulate it and increase CPU utilization. When the Process requires more pages than available frames then the Page-Fault rate is high. On the contrary, If the Page-Fault rate is low means the process has more available frames than active pages.
In this approach, we can establish upper and lower bounds for Page-Fault frequency. In the first scenario if Page-Fault frequency goes above the upper bound for any process then Operation System will allocate more frames to the process Instead it will release some frames from the process if the Page-Fault frequency is below the lower bound in the second scenario.
Summary:
- Thrashing occurs in os when the page fault rate is higher.
- Using Working-Set Model, Operating System can do pre paging computation and control thrashing in os
- Page-Fault Frequency is the straightforward approach with predefined frequency limits to handle thrashing in os
- Realistically speaking, Thrashing and Techniques to handle it have a huge impact on the performance of the system. The standard practice is to include enough physical memory to avoid thrashing in the system.
Author Bio:
Akash Pawar is a software engineer and has the passion to write technical blogs with expertise in Data Structures and Algorithms, Operating systems and has exposure to large scale System Designs.