Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class LeastRecentlyUsedAlgorithm {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner sc = new Scanner(System.in);
- int k = 0;
- System.out.println("enter no. of frames: ");
- int f = sc.nextInt();
- int frame1[] = new int[f];
- int a[] = new int[f];
- int b[] = new int[f];
- for (int i = 0; i < f; i++) {
- frame1[i] = -1;
- a[i] = -1;
- b[i] = -1;
- }
- System.out.println("enter the no of pages ");
- int n = sc.nextInt();
- int[] pages = new int[n];
- System.out.println("enter the page no ");
- for (int j = 0; j < n; j++)
- pages[j] = sc.nextInt();
- int pgf = 0;
- int chn = 0;
- int pg = 0;
- System.out.println("Accessed Page\tframes");
- for (pg = 0; pg < n; pg++) {
- int page = pages[pg];
- boolean flag = true;
- for (int j = 0; j < f; j++) {
- if (page == frame1[j]) {
- flag = false;
- break;
- }
- }
- for (int j = 0; j < f && flag; j++) {
- if (frame1[j] == a[f - 1]) {
- k = j;
- break;
- }
- }
- if (flag) {
- frame1[k] = page;
- System.out.println("\t" + page + "\t" + Arrays.toString(frame1)
- + " Fault");
- pgf++;
- } else {
- System.out
- .println("\t" + page + "\t" + Arrays.toString(frame1));
- }
- int p = 1;
- b[0] = page;
- for (int j = 0; j < a.length; j++) {
- if (page != a[j] && p < f) {
- b[p] = a[j];
- p++;
- }
- }
- for (int j = 0; j < f; j++) {
- a[j] = b[j];
- }
- }
- System.out.println("Page fault:" + pgf);
- }
- }
- /*enter no. of frames:
- 3
- enter the no of pages
- 20
- enter the page no
- 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
- Accessed Page frames
- 7 [7, -1, -1] Fault
- 0 [7, 0, -1] Fault
- 1 [7, 0, 1] Fault
- 2 [2, 0, 1] Fault
- 0 [2, 0, 1]
- 3 [2, 0, 3] Fault
- 0 [2, 0, 3]
- 4 [4, 0, 3] Fault
- 2 [4, 0, 2] Fault
- 3 [4, 3, 2] Fault
- 0 [0, 3, 2] Fault
- 3 [0, 3, 2]
- 2 [0, 3, 2]
- 1 [1, 3, 2] Fault
- 2 [1, 3, 2]
- 0 [1, 0, 2] Fault
- 1 [1, 0, 2]
- 7 [1, 0, 7] Fault
- 0 [1, 0, 7]
- 1 [1, 0, 7]
- */
Advertisement
Add Comment
Please, Sign In to add comment