-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOptimalPageReplacement.java
69 lines (65 loc) · 2.27 KB
/
OptimalPageReplacement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.*;
public class OptimalPageReplacement{
// initialise seq and frameSize only, run main.
static int[] seq = {7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
static int[] frame;
static int frameSize = 3;
static int pageFault = 0;
static String pattern = "";
public static void main(String[] args){
System.out.println("OptimalPageReplacement : ");
System.out.println(Integer.MIN_VALUE+" indicates empty memory array !");
frame = new int[frameSize];
for(int i = 0; i < frameSize; i++){
frame[i] = Integer.MIN_VALUE;
}
int index = 0;
System.out.println("Initial Frame : "+Arrays.toString(frame));
while(index < seq.length){
int page = seq[index];
if(!inFrame(page)){
int findex = indexToRemove(index);
pageFault++;
pattern += "Y";
frame[findex] = page;
System.out.println("Frame : "+Arrays.toString(frame));
}
else{
pattern += "N";
}
index++;
}
System.out.println(System.lineSeparator()+"Final Frame : "+Arrays.toString(frame));
System.out.println("no of page faults = "+pageFault);
System.out.println("pattern : "+pattern);
}
public static int indexToRemove(int currentIndex){
int[] temp = new int[frame.length];
for(int i = 0; i < frame.length; i++){ temp[i] = frame[i];}
for(int i = 0; i < temp.length; i++){
boolean isAlreadySet = false;
for(int j = currentIndex+1; j < seq.length; j++){
if(temp[i] == seq[j]){
temp[i] = j;
isAlreadySet = true;
break;
}
}
if(!isAlreadySet){temp[i] = seq.length;}
}
return findIndexOfFurther(temp);
}
public static int findIndexOfFurther(int[] arr){
int ret = 0;
for(int i = 0; i < arr.length; i++){
if( arr[i] > arr[ret] ){ ret = i;}
}
return ret;
}
public static boolean inFrame(int page){
for(int each : frame){
if(each == page){return true;}
}
return false;
}
}