Sreekar_0125

FIFO Page Replacement Algorithm

Aug 14th, 2023
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. // Fifo Page Replacement Algorithm
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4.  
  5.  
  6. // Function which runs the algorithm and in the end, returns no. of page faults
  7.  
  8. int FIFO_algorithm(int totalPages, int pages[], int totalFrames, int frames[])
  9. {
  10.     int i, j, currentFrame = 0;
  11.     int pageFaults = 0;
  12.  
  13.     for(i = 0; i < totalPages; i++)
  14.     {
  15.         int currentPage = pages[i];
  16.  
  17.         // Check if Page Already Exists
  18.         bool found = false;
  19.         for(j = 0; j < totalFrames; j++)
  20.         {
  21.             if(frames[j] == currentPage)
  22.             {
  23.                 found = true;
  24.                 break;
  25.             }
  26.         }
  27.         // If Page Already Exists, Page Hit
  28.         if(found == true)
  29.         {
  30.             for(j = 0; j < totalFrames; j++)
  31.             {
  32.                 printf("%d \t", frames[j]);
  33.             }
  34.             printf("Page Hit !\n");
  35.         }
  36.         // If not, put currentPage in currentFrame and Increment Current Pointer
  37.         else
  38.         {
  39.             frames[currentFrame] = currentPage;
  40.             // If currentFrame is greater than totalFrames, make it 0 again
  41.             currentFrame = (currentFrame + 1) % totalFrames;
  42.  
  43.  
  44.             // Increment Page Faults
  45.             pageFaults++;
  46.             for (j = 0; j < totalFrames; j++)
  47.             {
  48.                 printf("%d \t", frames[j]);
  49.             }
  50.             printf("Page Fault !\n");
  51.         }
  52.     }
  53.     return pageFaults;
  54. }
  55.  
  56. int main ()
  57. {
  58.     int totalPages = 13;
  59.     int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
  60.  
  61.     int totalFrames = 4;
  62.     int frames[totalFrames];
  63.  
  64.     int i;
  65.     for(i = 0; i < totalFrames; i++)
  66.     {
  67.         frames[i] = -1;
  68.     }
  69.  
  70.  
  71.     printf("Total Number of Page Faults = %d\n", algorithm(totalPages, pages, totalFrames, frames));
  72. }
Advertisement
Add Comment
Please, Sign In to add comment