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.
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.