Javatpoint Logo
Javatpoint Logo

FIFO Page Replacement Algorithm in C

A page replacement algorithm is required in an operating system that uses paging to manage memory in order to determine which page needs to be replaced when a new page is received.

Page Fault

A page fault occurs when an active application tries to access a memory page that is loaded in virtual memory but not physically in the system. Page errors occur because actual physical memory is substantially less than virtual memory. In the event of a page fault, the operating system might have to swap out one of the current pages for the one that is now required. Different page replacement algorithms offer various suggestions for selecting the appropriate replacement page. All algorithms strive to lower the number of page faults.

FIFO

The simplest algorithm for replacing pages is this one. The operating system maintains a queue for all of the memory pages in this method, with the oldest page at the front of the queue. The first page in the queue is chosen for removal when a page has to be replaced.

Example1: Take page reference strings 1, 3, 0, 3, 5, and 6 and three page slots as an example. All of the spaces are initially vacant, thus when 1, 3, and 0 arrived, they were allotted to the empty slots, which led to 3 Page Faults.

Since 3 are already in memory when it arrives, there are no page faults. Then page number 5 appears; it cannot fit in memory, therefore it takes the position of the oldest page slot, which is 1. ->1Page Fault.

Finally, page 6 appears; however, it is likewise not in memory, thus it takes the position of the oldest page slot, which is 3 ->1 Page Fault.

Total page faults thus equal 5.

Example2: Consider the reference string 0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1. making use of the FIFO page replacement algorithm

Therefore, there were 9 total page defects. Write a function to find the number of page faults given the memory capacity (measured in the number of pages it can carry) and a string representing the pages to be referred to.

Implementation

Let the amount of pages that memory can store serve as the capacity. Set, the current collection of memory pages, shall be.

Below is the code for the above approach:

Output:

Incoming  Frame 1  Frame 2  Frame 3
4                   4              -               - 
1                   4              1              -  
2                   4              1              2 
4                   4              1              2 
5                   5              1              2 
Total Page Faults: 4






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA