0% found this document useful (0 votes)
55 views

Unit-2 OS

Uploaded by

Bipin Bhadra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Unit-2 OS

Uploaded by

Bipin Bhadra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Demand Paging

Performance of Demand Paging

Page Replacement

Page Replacement-Algoritham

Allocation of frames and Thrashing.

Page Replacement Algorithms in Operating System


 In Virtual Memory Management, Page Replacement Algorithms play an important role. The main
objective of all the Page replacement policies is to decrease the maximum number of page faults.

 Page Fault – It is basically a memory error, and it occurs when the current programs attempt to
access the memory page for mapping into virtual address space, but it is unable to load into the
physical memory then this is referred to as Page fault.

Basic Page Replacement Algorithm in OS:

Page Replacement technique uses the following approach. If there is no free frame, then we will find the one
that is not currently being used and then free it. A-frame can be freed by writing its content to swap space
and then change the page table in order to indicate that the page is no longer in the memory.

1. First of all, find the location of the desired page on the disk.

2. Find a free Frame:

3. a) If there is a free frame, then use it.


b) If there is no free frame then make use of the page-replacement algorithm in order to select
the victim frame.
c) Then after that write the victim frame to the disk and then make the changes in the page
table and frame table accordingly.
4. After that read the desired page into the newly freed frame and then change the page and
frame tables.

5. Restart the process.


Figure: Page Replacement

Page Replacement Algorithms in OS:

 This algorithm helps to decide which pages must be swapped out from the main memory in order to
create a room for the incoming page. This Algorithm wants the lowest page-fault rate.

 Various Page Replacement algorithms used in the Operating system are as follows;

1. FIFO Page Replacement Algorithm

It is a very simple way of Page replacement and is referred to as First in First Out. This algorithm mainly
replaces the oldest page that has been present in the main memory for the longest time.

 This algorithm is implemented by keeping the track of all the pages in the queue.
 As new pages are requested and are swapped in, they are added to the tail of a queue and the page
which is at the head becomes the victim.

 This is not an effective way of page replacement but it can be used for small systems.

Advantages

 This algorithm is simple and easy to use.

 FIFO does not cause more overhead.

Disadvantages

 This algorithm does not make the use of the frequency of last used time rather it just replaces the
Oldest Page.

 There is an increase in page faults as page frames increases.

 The performance of this algorithm is the worst.

2. LIFO Page Replacement Algorithm

This Page Replacement algorithm stands for "Last In First Out".This algorithm works in a similar way to the
LIFO principle.

 In this, the newest page is replaced which is arrived at last in the primary memory

 This algorithm makes use of the stack for monitoring all the pages.

3. LRU Page Replacement Algorithm in OS

This algorithm stands for "Least recent used" and this algorithm helps the Operating system to search those
pages that are used over a short duration of time frame.

 The page that has not been used for the longest time in the main memory will be selected for
replacement.

 This algorithm is easy to implement.

 This algorithm makes use of the counter along with the even-page.

Advantages of LRU

 It is an efficient technique.

 With this algorithm, it becomes easy to identify the faulty pages that are not needed for a long time.

 It helps in Full analysis.


Disadvantages of LRU

 It is expensive and has more complexity.

 There is a need for an additional data structure.

4. Optimal Page Replacement Algorithm

This algorithm mainly replaces the page that will not be used for the longest time in the future. The practical
implementation of this algorithm is not possible.

 Practical implementation is not possible because we cannot predict in advance those pages that will
not be used for the longest time in the future.

 This algorithm leads to less number of page faults and thus is the best-known algorithm

Also, this algorithm can be used to measure the performance of other algorithms.

Advantages of OPR

 This algorithm is easy to use.

 This algorithm provides excellent efficiency and is less complex.

 For the best result, the implementation of data structures is very easy

Disadvantages of OPR

 In this algorithm future awareness of the program is needed.

 Practical Implementation is not possible because the operating system is unable to track the future
request

5. Random Page Replacement Algorithm

As indicated from the name this algorithm replaces the page randomly. This Algorithm can work like any
other page replacement algorithm that is LIFO, FIFO, Optimal, and LRU.

Frames:
 In the OSI model of computer networking, a frame is the protocol data unit at the

data link layer. Frames are the result of the final layer of encapsulation before the
data is transmitted over the physical layer. A frame is "the unit of transmission in a
link layer protocol, and consists of a link layer header followed by a packet."

Allocation of frames in OS:


The main memory of the system is divided into frames.
The OS has to allocate a sufficient number of frames for each process and to do so,
the OS uses various algorithms.

The five major ways to allocate frames are as follows:


There are mainly five ways of frame allocation algorithms in the OS. These are as follows:

1. Equal Frame Allocation


2. Proportional Frame Allocation
3. Priority Frame Allocation
4. Global Replacement Allocation
5. Local Replacement Allocation

1. Proportional frame allocation


The proportional frame allocation algorithm allocates frames based on the size that
is necessary for the execution and the number of total frames the memory has.
The only disadvantage of this algorithm is it does not allocate frames based on
priority. This situation is solved by Priority frame allocation.

2. Priority frame allocation


Priority frame allocation allocates frames based on the priority of the processes and
the number of frame allocations.
If a process is of high priority and needs more frames then the process will be
allocated that many frames. The allocation of lower priority processes occurs after
it.

3. Global replacement allocation


When there is a page fault in the operating system, then the global replacement
allocation takes care of it.
The process with lower priority can give frames to the process with higher priority
to avoid page faults.

4. Local replacement allocation


In local replacement allocation, the frames of pages can be stored on the same
page.
It doesn’t influence the behavior of the process as it did in global replacement
allocation.

5. Equal frame allocation


