Fifo Page Replacement Algorithm In C

A page fault happens when a running application attempts to reach a memory page that exists in virtual memory but is not present in the physical memory of the system. These errors occur due to the limited capacity of physical memory compared to virtual memory. When a page fault occurs, the operating system may need to exchange one of the existing pages with the one that is needed at that moment. Various page replacement algorithms provide different strategies for choosing the right page to replace, all aiming to minimize the occurrence of page faults.

The most basic method for swapping out pages is as follows. In this approach, the operating system manages a queue containing all memory pages, with the oldest page positioned at the front of the queue. When the need arises to replace a page, the system selects the initial page in the queue for elimination.

Example1: Consider an illustration with page reference strings 1, 3, 0, 3, 5, and 6 along with three page slots. Initially, all slots are empty. As pages 1, 3, and 0 are accessed, they fill the vacant slots, resulting in a total of 3 Page Faults.

Since there are already 3 pages stored in memory upon its arrival, there are no occurrences of page faults. Subsequently, when page number 5 emerges, it is unable to be accommodated in memory. As a result, it replaces the content of the oldest page slot, which in this case is page 1. This situation leads to a single instance of a page fault.

Finally, when page 6 is accessed, it is not present in memory, causing it to replace the oldest page slot, which is 3 ->1 resulting in a Page Fault.

Total page faults thus equal 5.

Using the FIFO page replacement algorithm, let's analyze the given reference string: 0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1.

Hence, there were a total of 9 page faults in the system. Develop a function that determines the quantity of page faults based on the memory capacity (specified in the number of pages it can hold) and a string indicating the pages to be referenced.

Implementation

Let the storage capacity be determined by the number of pages that can be held in memory. Define the current set of memory pages as ```

  1. Begin turning the pages.

i) If the set can hold no more pages.

a) Add pages one at a time into the collection until it is full or all requests for pages have been fulfilled.

b) Maintain the pages in the queue simultaneously to carry out FIFO.

b) Increased page error

ii) Other

Do nothing if the current page is included in the collection.

If not, either a) the current page in the string should be substituted for the first page in the queue since it was the first to be placed into memory, or b) the first page in the queue should be removed.

b) Add the currently viewing page to the queue.

d) Page faults that increase.

  1. Return page errors.
  2. Example
    
    
    
  3. Begin turning the pages.

i) If the set can hold no more pages.

a) Add pages one at a time into the collection until it is full or all requests for pages have been fulfilled.

b) Maintain the pages in the queue simultaneously to carry out FIFO.

b) Increased page error

ii) Other

Do nothing if the current page is included in the collection.

If not, either a) the current page in the string should be substituted for the first page in the queue since it was the first to be placed into memory, or b) the first page in the queue should be removed.

b) Add the currently viewing page to the queue.

d) Page faults that increase.

  1. Return page errors.
  2. Example
    
    
    Below is the code for the above approach:
    
    

    include < stdio.h >

int main

{

int incomingStream = {4 , 1 , 2 , 4 , 5};

int pageFaults = 0;

int frames = 3;

int m, n, s, pages;

pages = sizeof(incomingStream)/sizeof(incomingStream[0]);

printf(" Incoming \ t Frame 1 \ t Frame 2 \ t Frame 3 ");

int temp[ frames ];

for(m = 0; m < frames; m++)

{

temp[m] = -1;

}

for(m = 0; m < pages; m++)

{

s = 0;

for(n = 0; n < frames; n++)

{

if(incomingStream[m] == temp[n])

{

s++;

pageFaults--;

}

}

pageFaults++;

if((pageFaults <= frames) && (s == 0))

{

temp[m] = incomingStream[m];

}

else if(s == 0)

{

temp[(pageFaults - 1) % frames] = incomingStream[m];

}

printf("\n");

printf("%d\t\t\t",incomingStream[m]);

for(n = 0; n < frames; n++)

{

if(temp[n] != -1)

printf(" %d\t\t\t", temp[n]);

else

printf(" - \t\t\t");

}

}

printf("\nTotal Page Faults:\t%d\n", pageFaults);

return 0;

}

Example


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

Example


Input Required

This code uses input(). Please provide values below: