In Computer science, Thrash is a term used to describe a virtual memory system’s poor performance when the same pages are loaded repeatedly due to a lack of main memory to store them in secondary memory. So before discussing thrashing in os we will understand some basic terms like pages and virtual memory.
Small fixed-size chunks of the program are called pages and these pages are mapped to the frames in the main memory. Each program page has a fixed size that is equal to the frame size in the main memory. To realize the concept of virtual memory, we use demand paging, which includes loading a page containing the code that is currently required. A process is a program that is stored in the main memory. When a running process’s requested page is not present in the main memory, a page fault exception is elevated, requesting an IO operation (page fault service) to load the requested page from the secondary memory to the main memory.
What is Thrashing in Operating System
Thrashing in operating systems is a computing phenomenon that occurs when virtual memory is used. It occurs when a computer’s virtual memory rapidly exchanges data for data on the hard drive, to the exclusion of most application-level operations. If the CPU spends more time serving page faults than executing pages, the process is said to be thrashing. This results in low CPU utilization, and the operating system responds by attempting to increase the degree of multiprogramming.
Following the visualization of the graphical representation
The underlying idea is that if a process is given too few frames, too many and frequent page faults will occur. As a result, the CPU would do no useful work and CPU utilization would plummet significantly.
The long-term scheduler would then try to improve CPU usage by loading more processes into memory, thereby increasing the degree of multiprogramming. Unfortunately, this would result in even lower CPU utilization, which would set off a chain reaction of larger page faults, followed by an increase in the degree of multiprogramming, a phenomenon known as Thrashing.
Page Replacement Algorithms and Effect of Thrashing
To deal with page faults, the operating system uses either the global frames replacement algorithm or the local frames replacement algorithm to bring in enough pages in the main memory. Let’s see how these replacement algorithms affect thrashing.
1. Global Page Replacement
The Global Page replacement has the ability to bring any page, and once Thrashing in the operating system is detected, it attempts to bring more pages. As a result, no process can acquire enough frames, and the thrashing in the operating system worsens. To summarise, when Thrashing occurs in the operating system, the global page replacement technique is ineffective.
2. Local Page Replacement
In contrast to the Global Page Replacement, the Local Page Replacement will select pages that are part of that process. As a result, there is a chance that the operating system’s thrashing will be reduced. As previously demonstrated, there are several disadvantages to using Local Page replacement. As a result, local page replacement is simply an alternative to global page replacement.
Causing of Thrashing in OS
Thrashing in the operating system has an effect on the execution performance of the operating system. Furthermore, thrashing degrades the operating system’s performance.
When CPU usage is low, the process scheduling mechanism attempts to load many processes into memory, increasing the degree of Multiprogramming. In this case, there are more processes in memory than there are memory frames available. When a high-priority process enters memory, if the frame is not already occupied by another process, the other process will be moved to secondary storage, and the free frame will be assigned to a higher-priority process.
How to Overcome Thrashing in OS?
Thrashing in the OS is harmful to hard disc health and system performance. As a result, some precautions are required. Thrashing in the operating system can be avoided by allocating as many frames as each process requires during execution. A process’s number of frames can be determined using the locality model. A locality is a grouping of pages that are commonly used together. A process switches from one locality to another during execution, and these localities may overlap, according to the locality model. We must provide a process with enough frames to prevent page faulting in the current location. The Working-set model goes into greater detail on this.If the system is already thrashing, the process scheduler can be told to stop some of the processes.
Technique to Prevent Thrashing in OS
Given below are the Techniques to prevent thrashing in OS:
1) Working Set Model
This model is based on the above-mentioned Locality Model concept.
The basic principle states that if we give a process enough frames to accommodate its current locality, it will only fail when it moves to a new locality. However, if the allocated frames are smaller than the size of the current locality, the process will thrash.
According to this model, the working set is defined as the set of pages in the most recent ‘A’ page references based on parameter A. As a result, all actively used pages would always become a part of the working set. The working set’s accuracy is determined by the value of parameter A. Working sets may overlap if A is too large. However, for smaller values of A, the locality may not be completely covered.
If D represents the total demand for frames and WSSi represents the working set size for the process I then
D = ⅀ WSSi
If’m’ is the number of frames available in memory, there are two options:
When D>m, i.e., total demand exceeds the number of frames, thrashing occurs because some processes do not receive enough frames.
If D<=m, there would be no thrashing.
More processes can be loaded into memory if there are enough extra frames. However, if the sum of working set sizes exceeds the availability of frames, some processes must be suspended (swapped out of memory).
This method prevents thrashing while also ensuring the greatest degree of multiprogramming possible. As a result, it optimises CPU utilisation.
2) Page Fault Frequency
The Page-Fault Frequency concept is a more direct approach to dealing with thrashing.
The high page fault rate is associated with thrashing, so the concept here is to control the page fault rate.
If the page fault rate is too high, it indicates that the process is receiving too few frames. A low page fault rate, on the other hand, indicates that the process has too many frames.
As shown in the diagram, upper and lower limits on the desired page fault rate can be set.
Frames can be removed from the process if the page fault rate falls below the lower limit. Similarly, if the rate of page faults exceeds the maximum, more frames can be allocated to the process.
In other words, the system’s graphical state should be restricted to the rectangular region defined by the given diagram.If the page fault rate is high and there are no available frames, some processes can be suspended and the resources allocated to them reallocated to other processes. The suspended processes can be resumed at a later time.
Conclusion
Thrashing has an effect on the performance of the operating system’s execution. Thrashing also has a negative impact on the operating system’s performance. When the CPU is underutilized, the process scheduling mechanism attempts to load multiple processes into memory at the same time, thereby increasing the degree of Multiprogramming. Thrashing in os can be overcome by allocating each process as many frames as it requires during execution. Techniques to prevent thrashing in os are working set model and page fault frequency.
Frequently Asked Questions(FAQs)
Here are some FAQs on thrashing in os :
Q1: How can thrashing be detected in Operating System?
A: The system’s performance metrics can be used to detect thrashing. These metrics include the system’s CPU utilization, the amount of time spent in disk I/O, and the amount of time spent paging. If the system’s CPU utilization is low, and the system spends a significant amount of time in disk I/O and paging, then it’s likely that the system is thrashing.
Q2: What are the effects of thrashing on system performance?
A: Thrashing has a severe impact on system performance. The system spends more time swapping pages between main memory and disk than executing useful work, leading to a slowdown in the overall system performance. This can result in a system becoming unresponsive and sluggish, leading to a poor user experience.
Q3: How can thrashing be prevented in Operating System?
A: Thrashing can be prevented by using techniques like memory management, virtual memory, and paging. The system can also use techniques like load balancing and memory swapping to prevent thrashing. Additionally, the system can use monitoring tools to detect and respond to thrashing, such as adjusting the memory allocation to improve performance.
Q4: What is the difference between thrashing and deadlock in Operating System?
A: Thrashing occurs when the system spends more time swapping pages between main memory and disk than executing useful work. Deadlock occurs when two or more processes are waiting for each other to release resources, and as a result, none of the processes can continue. While both thrashing and deadlock can cause a system to become unresponsive, they are caused by different issues in the system.