Open In App

Counting Based Page Replacement Algorithm in Operating System

Last Updated : 16 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
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.

Frequently Asked Questions

Q.1: What is a data block in Operating System?

Answer:

The memory in Operating System consists of data blocks which contains data in bytes/words. Every data block has the same size.

Q.2: What is a Frame and a Page in Operating System?

Answer:

The Main Memory is made up of data blocks called Frames. The Secondary Memory is made up of data blocks called Pages. Frame size = Page size.

Q.3: What is a Page Fault in Operating System and why does it occur?

Answer:

Absence of a particular page is known as Page Fault. It occurs when CPU demands for a page which is not present inside the Main Memory.

Q.4: How does Operating System deals with Page Fault?

Answer:

When Page Fault occurs, it brings that page from Secondary Memory to Main Memory so that CPU can access it. If all the frames of Main Memory are occupied with data, it replaces the page using replacement algorithms.

Q.5: Other than Most Frequently Used (MFU) Algorithm, what are some other Page Replacement Algorithms in Operating System?

Answer:

Some of the Page Replacement Algorithms include First In First Out, Last In First Out, Most Recently Used, Least Recently Used, Optimal Page Replacement, etc.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads