# Counting Based Page Replacement Algorithm in Operating System

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

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

Block 7 replaced with Block 3

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

Block 1 replaced with Block 4

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

Block 4 replaced with Block 7

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

Block 7 replaced with Block 1

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

## 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.

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

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?

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?

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.