You are on page 1of 13

Page Replacement Algorithms in Operating Systems

In an operating system that uses paging for memory management, a page


replacement algorithm is needed to decide which page needs to be replaced when
new page comes in.
Page Fault – A page fault happens when a running program accesses a memory
page that is mapped into the virtual address space, but not loaded in physical
memory.
Since actual physical memory is much smaller than virtual memory, page faults
happen. In case of page fault, Operating System might have to replace one of the
existing pages with the newly needed page. Different page replacement
algorithms suggest different ways to decide which page to replace. The target for
all algorithms is to reduce the number of page faults.

Page Replacement Algorithms :


•First In First Out (FIFO)
•Optimal Page replacement –
•Least Recently Used –
The page replacement algorithm decides which memory page is to
be replaced. The process of replacement is sometimes called swap
out or write to disk. Page replacement is done when the requested
page is not found in the main memory (page fault).
There are two main aspects of virtual memory, Frame allocation
and Page Replacement. It is very important to have the optimal
frame allocation and page replacement algorithm. Frame allocation
is all about how many frames are to be allocated to the
process while the page replacement is all about determining
the page number which needs to be replaced in order to make
space for the requested page.
First In First Out (FIFO) –
This is the simplest page replacement algorithm. In this algorithm, the operating system
keeps track of all pages in the memory in a queue, the oldest page is in the front of the queue.
When a page needs to be replaced page in the front of the queue is selected for removal.

Example1: Consider a reference string: 4, 7, 6, 1, 7, 6, 1, 2, 7, 2. the number of frames in


the memory is 3. Find out the number of page faults respective to:

Example2: 1, 2, 3, 2, 1, 5, 2, 1, 6, 2, 5, 6, 3, 1, 3,6, 1,2,4,3

Example3:

70120 304230321201701
Example2: 1, 2, 3, 2, 1, 5, 2, 1, 6, 2, 5, 6, 3, 1, 3,6, 1,2,4,3

1 1 1 1 1 1 5 5 5 5 2 2 2 2 1 1 1 1 1 4 4
2 2 2 2 2 2 2 1 1 1 5 5 5 5 5 6 6 6 6 3
3 3 3 3 3 3 3 6 6 6 6 3 3 3 3 3 2 2 2

p p
p pf
f h
f

No of page faults=14
Hits=6
FIFO

•Advantages –
• It is simple and easy to understand & implement.
•Disadvantages –
• The process effectiveness is low.
• When we increase the number of frames while using FIFO, we are giving
more memory to processes. So, page fault should decrease, but here the
page faults are increasing. This problem is called as Belady’s Anomaly.
• Every frame needs to be taken account off.
FIFO

Advantages
•Simple and easy to implement.
•Low overhead.
Disadvantages
•Poor performance.
•Doesn’t consider the frequency of use or last used time, simply replaces the oldest
page.
•Suffers from Belady’s Anomaly(i.e. more page faults when we increase the number
of page frames).
Least Recently Used (LRU)
• Least Recently Used page replacement algorithm keeps track of page
usage over a short period of time. It works on the idea that the pages
that have been most heavily used in the past are most likely to be used
heavily in the future too.
• In LRU, whenever page replacement happens, the page which has not
been used for the longest amount of time is replaced.

Example 1:

Example 2:

0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1

Example 3:

(7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2)
(7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2)

7 7 7 2 2 2 2 4 4 4 0 0 0
0 0 0 0 0 0 0 0 3 3 3 3
1 1 1 3 3 3 2 2 2 2 2

Page faults=9
Page hits =4
Least Recently Used (LRU)

Advantages
•Efficient.
•Doesn't suffer from Belady’s Anomaly.
Disadvantages
•Complex Implementation.
•Expensive.
•Requires hardware support.
Optimal Page algorithm
• An optimal page-replacement algorithm has the lowest page-fault rate of all
algorithms. An optimal page-replacement algorithm exists, and has been called
OPT or MIN.
• Replace the page that will not be used for the longest period of time. Use the time
when a page is to be used.
Example 1:

Example 2:

0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1

Example 3:

(7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2,1,2,0,1,7,0,1)
1 1 1 1 1 1 1 1 6 6 6 6
2 2 4 5 5 5 5 5 5 2 2
3 3 3 3 3 3 3 3 3 3
0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1

0 0 0 0 0 0 0 0 3 3 3 3
2 2 6 4 4 4 4 4 4 2 2
1 1 1 1 1 1 1 1 1 1

1,2,3,4,1,2,5,1,2,3,4,5 Using fifo, lru and optimal.


Advantages
•Easy to Implement.
•Simple data structures are used.
•Highly efficient.
Disadvantages
•Requires future knowledge of the program.
•Time-consuming.

You might also like