In equal frame allocation, the processes are allocated equally among the processes
in the operating system.

The only disadvantage in equal frame allocation is that a process requires more
frames for allocation for execution and there are only a set number of frames.

What is Thrash?
In computer science, thrash is the poor performance of a virtual memory (or paging) system when the same
pages are being loaded repeatedly due to a lack of main memory to keep them in memory. Depending on the
configuration and algorithm, the actual throughput of a system can degrade by multiple orders of magnitude.

In computer science, thrashing occurs when a computer's virtual memory resources are overused, leading to
a constant state of paging and page faults, inhibiting most application-level processing. It causes the
performance of the computer to degrade or collapse. The situation can continue indefinitely until the user
closes some running applications or the active processes free up additional virtual memory resources.

To know more clearly about thrashing, first, we need to know about page fault and swapping.

o Page fault: We know every program is divided into some pages. A page fault occurs when a
program attempts to access data or code in its address space but is not currently located in the system
RAM.
o Swapping: Whenever a page fault happens, the operating system will try to fetch that page from
secondary memory and try to swap it with one of the pages in RAM. This process is called
swapping.

Thrashing is when the page fault and swapping happens very frequently at a higher rate, and then the
operating system has to spend more time swapping these pages. This state in the operating system is known
as thrashing. Because of thrashing, the CPU utilization is going to be reduced or negligible.
The basic concept involved is that if a process is allocated too few frames, then there will be too many and
too frequent page faults. As a result, no valuable work would be done by the CPU, and the CPU utilization
would fall drastically.

The long-term scheduler would then try to improve the CPU utilization by loading some more processes into
the memory, thereby increasing the degree of multiprogramming. Unfortunately, this would result in a
further decrease in the CPU utilization, triggering a chained reaction of higher page faults followed by an
increase in the degree of multiprogramming, called thrashing.

Algorithms during Thrashing


Whenever thrashing starts, the operating system tries to apply either the Global page replacement Algorithm
or the Local page replacement algorithm.

1. Global Page Replacement

Since global page replacement can bring any page, it tries to bring more pages whenever thrashing is found.
But what actually will happen is that no process gets enough frames, and as a result, the thrashing will
increase more and more. Therefore, the global page replacement algorithm is not suitable when thrashing
happens.

2. Local Page Replacement

Unlike the global page replacement algorithm, local page replacement will select pages which only belong
to that process. So there is a chance to reduce the thrashing. But it is proven that there are many
disadvantages if we use local page replacement. Therefore, local page replacement is just an alternative to
global page replacement in a thrashing scenario.

Causes of Thrashing
Programs or workloads may cause thrashing, and it results in severe performance problems, such as:

o If CPU utilization is too low, we increase the degree of multiprogramming by introducing a new system. A
global page replacement algorithm is used. The CPU scheduler sees the decreasing CPU utilization and
increases the degree of multiprogramming.
o CPU utilization is plotted against the degree of multiprogramming.
o As the degree of multiprogramming increases, CPU utilization also increases.
o If the degree of multiprogramming is increased further, thrashing sets in, and CPU utilization drops sharply.
o So, at this point, to increase CPU utilization and to stop thrashing, we must decrease the degree of
multiprogramming.

How to Eliminate Thrashing


Thrashing has some negative impacts on hard drive health and system performance. Therefore, it is
necessary to take some actions to avoid it. To resolve the problem of thrashing, here are the following
methods, such as:

o Adjust the swap file size:If the system swap file is not configured correctly, disk thrashing can also happen
to you.
o Increase the amount of RAM: As insufficient memory can cause disk thrashing, one solution is to add more
RAM to the laptop. With more memory, your computer can handle tasks easily and don't have to work
excessively. Generally, it is the best long-term solution.
o Decrease the number of applications running on the computer: If there are too many applications running
in the background, your system resource will consume a lot. And the remaining system resource is slow that
can result in thrashing. So while closing, some applications will release some resources so that you can avoid
thrashing to some extent.
o Replace programs: Replace those programs that are heavy memory occupied with equivalents that use less
memory.

Techniques to Prevent Thrashing


The Local Page replacement is better than the Global Page replacement, but local page replacement has
many disadvantages, so it is sometimes not helpful. Therefore below are some other techniques that are used
to handle thrashing:

1. Locality Model

A locality is a set of pages that are actively used together. The locality model states that as a process
executes, it moves from one locality to another. Thus, a program is generally composed of several different
localities which may overlap.

For example, when a function is called, it defines a new locality where memory references are made to the
function call instructions, local and global variables, etc. Similarly, when the function is exited, the process
leaves this locality.

2. Working-Set Model

This model is based on the above-stated concept of the Locality Model.

The basic principle states that if we allocate enough frames to a process to accommodate its current locality,
it will only fault whenever it moves to some new locality. But if the allocated frames are lesser than the size
of the current locality, the process is bound to thrash.

3. Page Fault Frequency

A more direct approach to handle thrashing is the one that uses the Page-Fault Frequency concept.
The problem associated with thrashing is the high page fault rate, and thus, the concept here is to control the
page fault rate.

If the page fault rate is too high, it indicates that the process has too few frames allocated to it. On the
contrary, a low page fault rate indicates that the process has too many frames.

Upper and lower limits can be established on the desired page fault rate, as shown in the diagram.

If the page fault rate falls below the lower limit, frames can be removed from the process. Similarly, if the
page faults rate exceeds the upper limit, more frames can be allocated to the process.

In other words, the graphical state of the system should be kept limited to the rectangular region formed in
the given diagram.

If the page fault rate is high with no free frames, some of the processes can be suspended and allocated to
them can be reallocated to other processes. The suspended processes can restart later.

You might also like