Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Thrashing in OS?
3.
Causes of Thrashing in Operating System
4.
Algorithms during Thrashing
4.1.
1. Global Page Replacement
4.2.
2. Local Page Replacement
5.
How to Overcome Thrashing?
6.
Techniques to Prevent Thrashing
6.1.
1. Working-Set Model
6.1.1.
How do we compute Working Sets?
6.2.
2. Page Fault Frequency
6.3.
3. Locality Model
7.
Symptoms of Thrashing in OS and How to Detect it?
8.
Effects on System Performance and User Experience
9.
Difference between Thrashing in OS and Swapping
10.
Frequently Asked Questions
10.1.
What is thrash in operating system?
10.2.
How to detect thrashing in OS?
10.3.
What are some examples of thrashing?
10.4.
What is thrashing in OS Mcq?
11.
Conclusion
Last Updated: Mar 27, 2024
Medium

Thrashing in OS (Operating System)

Author Divyansh Jain
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

"Thrashing" in computer science is when a computer's virtual memory system starts to struggle and slows down because it keeps loading the same information over and over again. This happens because there's not enough main memory, and it can't keep up with storing everything in secondary memory, like your hard drive. Imagine trying to work on several big projects on a small desk; you keep shuffling papers in and out, but you can't get anything done efficiently. That's thrashing.

thrashing in os

What is Thrashing in OS?

Thrashing in OS is a common phenomenon in computer systems. It occurs when the system spends more time swapping pages than actually performing useful tasks. It is caused by a high number of page faults, which happens when the system needs to retrieve a page from the disk because it is not present in memory. Thrashing in operating system is a phenomenon in computing that occurs when virtual memory is employed. 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. As main memory is depleted, more pages must be swapped into and out of virtual memory. 

To know more about thrashing in OS, first, we need to know about page faults and swapping.

  • Page fault: A page fault is a type of interrupt that occurs when a program attempts to access a page of memory that is not currently mapped to physical memory, leading to a disk I/O operation.
  • Swapping: Swapping results in a high rate of hard drive access. Thrashing can last for a long time if the underlying problem is not addressed. Thrashing in operating system has the ability to cause the computer's hard disk to fail completely.
     

Thus, thrashing in operating system is sometimes referred to as disk thrashing.

Disk Thrashing

After visualizing the Graphical representation, 

  • The underlying idea is that if a process is given too few frames, there would be too many and too frequent page faults. As a result, the CPU would perform no useful work, and CPU usage would plummet drastically.
  • The long-term scheduler would then attempt to enhance CPU usage by loading additional processes into memory, increasing the degree of multiprogramming. Unfortunately, this would result in even lower CPU utilization, creating a chain reaction of larger page faults followed by a rise in the degree of multiprogramming, a phenomenon known as Thrashing.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Causes of Thrashing in Operating System

Thrashing in operating systems can occur due to several reasons:

  1. Insufficient Physical Memory: When the available physical RAM is not enough to hold the needed data and programs, leading to excessive swapping between RAM and disk.
  2. Overallocation of Memory: When an operating system allocates more memory to applications than what is available in physical memory, causing constant page swaps.
  3. Poor Memory Management: Inefficient use of memory by applications or the operating system, leading to frequent loading and unloading of data.
  4. Excessive Multitasking: Running too many applications or processes at the same time, which exceeds the system's memory capacity.
  5. Highly Demanding Applications: Applications that require a large amount of memory, leading to frequent swapping when they compete for resources.
  6. Inefficient Swapping Strategy: Poor management of the swap file or partition by the operating system, leading to inefficient use of disk space for memory swapping.

Algorithms during Thrashing

In order to deal with page faults, the operating system uses either the local frames replacement algorithm or the global frames replacement algorithm to try to bring in enough pages in the main memory. Let's explore how various replacement strategies 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 of this, no process can acquire enough frames, and the thrashing in the operating system will get worse. To conclude, when Thrashing in operating system occurs, the global page replacement technique is ineffective.

2. Local Page Replacement

Unlike the Global Page Replacement, the Local Page Replacement will choose pages that belong to that process. As a result, there is a potential that the Thrashing in operating system will be reduced. As previously been demonstrated, there are several drawbacks to Local Page replacement. As a result, local page replacement is merely a different option than global page replacement.

How to Overcome Thrashing?

Thrashing has an adverse effect on hard disk health and system performance. As a result, some steps must be taken to avoid it. The following strategies can be used to solve the thrashing problem:

  • Upgrade the RAM size - As inadequate memory might result in disk thrashing, one approach is to upgrade the RAM. With additional memory, your computer can perform tasks more simply and without having to work as hard. It is, in general, the best long-term answer.
     
  • Replace programs - In order to overcome thrashing, what we can do is we can remove heavy memory-intensive programs with relatively less memory-intensive alternatives.
     
  • Reduce the number of active applications - If you have too many apps running in the background, your system resources will be depleted quickly. Furthermore, the remaining system resource is sluggish, which might cause Thrashing. As a result, when you close an application, certain resources are released, allowing you to prevent Thrashing in operating system to some extent.
     
  • Alter the size of the swap file - There is a probability of occurrence of thrashing also when the swap file is not properly configured in the operating system. We can define swapping as when a page fault occurs. The operating system attempts to retrieve that page from secondary memory and swap it with one of the pages in RAM.

Techniques to Prevent Thrashing

The Local Page replacement is superior to the Global Page replacement, although it has several drawbacks and is not always useful. As a result, here are some more strategies for dealing with Thrashing.

1. Working-Set Model

A process goes from locality to locality when it runs, while a locality is a set of pages that are actively being used together. The working set window is defined by the parameter Δ(theta) in this model. The working set is the collection of pages in the most current Δ page references. The working set contains pages that are currently in use. 

If Δ is too small, the entire locality will not be covered. On the other hand, if Δ is too large, it may overlap numerous localities. The size of the working set is thus the essential feature.

We calculate the working-set size, WSSi, for each process in the system if there are m processes. 

D = ΣWSSi, where D is the overall demand for frames. Each process actively makes use of the pages in its working set. As a result, WSSi frames are required by process i. If the overall demand exceeds the total amount of possible frames (D > m), thrashing will occur because certain processes will run out of frames.

After choosing, the operating system monitors each process's working set and allocates enough frames to that working set to provide it with its working-set size. Another process can be started if there are enough spare frames. The OS selects a process to suspend if the sum of the working-set sizes exceeds the total number of available frames.

The series of numbers in the following diagram represents the pages required by a process. If the working set size is set to nine, the following pages are displayed at times t1 and t2:

working set model

How do we compute Working Sets?

Peter D.Denning introduced the working set parameter as T, which means that the working set is made up of all pages that have been referenced in the recent T seconds. The clock method may be extended to retain an idle time for each page. The working set contains pages with idle times shorter than T.

2. Page Fault Frequency

Page Fault Frequency

This is a straightforward model. We must act depending on the frequency/rate of page faults and assign frames to each process accordingly. For this page-fault rate, we specified an upper bound (UB) and a lower bound (LB). We compare the page fault rate(R) of each process to the stated upper bound(UB) and lower bound(LB).

If R > UB, we can deduce that a process requires additional frames to keep this rate under control. To avoid thrashing, we'll need to dedicate extra frames to it. If there aren't any frames available, the process can be paused until a sufficient number of frames becomes available. The allotted frames should be assigned to another high-paging process once this process has been suspended.

We have more than enough frames for a process if R < LB, and some of them can be given to other processes. We can maintain a balance between frame needs and frame allocation by using the R, UB, and LB.

3. Locality Model

The locality model in thrashing is based on the observation that when a system is thrashing, a large proportion of memory accesses are likely to be concentrated in a few pages of memory that are in high demand. By identifying and prioritizing these pages, it may be possible to reduce the number of page faults and alleviate the thrashing condition. This is because if the system can keep these high-demand pages in physical memory, it will need to page them in and out less frequently, reducing the number of disk I/O operations and improving system performance. The locality model can be applied using techniques such as page replacement algorithms that aim to keep the most frequently accessed pages in physical memory.

Symptoms of Thrashing in OS and How to Detect it?

Thrashing might occur in OS when the system is overloaded with excessive paging, which results in decreased performance of the OS. Here are the different ways to detect Thrashing in OS.

  • Maximum cores of CPU being used, and little or no activity is done.
     
  • Thrashing leads to the swapping of pages between the main memory and disk, so disk activity increases rapidly.
     
  • Frequent page faults can also be a reason for thrashing in OS.

Effects on System Performance and User Experience

Thrashing in OS has various impacts on system performance and user experience.

  • It decreases the overall performance of the system due to excessive cores of the CPU being used.
     
  • Due to the swapping of pages between memory and disk, the response time for user interaction gets increased, which reduces the user experience.
     
  • Thrashing increases application loading time, which reduces the efficiency of the system.
     
  • Disk activity is increased rapidly, which reduces the performance of systems. Due to slow performance and increased loading time of the system and applications, the user might get frustrated, which depicts a bad user experience.

Difference between Thrashing in OS and Swapping

Let us note the differences between thrashing and swapping in OS in the table below.

Thrashing Swapping
Here, the CPU spends most of its time swapping pages in and out of the main memory. It results in a decrease in the system's performance. It is a technique that includes moving an entire process or a part of it from the main memory to the secondary memory and vice versa.
It is caused by overcommitment of the physical memory where the system system tries to manage many processes with only limited RAM. It is done by the OS to manage the memory efficiently and mainly to free up the space in RAM when not required by the process.
It degrades the system's performance. It improves the overall performance of the system.
To resolve it, the system can reduce the number of running processes or can increase the physical memory(RAM). It can be controlled by many memory management techniques to optimize the memory usage.
If not taken care of, it can lead to delays and crashes. It prevents memory exhaustion leading to efficient memory usage.

Frequently Asked Questions

What is thrash in operating system?

Thrashing occurs when page faults and switching occur at a higher frequency, requiring the operating system to spend more time exchanging these pages. Thrashing slows down an operating system as the OS is not doing any productive work during thrashing.

How to detect thrashing in OS?

Because of thrashing, the CPU utilization is going to be reduced or negligible. Thrashing can be detected by comparing the level of CPU use to the level of multiprogramming.

What are some examples of thrashing?

A common example of thrashing is when a system with limited RAM runs many memory intensive programs simultaneously. This can lead the system to a state of thrashing because the system constantly swaps data between RAM and the Hard Drive, making the processing slower.

What is thrashing in OS Mcq?

Thrashing in an operating system is a condition where excessive paging and swapping lead to a major slowdown. It happens when a system spends more time moving data between RAM and disk than executing tasks, severely impacting performance.

Conclusion

We investigated the causes of Thrashing in OS (Operating System) and talked about several ways to overcome it. And then, we also conversed about some other techniques to handle thrashing in the operating system. Hope you learned something from this article. But learning should never stop, so to better understand the Operating system, you can go through the other articles on our platform. 

Recommended Readings:

 

Do upvote our blog to help other ninjas grow. 

Happy Learning!!

Previous article
Difference between Logical Address and Physical Address
Next article
Virtual Memory
Live masterclass