Sreekar_0125

LRU Page Replacement Algorithm

Aug 14th, 2023
1,246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. // Least Recently Used (LRU) Page Replacement Algorithm
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4.  
  5. // Function which runs the algorithm and in the end, returns no. of page faults
  6.  
  7. int LRU_algorithm(int totalPages, int pages[], int totalFrames, int frames[])
  8. {
  9.     int i, j, k;
  10.     int pageFaults = 0;
  11.  
  12.     for (i = 0; i < totalPages; i++)
  13.     {
  14.         int currentPage = pages[i];
  15.  
  16.         // Check if Page Already Exists
  17.         bool found = false;
  18.         for (j = 0; j < totalFrames; j++)
  19.         {
  20.             if (frames[j] == currentPage)
  21.             {
  22.                 found = true;
  23.                 break;
  24.             }
  25.         }
  26.         // If Page Already Exists, Page Hit
  27.         if (found == true)
  28.         {
  29.             for (j = 0; j < totalFrames; j++)
  30.             {
  31.                 printf("%d \t", frames[j]);
  32.             }
  33.             printf("Page Hit !\n");
  34.         }
  35.         // If Not, Already Frame lo unnavati lo max distance to the left of current page em ochindo adhi replace aitadi
  36.         else
  37.         {
  38.             int maxDistance = -1;
  39.             int indexToBeReplaced;
  40.            
  41.             // calculate that distance for every frame
  42.             for(j = 0; j < totalFrames; j++)
  43.             {
  44.                 int distance = 0;
  45.                 for(k = i - 1; k >= 0; k--)
  46.                 {
  47.                     if(frames[j] != pages[k])
  48.                     {
  49.                         distance++;
  50.                     }
  51.                     else
  52.                     {
  53.                         break;
  54.                     }
  55.                 }
  56.                 if(distance > maxDistance)
  57.                 {
  58.                     maxDistance = distance;
  59.                     indexToBeReplaced = j;
  60.                 }
  61.             }
  62.  
  63.             frames[indexToBeReplaced] = pages[i];
  64.  
  65.             // Increment Page Faults
  66.             pageFaults++;
  67.             for (j = 0; j < totalFrames; j++)
  68.             {
  69.                 printf("%d \t", frames[j]);
  70.             }
  71.             printf("Page Fault !\n");
  72.         }
  73.     }
  74.     return pageFaults;
  75. }
  76.  
  77. int main()
  78. {
  79.     int totalPages = 19;
  80.     int pages[] = {3, 2, 1, 3, 4, 1, 6, 2, 4, 3, 4, 2, 1, 4, 5, 2, 1, 3, 4};
  81.  
  82.     int totalFrames = 3;
  83.     int frames[totalFrames];
  84.  
  85.     int i;
  86.     for (i = 0; i < totalFrames; i++)
  87.     {
  88.         frames[i] = -1;
  89.     }
  90.  
  91.     printf("Total Number of Page Faults = %d\n", LRU_algorithm(totalPages, pages, totalFrames, frames));
  92. }
Advertisement
Add Comment
Please, Sign In to add comment