Open In App

Counting Based Page Replacement Algorithm in Operating System

Last Updated : 16 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Counting Based Page Replacement Algorithm replaces the page based on count i.e. number of times the page is accessed in the past. If more than one page has the same count, then the page that occupied the frame first would be replaced.

Page Replacement: Page Replacement is a technique of replacing a data block (frame) of Main Memory with the data block (page) of Secondary Memory when all the frames of Main Memory are occupied and CPU demands for the data block that is not available inside Main Memory. Technically, when a Page Fault occurs.

Two Types of Counting-Based Algorithms

  • Most Frequently Used (MFU) Algorithm: It replaces the page with a count greater than other pages i.e. which is accessed a maximum number of times in the past.
  • Least Frequently Used (LFU) Algorithm: It replaces the page with a count lesser than other pages i.e. which is accessed a minimum number of times in the past.

Example

Consider a Main Memory with a number of frames = 4 and the following are the data block access requests made by the CPU.

CPU Requests - 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 7, 3, 2, 1

Given, the number of frames = 4. Initially, all are empty

Using Least Frequently Used (LFU) Algorithm

(i) The 4 frames are initially empty, the first 4 blocks occupy them.

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

Step 1

(ii) Block 0 has already occupied the frame.

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

Step 2

(iii) Blocks 2,1,7 are accessed least (once) and Block 7 occupied the frame first, so, Block 7 replaced with incoming Block 3.

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

Step 3
Block 7 replaced with Block 3


(iv) Blocks 3,0 have already occupied the frame.

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

Step 4

(v) Blocks 2,1,3 are accessed least (once) and Block 1 occupied the frame first, so, Block 1 replaced with incoming Block 4.

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

Step 5
Block 1 replaced with Block 4


(vi) Blocks 2,3 have already occupied the frame.

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

Step 6

(vii) Block 4 is accessed minimum times (once) so replaced with incoming Block 7.

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

Step 7
Block 4 replaced with Block 7


(viii) Blocks 3,2 have already occupied the frame.

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

Step 8

(ix) Block 7 is accessed minimum times (twice) so replaced with incoming Block 1.

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

Step 9
Block 7 replaced with Block 1

Number of Page faults = Number of blocks not already present and brought inside Main Memory

Page faults for the above example = 8

Please refer:- Example using Most Frequently Used (MFU) Algorithm to know how MFU algorithm works.

Comparing MFU and LFU Counting Based Algorithms

Most Frequently Used

Least Frequently Used

Replaces page which is accessed maximum number of times.

Replaces page which is accessed minimum number of times.

Since most frequently page is replaced, this increases the number of page faults as in future the page has higher chances to be accessed again

Since least frequently page is replaced, this decreases the number of page faults as in future the page has lower chances to be accessed again.


Next Article

Similar Reads