THRASHING AND LOCALITY OF REFERENCE
Ques-Explain thrashing. Also discuss cause of thrashing and its recovery methods.
Ans- Thrashing is a phenomenon that happen when the system spends an excessive
amount of time swapping data between physical memory (RAM) and virtual memory
(disk storage) due to high memory demand and low available resources.
When a system’s physical memory is insufficient to accommodate all the programs that
are currently executing, the operating system uses virtual memory to swap out pages of
memory that are not currently needed to free up space for other programs.
It can result in severe performance degradation, as the system spends more time
swapping data than executing programs.
If the system constantly swaps data back and forth between physical and virtual memory,
it can become overwhelmed and enter a state of Thrashing.
Example 1
Consider a system with 100 processes and 400 available frames.
# of processes= 100
# of Frames= 400
# of frame each process get= 400/100 =4
Example 2
Consider a system with 400 processes and 400 available frames.
# of processes= 400
# of Frames= 400
# of frame each process get= 400/400 =1
Causes of Thrashing
The main causes of thrashing in an operating system are:
1. High degree of multiprogramming:
When too many processes are running on a system, the operating system may not have
enough physical memory to accommodate them all.
This can lead to thrashing, as the operating system is constantly swapping pages of
memory between physical memory and disk.
2. Lack of frames:
Frames are the units of memory that are used to store pages of memory.
If there are not enough frames available, the operating system will have to swap pages of
memory to disk, which can lead to thrashing.
Methods to prevent Thrashing
There are a number of ways to eliminate thrashing, including:
1. Increase the amount of physical memory: This is the most effective way to
eliminate thrashing, as it will give the operating system more space to store pages of
memory in physical memory.
2. Reduce the degree of multiprogramming: This means reducing the number of
processes that are running on the system. This can be done by terminating or suspending
processes (by using Mid-term scheduler), or by denying new processes from starting (by
using Long-term scheduler).
Ques-2 Explain locality of reference.
Ans- Locality of reference refers to a phenomenon in which a computer program tends to
access same set of memory locations for a particular time period.
In other words, Locality of Reference refers to the tendency of the computer program to
access instructions whose addresses are near one another.
The property of locality of reference is mainly shown by:
1. Loops in program cause the CPU to repeatedly execute a set of instructions that
constitute the loop.
2. Subroutine calls, cause the set of instructions are fetched from memory each time
the subroutine gets called.
Even though accessing memory is quite fast, it is possible for repeated calls for data from
main memory can become a bottleneck.
By using faster cache memory, it is possible to speed up the retrieval of frequently used
instructions or data.
Locality of reference is implemented by
1. Temporal Locality
2. Spatial Locality
Temporal Locality
Temporal locality means current data or instruction that is being fetched may be needed
soon.
To store that data or instruction in the cache memory can avoid again searching in main
memory for the same data.
When CPU accesses the current main memory location for reading required data or
instruction, it also gets stored in the cache memory which is based on the fact that same
data or instruction may be needed in near future. This is known as temporal locality.
If some data is referenced, then there is a high probability that it will be referenced again
in the near future.
Spatial Locality
Spatial locality means instruction or data near to the current memory location that is
being fetched, may be needed by the processor soon.