Page Replacement Algorithm
Page Replacement Algorithm
One of the techniques which are used for memory management is paging. In paging, processes
are divided into pages and main memory is divided into frames. Pages of a process are loaded
into frames of main memory when required.
Page Replacement Algorithm is used when a page fault occurs. Page Fault means the page
referenced by the CPU is not present in the main memory.
When the CPU generates the reference of a page, if there is any vacant frame available in the
main memory then the page is loaded in that vacant frame. In another case, if there is no vacant
frame available in the main memory, it is required to replace one of the pages in the main
memory with the page referenced by the CPU.
Page Replacement Algorithm is used to decide which page will be replaced to allocate memory
to the current referenced page.
Different Page Replacement Algorithms suggest different ways to decide which page is to be
replaced. The main objective of these algorithms is to reduce the number of page faults.
Example: Consider the Pages referenced by the CPU in the order are 6, 7, 8, 9, 6, 7, 1, 6, 7, 8,
9, 1
As in the above figure shown, Let there are 3 frames in the memory.
6, 7, 8 are allocated to the vacant slots as they are not in memory.
When 9 comes page fault occurs, it replaces 6 which is the oldest in memory or front element of
the queue.
Then 6 comes (Page Fault), it replaces 7 which is the oldest page in memory now.
Similarly, 7 replaces 8, 1 replaces 9.
Then 6 comes which is already in memory (Page Hit).
Then 7 comes (Page Hit).
Then 8 replaces 6, 9 replaces 7. Then 1 comes (Page Hit).
Number of Page Faults = 9
While using the First In First Out algorithm, the number of page faults increases by increasing
the number of frames. This phenomenon is called Belady's Anomaly.
Let's take the same above order of pages with 4 frames.
In the above picture shown, it can be seen that the number of page faults is 10.
There were 9 page faults with 3 frames and 10 page faults with 4 frames.
The number of page faults increased by increasing the number of frames.
2) Optimal Page Replacement - In this algorithm, the page which would be used after the
longest interval is replaced. In other words, the page which is farthest to come in the upcoming
sequence is replaced.
Example: Consider the Pages referenced by the CPU in the order are 6, 7, 8, 9, 6, 7, 1, 6, 7, 8,
9, 1, 7, 9, 6
First, all the frames are empty. 6, 7, 8 are allocated to the frames (Page Fault).
Now, 9 comes and replaces 8 as it is the farthest in the upcoming sequence. 6 and 7 would
come earlier than that so not replaced.
Then, 6 comes which is already present (Page Hit).
Then 7 comes (Page Hit).
Then 1 replaces 9 similarly (Page Fault).
Then 6 comes (Page Hit), 7 comes (Page Hit).
Then 8 replaces 6 (Page Fault) and 9 replaces 8 (Page Fault).
Then 1, 7, 9 come respectively which are already present in the memory.
Then 6 replaces 9 (Page Fault), it can also replace 7 and 1 as no other page is present in the
upcoming sequence.
The number of Page Faults = 8
This is the most optimal algorithm but is impractical because it is impossible to predict the
upcoming page references.
3 ) Least Recently Used - This algorithm works on previous data. The page which is used
the earliest is replaced or which appears the earliest in the sequence is replaced.
Example: Consider the Pages referenced by the CPU in the order are 6, 7, 8, 9, 6, 7, 1, 6, 7, 8,
9, 1, 7, 9, 6
First, all the frames are empty. 6, 7, 8 are allocated to the frames (Page Fault).
Now, 9 comes and replaces 6 which is used the earliest (Page Fault).
Then, 6 replaces 7, 7 replaces 8, 1 replaces 9 (Page Fault).
Then 6 comes which is already present (Page Hit).
Then 7 comes (Page Hit).
Then 8 replaces 1, 9 replaces 6, 1 replaces 7, and 7 replaces 8 (Page Fault).
Then 9 comes (Page Hit).
Then 6 replaces 1 (Page Fault).
The number of Page Faults = 12
LFU is one of the page replacement policy where an user can replace the least frequency of a
particular operation page. If the page frequency is same in a process, then it will come first on
the replacement list.
4) Least Frequently Used (LFU)
LFU is one of the page replacement policies where a user can replace the least frequency of a
particular operation page. If the page frequency is the same in a process, then it will come first
on the replacement list. i.e., In the LFU Page Replacement method, the page with the minimum
count /frequency is selected for replacement with the page that needs to be entered into the
system.
· How many number of times is a page is repeated, so less the number of times the page is
repeated the higher is the chance that it is going to replaced.
Example:
Pages-> 7 0 1 2 0 3 0 4 2 3 0 3 2
Frame 1
Frame 2
Frame 3
Page Fault
Pages-> 7 0 1 2 0 3 0 4 2 3 0 3 2
Frame 1 7 7 7
Frame 2 0 0
Frame 3 1
Page Fault X X X
3. 0,1,7 have same frequency (1) so now used FIFO, now Frequency 0-2, 1-1, 2-1, 3-0, 4-0,
7-0
Pages-> 7 0 1 2 0 3 0 4 2 3 0 3 2
Frame 1 7 7 7 2 2
Frame 2 0 0 0 0
Frame 3 1 1 1
Page Fault X X X X √
4. Use FIFO to insert 3, 0 frequency is 2 but 1,2 have frequency 1 so we have to use FIFO
for 1 & 2. Frequency 0-3, 1-0, 2-1, 3-1, 4-0, 7-0
Pages-> 7 0 1 2 0 3 0 4 2 3 0 3 2
Frame 1 7 7 7 2 2 2 2
Frame 2 0 0 0 0 0 0
Frame 3 1 1 1 3 3
Page Fault X X X X √ X √
5. Follow the same steps. Frequency: 0-4, 1-0, 2-2, 3-2, 4-0, 7-0
Pages-> 7 0 1 2 0 3 0 4 2 3 0 3 2
Frame 1 7 7 7 2 2 2 2 4 4 3 3 3 3
Frame 2 0 0 0 0 0 0 0 0 0 0 0 0
Frame 3 1 1 1 3 3 3 2 2 2 2 2
Page Fault X X X X √ X √ X X X √ √ √
1 2 3 4 5 6 7 8
Miss Miss Miss Miss Hit Miss Hit Miss Miss Miss Hit Hit Hit