BTCOL406                                                                              Operating Systems
Gramin Technical and Management campus Nanded
              Department of computer Engineering (Degree)
Name- Atharva Yashwantrao Bodhankar            PRN No: 24025081245505
                Subject - BTCOL406 _Operating Systems
      DOP – ___/___/ 2025                    DOC – ___/___/ 2025
                                        Practical No. 5
Aim - Implementation of various page replacement algorithms (FIFO,
Optimal, LRU).
   1. FIFO Page Replacement.
Code –
#include <stdio.h>
int main() {
   int frames, pages[50], temp[50], n, i, j, k, pageFaults = 0, position = 0, flag;
   printf("Enter number of pages: ");
   scanf("%d", &n);
   printf("Enter the page reference string:\n");
   for (i = 0; i < n; i++) {
      scanf("%d", &pages[i]);
   }
   printf("Enter number of frames: ");
   scanf("%d", &frames);
   for (i = 0; i < frames; i++) {
      temp[i] = -1; // Initialize frames as empty
   }
   printf("\nPage\tFrames\n");
   for (i = 0; i < n; i++) {
      flag = 0;
      // Check if page is already in frame
      for (j = 0; j < frames; j++) {
         if (temp[j] == pages[i]) {
             flag = 1;
             break;
         }
      }
      if (!flag) {
Practical N0. 4                                                                              Page 1 of 7
BTCOL406                                                                     Operating Systems
          temp[position] = pages[i];     // Replace the oldest page (FIFO)
          position = (position + 1) % frames;
          pageFaults++;
          // Print current frame status
          printf("%d\t", pages[i]);
          for (k = 0; k < frames; k++) {
             if (temp[k] != -1)
                printf("%d ", temp[k]);
             else
                printf("- ");
          }
          printf("\n");
      }
    }
    printf("\nTotal Page Faults = %d\n", pageFaults);
    return 0;
}
Output –
Practical N0. 4                                                                     Page 2 of 7
BTCOL406                                                       Operating Systems
    2. LRU Page Replacement
Code –
#include <stdio.h>
int main() {
   int frames, pages[50], temp[50], counter[50];
   int n, i, j, k, pageFaults = 0, pos, max, flag, time = 0;
   printf("Enter number of pages: ");
   scanf("%d", &n);
   printf("Enter the page reference string:\n");
   for (i = 0; i < n; i++) {
      scanf("%d", &pages[i]);
   }
   printf("Enter number of frames: ");
   scanf("%d", &frames);
   for (i = 0; i < frames; i++) {
      temp[i] = -1;
      counter[i] = 0;
   }
   printf("\nPage\tFrames\n");
   for (i = 0; i < n; i++) {
      flag = 0;
      for (j = 0; j < frames; j++) {
         if (temp[j] == pages[i]) {
             time++;
             counter[j] = time; // Update recent use
             flag = 1;
             break;
         }
      }
      if (!flag) {
         // Find empty slot
         for (j = 0; j < frames; j++) {
             if (temp[j] == -1) {
                time++;
                temp[j] = pages[i];
                counter[j] = time;
                pageFaults++;
                flag = 2;
                break;
             }
Practical N0. 4                                                       Page 3 of 7
BTCOL406                                                Operating Systems
        }
      }
      // If no empty slot, use LRU replacement
      if (flag == 0) {
          int min = counter[0];
          pos = 0;
          for (j = 1; j < frames; j++) {
             if (counter[j] < min) {
                min = counter[j];
                pos = j;
             }
          }
          time++;
          temp[pos] = pages[i];
          counter[pos] = time;
          pageFaults++;
      }
      if (flag != 1) {
          printf("%d\t", pages[i]);
          for (k = 0; k < frames; k++) {
             if (temp[k] != -1)
                printf("%d ", temp[k]);
             else
                printf("- ");
          }
          printf("\n");
      }
    }
    printf("\nTotal Page Faults = %d\n", pageFaults);
    return 0;
}
Practical N0. 4                                                Page 4 of 7
BTCOL406                                                               Operating Systems
Output –
    3. Optimal Page Replacement
Code –
#include <stdio.h>
int predict(int pages[], int temp[], int n, int index, int frames) {
   int res = -1, farthest = index;
   for (int i = 0; i < frames; i++) {
      int j;
      for (j = index; j < n; j++) {
          if (temp[i] == pages[j]) {
             if (j > farthest) {
                farthest = j;
                res = i;
             }
             break;
          }
      }
      // If page not found in future
      if (j == n)
          return i;
   }
   return (res == -1) ? 0 : res;
}
int main() {
   int frames, pages[50], temp[50];
   int n, i, j, pageFaults = 0, hit;
   printf("Enter number of pages: ");
Practical N0. 4                                                               Page 5 of 7
BTCOL406                                                       Operating Systems
  scanf("%d", &n);
  printf("Enter the page reference string:\n");
  for (i = 0; i < n; i++) {
     scanf("%d", &pages[i]);
  }
  printf("Enter number of frames: ");
  scanf("%d", &frames);
  for (i = 0; i < frames; i++)
     temp[i] = -1;
  printf("\nPage\tFrames\n");
  for (i = 0; i < n; i++) {
     hit = 0;
     // Page hit check
     for (j = 0; j < frames; j++) {
        if (temp[j] == pages[i]) {
           hit = 1;
           break;
        }
     }
     if (!hit) {
        int empty = -1;
        // Check for empty frame
        for (j = 0; j < frames; j++) {
           if (temp[j] == -1) {
              empty = j;
              break;
           }
        }
        if (empty != -1) {
           temp[empty] = pages[i];
        } else {
           int pos = predict(pages, temp, n, i + 1, frames);
           temp[pos] = pages[i];
        }
        pageFaults++;
        printf("%d\t", pages[i]);
        for (j = 0; j < frames; j++) {
           if (temp[j] != -1)
              printf("%d ", temp[j]);
           else
Practical N0. 4                                                       Page 6 of 7
BTCOL406                                                Operating Systems
               printf("- ");
          }
          printf("\n");
      }
    }
    printf("\nTotal Page Faults = %d\n", pageFaults);
    return 0;
}
Output –
Practical N0. 4                                                Page 7 of 7