To understand Thrashing, You must be aware of Swapping and Page Fault.
Thrashing is a state in which our CPU perform ‘productive’ work less and ‘swapping’ more. CPU is busy in swapping pages, so much that it cannot respond to user program as much as required.
Thrashing degrades the performance of a system
There may be a situation when the memory is full with pages that are accessed frequently.
A page fault will occur if the required page is not present in memory.
In order to make space for swapping in the required page, one of the frequently accessed page is swapped out from main memory.
Soon the page that is swapped out is required for execution and this again results in page fault.
Thus a series of page fault occur and swapping becomes a large overhead.
Thrashing generally occurs in multitasking operating system.
Thrashing is related to degree of multiprogramming in following manner:
§ When a global page replacement algorithm is used, it replaces pages with no regard to the process to which they belong.
§ After increasing the degree of multiprogramming, if a particular process enters a new phase, it may need more frames.
§ Under these circumstances, this process starts faulting and takes away frames from other processes.
§ These process need those pages, however and so they also fault and take frames from other processes.
§ All these faulting processes then queues up for the paging device to swap pages in and out. As processes wait for paging device, CPU utilization decreases.
§ When CPU scheduler sees the decrease in CPU utilization, it increases the degree of multiprogramming. Thus new processes enter the system and take the frames from running processes. This results in more page faults and longer queue for paging devices.
§ As a result, CPU utilization drops even further and thrashing sets in.
Increased Degree of Multiprogramming:
§ With the implementation of virtual memory, each user program occupies less physical memory; as a result more programs can be run at the same time.
§ Thus, CPU utilization and throughput increases.
Methods to handle Thrashing:
§ The Operating System attempts to recover from thrashing by suspending the execution of current processes and preventing the execution of new processes to start.
§ Thrashing can be removed by selecting an appropriate page replacement algorithm.
§ Thrashing can also be avoided by using a local page replacement algorithm.
§ Thrashing can also be prevented by allocating a process as many frames as it needs.