sandeshMC

LRU REPLACEMENT ALGORITHM

Apr 12th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class LeastRecentlyUsedAlgorithm {
  5.  
  6.     public static void main(String[] args) {
  7.         // TODO Auto-generated method stub
  8.  
  9.         Scanner sc = new Scanner(System.in);
  10.         int k = 0;
  11.         System.out.println("enter no. of frames: ");
  12.         int f = sc.nextInt();
  13.         int frame1[] = new int[f];
  14.         int a[] = new int[f];
  15.         int b[] = new int[f];
  16.         for (int i = 0; i < f; i++) {
  17.             frame1[i] = -1;
  18.             a[i] = -1;
  19.             b[i] = -1;
  20.         }
  21.         System.out.println("enter the no of pages ");
  22.         int n = sc.nextInt();
  23.         int[] pages = new int[n];
  24.         System.out.println("enter the page no ");
  25.         for (int j = 0; j < n; j++)
  26.             pages[j] = sc.nextInt();
  27.         int pgf = 0;
  28.         int chn = 0;
  29.  
  30.         int pg = 0;
  31.         System.out.println("Accessed Page\tframes");
  32.         for (pg = 0; pg < n; pg++) {
  33.             int page = pages[pg];
  34.             boolean flag = true;
  35.  
  36.             for (int j = 0; j < f; j++) {
  37.                 if (page == frame1[j]) {
  38.                     flag = false;
  39.                     break;
  40.                 }
  41.             }
  42.  
  43.             for (int j = 0; j < f && flag; j++) {
  44.                 if (frame1[j] == a[f - 1]) {
  45.                     k = j;
  46.                     break;
  47.                 }
  48.             }
  49.  
  50.             if (flag) {
  51.                 frame1[k] = page;
  52.  
  53.                 System.out.println("\t" + page + "\t" + Arrays.toString(frame1)
  54.                         + "   Fault");
  55.                 pgf++;
  56.  
  57.             } else {
  58.  
  59.                 System.out
  60.                         .println("\t" + page + "\t" + Arrays.toString(frame1));
  61.  
  62.             }
  63.             int p = 1;
  64.             b[0] = page;
  65.             for (int j = 0; j < a.length; j++) {
  66.                 if (page != a[j] && p < f) {
  67.                     b[p] = a[j];
  68.                     p++;
  69.                 }
  70.             }
  71.             for (int j = 0; j < f; j++) {
  72.                 a[j] = b[j];
  73.             }
  74.  
  75.         }
  76.  
  77.         System.out.println("Page fault:" + pgf);
  78.     }
  79.  
  80. }
  81.  
  82. /*enter no. of frames:
  83. 3
  84. enter the no of pages
  85. 20
  86. enter the page no
  87. 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
  88. Accessed Page   frames
  89.     7   [7, -1, -1]   Fault
  90.     0   [7, 0, -1]   Fault
  91.     1   [7, 0, 1]   Fault
  92.     2   [2, 0, 1]   Fault
  93.     0   [2, 0, 1]
  94.     3   [2, 0, 3]   Fault
  95.     0   [2, 0, 3]
  96.     4   [4, 0, 3]   Fault
  97.     2   [4, 0, 2]   Fault
  98.     3   [4, 3, 2]   Fault
  99.     0   [0, 3, 2]   Fault
  100.     3   [0, 3, 2]
  101.     2   [0, 3, 2]
  102.     1   [1, 3, 2]   Fault
  103.     2   [1, 3, 2]
  104.     0   [1, 0, 2]   Fault
  105.     1   [1, 0, 2]
  106.     7   [1, 0, 7]   Fault
  107.     0   [1, 0, 7]
  108.     1   [1, 0, 7]
  109.  
  110. */
Advertisement
Add Comment
Please, Sign In to add comment