In this article, we will cover the topic of thrashing in OS, why it happens, what its effects are and how to prevent it from happening. 

Thrash is a kind of phenomena in the context of computer science. It is a phenomenon when the virtual memory of the system performs poorly.

 This is due to the loading the same pages over and over again due to the incompetency of the main memory to store those pages in the memory. This usually happens when the resources of the virtual memory are being overused.

 Let us get into it and understand why this happens in OS.

What is thrashing?

Before we get an explanation of thrashing in operating system, there are two terms you should be familiar with. They are as follows:

Page Fault: Page fault happens when a program tries to access data or a code that is present in its address space, but the program is unable to find it in the system RAM.

Swapping: After every page fault, the OS tries to swap one of the pages from the RAM with the page that is being asked for.

We agree that the above explanation is difficult to understand, so below is an example that will simplify it for you.

Each program needs a minimum number of pages to be present at all times in the main memory to execute well according to the demands made.

Let us consider three programs and name them P1, P2 and P3.

P1 needs a minimum number of 7 pages in the main memory.

P2 needs a minimum number of 3 pages to work, and so does P3.

So, the total number of pages required to be in the main memory at all times for smooth functioning is 13. But, unfortunately, your main memory can only hold 12 pages at a time.

So, P3 will only get 2 pages in the memory. And since the number of pages required by P3 is not available, it will cause page faults when you use it.

After the page fault, the OS will try to swap one of the other pages, let us say a page from P1, with that of the page required by P3. This is called swapping.

So now, P3 has sufficient pages, but P1 is lacking, which will result again in page fault and then swapping from another page. This cycle will continue on and on until you do something to prevent it.

Now, let us land back on the topic of thrashing.

Thrashing in the OS takes place when page faults and swapping are happening increasingly and recurrently. Due to this, the main focus of the OS is on page faults and swapping, causing it to thrash and causing the CPU to perform inefficiently.

What causes thrashing?

The causes of thrashing in OS are varied. The main causes are as follows:

=> A high degree of multiprogramming in the operating system

=> Shortage of frames in main memory

=> Constant page faults and swapping

As you can probably see, these causes are interrelated. Multiprogramming operating system is what leads to the shortage of frames in RAM, which in turn leads to page fault and swapping. And you know that page fault and swapping are the causes of thrashing.

What are the effects of thrashing?

The operating system tries to free up space for the needed pages by deleting the existing ones in an effort to address the issue of page faults. 

Either the local frames replacement algorithm or the global frames replacement algorithm makes this happen. Below, we will examine these algorithms.

Global Frames Replacement Algorithm

Under this algorithm, the swapping will take place with any of the frames in the memory. Meaning if there is a shortage of frames, the OS will replace frames from another program to make space for the required frames. This will cause thrashing in the other program too. This algorithm can add to the thrashing.

Local Frames Replacement Algorithm

The local frames replacement algorithm allows a program to only replace the frames within its own set. This can decrease the chances of thrashing, but this algorithm has a number of disadvantages too.

How to prevent thrashing?

Now that you are aware of the causes and effects of thrashing, let us discuss the methods to prevent thrashing in OS:

Working Set

When a process is being run, it switches from locality to locality at every step. So, it will only return with fault while it is being changed.

This model is based on parameter A where the working set is a collection of pages in the most current A page references. In other words, the working set includes pages that are currently in use.

If the value of A is too small, the locality will not be included adequately, but if it is too large, the localities are bound to overlap. So, the value of parameter A needs to be in the accurate range.

We will calculate the Working Size Set [WSS] for the process ‘i’ if the total demand for the frames is D.

D = WSSi

We will take m as the total number of frames available in the memory. We now have two of the following possibilities:

- D>m, this will cause thrashing.

- D= or <m, there will be no thrashing in this case.

If the number of frames is low, your system will start thrashing due to shortage and swapping, but if the number of frames is higher, more processes will be able to get pages in the memory.

This method reduces the chances of thrashing while giving you the best multiprogramming operating system, which in turn optimises the performance of the CPU.

Conclusion

Well, now you know what thrashing in the OS is and how it can be prevented. Thrashing is basically a phenomenon that takes place when you are asking too much of the system by performing multiprogramming. Your system does not have that much space, and hence, it will thrash.