Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fifo Page Replacement Algorithm
- #include <stdio.h>
- #include <stdbool.h>
- // Function which runs the algorithm and in the end, returns no. of page faults
- int FIFO_algorithm(int totalPages, int pages[], int totalFrames, int frames[])
- {
- int i, j, currentFrame = 0;
- int pageFaults = 0;
- for(i = 0; i < totalPages; i++)
- {
- int currentPage = pages[i];
- // Check if Page Already Exists
- bool found = false;
- for(j = 0; j < totalFrames; j++)
- {
- if(frames[j] == currentPage)
- {
- found = true;
- break;
- }
- }
- // If Page Already Exists, Page Hit
- if(found == true)
- {
- for(j = 0; j < totalFrames; j++)
- {
- printf("%d \t", frames[j]);
- }
- printf("Page Hit !\n");
- }
- // If not, put currentPage in currentFrame and Increment Current Pointer
- else
- {
- frames[currentFrame] = currentPage;
- // If currentFrame is greater than totalFrames, make it 0 again
- currentFrame = (currentFrame + 1) % totalFrames;
- // Increment Page Faults
- pageFaults++;
- for (j = 0; j < totalFrames; j++)
- {
- printf("%d \t", frames[j]);
- }
- printf("Page Fault !\n");
- }
- }
- return pageFaults;
- }
- int main ()
- {
- int totalPages = 13;
- int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
- int totalFrames = 4;
- int frames[totalFrames];
- int i;
- for(i = 0; i < totalFrames; i++)
- {
- frames[i] = -1;
- }
- printf("Total Number of Page Faults = %d\n", FIFO_algorithm(totalPages, pages, totalFrames, frames));
- }
Advertisement
Add Comment
Please, Sign In to add comment