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
2.1.
Causes of Thrashing in OS
2.2.
How to Overcome Thrashing
3.
Techniques to Handle Thrashing
3.1.
1. Working Set Model
3.2.
2. Page Fault Frequency
4.
Frequently Asked Questions
4.1.
When could page fault occur?
4.2.
What is paging in OS?
4.3.
What is the meaning of thrashing in OS?
5.
Conclusion
Last Updated: Mar 27, 2024

Thrashing in Operating System

Author soham Medewar
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM
Operating Systems

Introduction

As we all know, a programme must be present in the main memory, or RAM, in order to be executed. We can easily download and store large applications in GBs, such as video games, in secondary memory devices (hard discs or SSDs), but they must eventually be relocated to RAM when they are executed. There are numerous memory management approaches available for managing data between secondary and primary memory. Paging is one of the most commonly employed strategies. Memory management is required since RAM is lower in size than secondary memory and so cannot store any programmes that are larger than the size of RAM completely.

Also Read, FCFS Scheduling Algorithm, Multiprogramming vs Multitasking

What is Thrashing

Due to frequent page faults, the system may spend the majority of its time multiprogramming, transporting pages between the main memory and the secondary memory. This is referred to as thrashing.

If the CPU spends more time serving page errors than executing pages, the process is said to be thrashing. This results in low CPU utilization, and the operating system responds by attempting to raise the degree of multiprogramming.

Degree of Multiprogramming

The above graph says that CPU utilization increases as the degree of multiprogramming increases. But after a certain degree of multiprogramming, there is a drastic drop in CPU utilization and thus thrashing occurs. I will be explaining thrashing using an example.

 

Consider two processes, P1 and P2; each process is broken down to 15 pages. Each process needs a minimum of 5 pages to properly execute with demand paging. It means that a minimum of 5 pages must be inside the main memory(RAM) to execute the processes. We have a main memory that has a capacity of only 8 pages. Then the memory will be filled in the following way.

P1

P1

P1

P1

P1

P2

P2

P2

In the above main memory block first five blocks are being taken by process P1, and the rest three blocks are taken by process P2.

Now, process P1 will run smoothly, whereas process P2 will not be executed; a minimum of 5 pages are required to properly execute process P2. Consider that page1 of process P2 requires a page2 of process P2. Similarly, page2 requires page3, and page3 requires page4 but page4 is not present in the main memory. So, page4 will be swapped with page1. But page4 requires a page1 to execute; again, page2 will be swapped with a page2. But page2 requires page3. This process will go endlessly, and thus it creates thrashing.

Causes of Thrashing in OS

There are three main causes of thrashing in OS they are as follows:

  • A higher degree of multiprogramming.
  • There are fewer frames than are required by the process.
  • When CPU utilization is low, the process scheduling strategy swaps in more processes.

How to Overcome Thrashing

  • Thrashing can be avoided by allocating each process as many frames as it requires during execution. The locality model can be used to determine the number of frames required by a process. A locality is a collection of pages that are frequently used together. According to the locality model, a process switches from one locality to another during execution, and these localities may overlap. We need to give a process enough frames to halt page faulting at the current location.
  • If the system is already thrashing, the process scheduler can be told to stop some of the processes.

 

Must Read Multiprocessing Operating System, Open Source Operating System.

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

Techniques to Handle Thrashing

1. Working Set Model

This model is based on the Locality Model concept.

The core idea is that if we provide a process with enough frames to accommodate its present locality, it will only fail when it transfers to a different locality. However, if the allotted frames are smaller than the size of the current locality, the process will thrash.

According to this paradigm, 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 precision is determined by the value of parameter A. Working sets may overlap if A is too large. However, with smaller values of A, the locality may not be completely covered.

If D is the desired value of the frames and WSS is the process i’s working set size, D = ∑WSSi.

If there are 'm' frames available in the memory, there are two possibilities:

  1. D>m, i.e., total demand exceeds the number of frames, and thrashing occurs because certain processes do not receive enough frames.
  2. If D <= m, there would be no thrashing.

2. Page Fault Frequency

The Page-Fault Frequency notion is a more direct technique for dealing with thrashing.

Page Fault Frequency

Thrashing is connected with a high page fault rate. Hence the goal 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, suggests that the process has too many frames.

As illustrated in the picture, upper and lower limitations on the intended page fault rate can be set.

Frames can be deleted from the process if the page fault rate falls below the lower limit. Similarly, if the page failure rate exceeds the upper limit, the process can be allotted more frames.

In other words, the system's graphical state should be restricted to the rectangular region represented in the given diagram.

If the page fault rate is high and there are no free frames, some processes can be halted, and frames allocated to them can be redistributed to other processes. The halted processes can then be resumed at a later time.

You can also read about the memory hierarchy Batch Operating System and Process Management in OS

Frequently Asked Questions

When could page fault occur?

A page fault happens when a page that has not been brought into the main memory is accessed. If the memory access is invalid, the operating system aborts the programme. If it is valid, a free frame is found, and I/O is requested to read the required page into it.

What is paging in OS?

Paging is a memory management function in which a computer stores and retrieves data from a device's secondary storage to the primary storage.

What is the meaning of thrashing in OS?

Thrashing is a phenomenon in which excessive paging operations occur in a virtual storage system (an operating system that maintains its logical storage or memory in units called pages). A thrashing system can be seen as either very slow or as having come to a standstill.

Conclusion

In this article, we have discussed the following topics:

  • Definition of thrashing
  • Causes of thrashing
  • Working Set Model
  • Page Fault Frequency


Recommended Readings: 


Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Want to learn more about Web Development or Android Development? Here is an amazing course for both by coding ninjas.

Happy Learning!

Previous article
Swapping in Operating System (OS)
Next article
Characteristics of Operating System
Live masterclass