Thrashing in Operating System
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 is referred to as "thrash" in computer science.
We all know that a program needs to be in the main memory, or RAM, to run. Although large programs, such as video games, can be easily downloaded and stored in GBs on secondary memory devices (such as hard disks or SSDs), they eventually need to be transferred to RAM when they are executed. Data can be managed between primary memory and secondary memory using a variety of memory management strategies. Paging is one of the most commonly used strategies. Because RAM is smaller than secondary memory, it cannot completely store programs that are larger than RAM's capacity. As a result, memory management is essential.
When does Thrashing occur?
In Computing, when virtual memory is used, thrashing occurs. It occurs when a computer's virtual memory rapidly swaps data on the hard disk, obstructing most application-level processing. Additional pages need to be moved into and out of virtual memory as the main memory gets full. A very high rate of hard disk access is caused by the swapping. Thrashing can last a long time until the underlying problem is fixed. The computer's hard drive could completely collapse if it is thrashed.
What is thrashing in Operating System?
The operating system needs to spend more time switching these pages if page faults and swapping occur frequently. This is referred to as thrashing in the operating system. Thrashing in the operating system will result in a reduction in CPU usage.
Thrashing in operating system is a peculiarity in processing that happens when virtual memory is utilized. It occurs when the virtual memory of a computer quickly swaps data with data on the hard drive, obstructing most application-level operations. More pages must be moved into and out of virtual memory as the main memory runs out.
As a result, disk thrashing is a term used to describe operating system thrashing.
If the CPU spends more time serving page faults than executing the pages, a process is said to be thrashing. As a result, CPU usage is reduced, and the operating system attempts to increase multiprogramming.
Causes of Thrashing
Operating system thrashing has an effect on how well the execution of the operating system works. Additionally, the operating system suffers from serious performance issues as a result of Thrashing.
The process scheduling mechanism tries to load many processes into memory when the CPU usage is low, allowing the level of multiprogramming to rise. In this instance, there are more processes in the memory than available memory frames. If the frame is not currently vacant when a high-priority process enters memory, the occupied process will be moved to secondary storage and the free frame will be assigned to a higher-priority process.
We could also argue that the process begins to take a long time to swap in the required pages as soon as the memory is full. CPU usage decreases once more as the majority of applications wait for pages.
As a result, operating system thrashing is frequently caused by a lack of frames and a high degree of multiprogramming.
We can say that thrashing occurs due to the following reasons:
- The process scheduling method that adds more processes when the CPU is not being used as much.
- Multiprogramming at a high level.
- Less frames than the processes that are needed.
Page Replacement Algorithms and Effect of Thrashing in OS
Using either the global frames replacement algorithm or the local frames replacement algorithm, the operating system tries to add enough pages to the main memory to deal with page faults. The Effects of these various replacement strategies on thrashing are explained below:
- The Global Page Replacement can bring any page, and if Thrashing in the operating system is detected, it will attempt to bring additional pages. This means that no process can get enough frames, and the operating system's thrashing will get worse. In conclusion, the global page replacement strategy fails when operating system thrashing occurs.
- Local Page Replacement, in contrast to Global Page Replacement, will select pages that are associated with that process. Consequently, there is a possibility that the operating system's thrashing will decrease.