/
PFF_VSWS.java
175 lines (171 loc) · 5.85 KB
/
PFF_VSWS.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
public class Homework {
public static int pff(String input, int f){
int prev_pf = 0;
int cur_pf;
int count = -1;
int total_pf = 0;
LinkedHashMap<Integer, Integer> mem = new LinkedHashMap<>();
try {
FileReader f1 = new FileReader(input);
BufferedReader b1 = new BufferedReader(f1);
String line;
while((line = b1.readLine()) != null){
count += 1;
if(count != 0){
//continue;
if(mem.containsKey(Integer.parseInt(line)))
mem.put(Integer.parseInt(line), 1);
else{
cur_pf = count;
total_pf += 1;
if((cur_pf - prev_pf) > f){
Iterator<Map.Entry<Integer, Integer>> itr = mem.entrySet().iterator();
while(itr.hasNext()){
Map.Entry<Integer, Integer> tuple = itr.next();
if(tuple.getValue() == 0){
itr.remove();
}
else{
tuple.setValue(0);
}
}
mem.put(Integer.parseInt(line), 1);
prev_pf = cur_pf;
}
else{
mem.put(Integer.parseInt(line), 1);
prev_pf = cur_pf;
}
}
}
}
b1.close();
}
catch(Exception e) {
System.out.println(e);
}
return total_pf;
}
public static int vsws(String input, int M, int L, int Q) {
int total_pf = 0;
LinkedHashMap<Integer, Integer> mem = new LinkedHashMap<>();
try {
FileReader f2 = new FileReader(input);
BufferedReader b2 = new BufferedReader(f2);
String line;
int count = -1, pf_per_intvl = 0;
while ((line = b2.readLine()) != null) {
count++;
if (count != 0) {
if (mem.containsKey(Integer.parseInt(line))) {
mem.put(Integer.parseInt(line), 1);
}
else {
total_pf++;
if (pf_per_intvl > Q && count >= M) {
Iterator<Map.Entry<Integer, Integer>> itr1 = mem.entrySet().iterator();
while (itr1.hasNext()) {
Map.Entry<Integer, Integer> entry = itr1.next();
if (entry.getValue() == 0) {
itr1.remove();
}
else {
entry.setValue(0);
}
}
count = 1;
mem.put(Integer.parseInt(line), 1);
pf_per_intvl++;
}
else {
mem.put(Integer.parseInt(line), 1);
pf_per_intvl++;
}
}
if (count == L) {
Iterator<Map.Entry<Integer, Integer>> itr2 = mem.entrySet().iterator();
while (itr2.hasNext()) {
Map.Entry<Integer, Integer> entry = itr2.next();
if (entry.getValue() == 0) {
itr2.remove();
}
else {
entry.setValue(0);
}
count = 0;
pf_per_intvl = 0;
}
}
}
}
}
catch (Exception e) {
System.out.println(e);
}
return total_pf;
}
public static void pff_exe(String temp){
System.out.println("PFF Algorithm initiated......");
int prev_total_pff;
int cur_total_pff;
int f = 1;
do{
prev_total_pff = pff(temp, f);
System.out.println("For Frequency " + f +", number of Page Faults is/are: " + prev_total_pff);
cur_total_pff = pff(temp, f+1);
f += 1;
}while(prev_total_pff > cur_total_pff);
System.out.println("For Frequency " + f +", number of Page Faults is/are: " + cur_total_pff);
System.out.println("As Frequency increases, the number of Page Faults decreases and reaches a point where the number of PAGE FAULTS becomes CONSTANT");
System.out.println("We reached the CONSTANT point");
System.out.println("Therefore, optimal frequency using PFF Algorithm is " + (f-1) + " with " + prev_total_pff + " Page Faults" );
System.out.println("\nPFF Algorithm terminated......");
System.out.println("############################################################");
}
public static void vsws_exe(String temp){
try{
System.out.println("\nVSWS Algorithm initiated......");
int prev_total_pf;
int cur_total_pf;
int M = 2;
int L, Q;
String s;
FileReader f3 = new FileReader(temp);
BufferedReader b3 = new BufferedReader(f3);
int tl = Integer.parseInt(b3.readLine());
do{
Q = 1;
do{
L= M + Q;
do{
prev_total_pf = vsws(temp, M, L, Q);
System.out.println("For M, L & Q as " + M +", "+ L +", and "+ Q + " respectively, the number of Page Faults is/are: " + prev_total_pf);
cur_total_pf = vsws(temp, M, L+1, Q);
L += 1;
}while(prev_total_pf > cur_total_pf);
if(cur_total_pf == tl){
break;
}
Q++;
}while(Q <= tl);
M++;
}while(cur_total_pf != tl);
System.out.println("As Frequency increases, the number of Page Faults decreases and reaches a point where the number of PAGE FAULTS becomes CONSTANT");
System.out.println("We reached the CONSTANT point");
System.out.println("Therefore optimal values of M, L & Q using VSWS are " + (M-1) +", "+ (L-1) +", and "+ Q + " respectively, the number of Page Faults is/are: " + prev_total_pf);
System.out.println("\nVSWS Algorithm terminated......");
System.out.println("##############################################################");
b3.close();
}
catch (Exception e){
System.out.println(e);
}
}
public static void main(String [] args){
pff_exe(args[0]);
vsws_exe(args[0]);
}
}