-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPageReplacement.java
355 lines (348 loc) · 9.16 KB
/
PageReplacement.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package osgla2;
/*
* NAME - MAYURI GUPTA
* ROLL - 1710110209
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class PageReplacement implements ActionListener {
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
JFrame f = new JFrame();
JPanel p1=new JPanel(); // main panel which is added to frame
JPanel p2=new JPanel(); // panel of grid 1
JPanel p3=new JPanel(); // panel of grid 2
JPanel p4=new JPanel(); // panel of grid 3
JPanel p5=new JPanel(); // panel of grid 4
JPanel p6=new JPanel(); // panel of grid 5
JPanel p7=new JPanel(); // panel of grid 6
JPanel p8=new JPanel(); // panel of grid 7
JPanel p9=new JPanel(); // panel of grid 8
JPanel p10=new JPanel(); // panel of grid 9
JLabel l1=new JLabel("Number Of Frames");
JLabel l2=new JLabel("Reference String");
JLabel l3=new JLabel("FIRST IN FIRST OUT PAGE REPLACEMENT ALGORITHM");
JLabel l4=new JLabel("OPTIMAL PAGE REPLACEMENT ALGORITHM");
JLabel l5=new JLabel("LEAST RECENTLY USED REPLACEMENT ALGORITHM");
JButton b1=new JButton("COMPUTE");
JTextField t1=new JTextField("");
String [] frm={"3","4","5","6","7"};
JComboBox box=new JComboBox(frm);
PageReplacement(){
f.setLayout(new BorderLayout());
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Font font=new Font("Arial",Font.BOLD,15);
p1.setBorder(BorderFactory.createLineBorder(Color.red,3));
p1.setLayout(new GridLayout(9,1));
p2.setLayout(null);
p3.setLayout(null);
p4.setLayout(null);
p5.setLayout(null);
p7.setLayout(null);
p9.setLayout(null);
l1.setBounds(500, 0, 300, 100);
l2.setBounds(50, 0,300,100);
l3.setBounds(50,0,500,100);
l4.setBounds(50,0,500,100);
l5.setBounds(50,0,500,100);
l1.setFont(font);
l2.setFont(font);
l3.setFont(font);
l4.setFont(font);
l5.setFont(font);
l3.setForeground(Color.BLUE);
l4.setForeground(Color.BLUE);
l5.setForeground(Color.BLUE);
box.setBounds(650,33,100,30);
t1.setBounds(200,34,1100,30);
b1.setBounds(580,30,150,50);
b1.addActionListener(this);
p2.add(l1);
p2.add(box);
p3.add(l2);
p3.add(t1);
p4.add(b1);
p1.add(p2);
p1.add(p3);
p1.add(p4);
JScrollPane scrollPane = new JScrollPane(p1);
f.getContentPane().add(scrollPane);
f.pack();
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setVisible(true);
}
public static void main(String args[]){
new PageReplacement();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
p5.removeAll();
p6.removeAll();
p7.removeAll();
p8.removeAll();
p9.removeAll();
p10.removeAll();
int counter=0;
String text=t1.getText();
String [] af=text.split("[,.]"); // array from textfield
int [] array=new int[af.length]; // integer array of reference string
for(int i=0;i<af.length;i++)
array[i]=Integer.parseInt(af[i]);
p5.add(l3);
p7.add(l4);
p9.add(l5);
int loc=Integer.parseInt(box.getSelectedIndex()+"");
int frames=Integer.parseInt(frm[loc]);
int n=frames+2;
int m=array.length+1;
p6.setLayout(new GridLayout(n,m));
p8.setLayout(new GridLayout(n,m));
p10.setLayout(new GridLayout(n,m));
String [][] ans=new String[n][m];
// FIFO:
for(int j=1;j<m;j++)
ans[0][j]=af[j-1];
for(int i=1;i<n-1;i++)
ans[i][0]="f"+i+"";
for(int i=1;i<n;i++)
for(int j=1;j<m;j++)
ans[i][j]="NULL";
Queue<String> queue=new LinkedList<String>();
for(int j=1;j<m;j++){
String check="";
int siz=queue.size();
if(queue.contains(af[j-1])){
ans[n-1][j]="HIT";
continue;
}
if(siz>=frames)
check=queue.remove();
for(int i=1;i<n;i++){
if(check.equals("2"));
if(siz<frames)
{
if(ans[i][j].equalsIgnoreCase("NULL"))
{queue.add(af[j-1]);
ans[n-1][j]="FAULT";
counter+=1;
int k=j;
while(k<m)
ans[i][k++]=af[j-1];
break;}
}
else{
if(check.equalsIgnoreCase(ans[i][j])){
queue.add(af[j-1]);
ans[n-1][j]="FAULT";
counter+=1;
int k=j;
while(k<m)
ans[i][k++]=af[j-1];
break;
}
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
JLabel b=new JLabel(ans[i][j]);
Border border = BorderFactory.createLineBorder(Color.BLACK, 1);
b.setOpaque(true);
b.setBackground(Color.WHITE);
b.setBorder(border);
b.setHorizontalAlignment(SwingConstants.CENTER);
p6.add(b);
}
}
JLabel pg1=new JLabel("NUMBER OF PAGE FAULTS: "+counter);
JLabel pg2=new JLabel("NUMBER OF PAGE HITS: "+(af.length-counter));
pg1.setBounds(800,0,500,100);
pg2.setBounds(1050,0,500,100);
Font font=new Font("Arial",Font.BOLD,15);
pg1.setFont(font);
pg2.setFont(font);
p5.add(pg1);
p5.add(pg2);
counter=0;
// OPTIMAL:
for(int j=1;j<m;j++)
ans[0][j]=af[j-1];
for(int i=1;i<n-1;i++)
ans[i][0]="f"+i+"";
for(int i=1;i<n;i++)
for(int j=1;j<m;j++)
ans[i][j]="NULL";
ArrayList<String> a=new ArrayList<String>();
for(int j=1;j<m;j++){
int time=0,test=0;
TreeMap<Integer,String> at=new TreeMap();
String check="";
if(a.contains(af[j-1])){
ans[n-1][j]="HIT";
continue;
}
if(a.size()>=frames){
for(int k=0;k<a.size();k++){
int flag=0;
for(int l=j+1;l<m;l++){
if(a.get(k).equalsIgnoreCase(af[l-1])){
flag=1;
at.put(l+1,a.get(k));
time+=1;
break;
}
}
if(flag==0)
at.put((time+1)*100,a.get(k));
}
test=at.lastKey();
check=at.get(test);
}
for(int i=1;i<n;i++){
if((a.size())<frames)
{
if(ans[i][j].equalsIgnoreCase("NULL"))
{
a.add(af[j-1]);
ans[n-1][j]="FAULT";
counter+=1;
int k=j;
while(k<m)
ans[i][k++]=af[j-1];
break;
}
}
else{
if(check.equalsIgnoreCase(ans[i][j])){
a.add(af[j-1]);
for(int k=0;k<a.size();k++)
{
if(a.get(k).equalsIgnoreCase(check))
a.remove(k);
}
ans[n-1][j]="FAULT";
counter+=1;
int k=j;
while(k<m)
ans[i][k++]=af[j-1];
break;
}
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
JLabel b=new JLabel(ans[i][j]);
Border border = BorderFactory.createLineBorder(Color.BLACK, 1);
b.setOpaque(true);
b.setBackground(Color.WHITE);
b.setBorder(border);
b.setHorizontalAlignment(SwingConstants.CENTER);
p8.add(b);
}
}
JLabel pg3=new JLabel("NUMBER OF PAGE FAULTS: "+counter);
JLabel pg4=new JLabel("NUMBER OF PAGE HITS: "+(af.length-counter));
pg3.setBounds(800,0,500,100);
pg4.setBounds(1050,0,500,100);
pg3.setFont(font);
pg4.setFont(font);
p7.add(pg3);
p7.add(pg4);
counter=0;
// LRU :
for(int j=1;j<m;j++)
ans[0][j]=af[j-1];
for(int i=1;i<n-1;i++)
ans[i][0]="f"+i+"";
for(int i=1;i<n;i++)
for(int j=1;j<m;j++)
ans[i][j]="NULL";
TreeMap<Integer,String> tree=new TreeMap();
int time=0;
for(int j=1;j<m;j++){
String check="";
Integer check_key=0;
for(Map.Entry<Integer,String>entry : tree.entrySet()){
check=entry.getValue();
check_key=entry.getKey();
break;
}
for(int i=1;i<n;i++){
if(tree.containsValue(af[j-1]))
{
ans[n-1][j]="HIT";
for(Map.Entry<Integer,String>entry : tree.entrySet()){
if(entry.getValue().equalsIgnoreCase(af[j-1]))
{
tree.remove(entry.getKey());
break;
}
}
tree.put(time,af[j-1]);
time+=1;
continue;
}
if(tree.size()<frames){
if(ans[i][j].equalsIgnoreCase("NULL"))
{
ans[n-1][j]="FAULT";
counter+=1;
tree.put(time,af[j-1]);
time+=1;
int k=j;
while(k<m)
ans[i][k++]=af[j-1];
break;}
}
else{;
if(check.equalsIgnoreCase(ans[i][j])){
tree.put(time,af[j-1]);
time+=1;
tree.remove(check_key);
ans[n-1][j]="FAULT";
counter+=1;
int k=j;
while(k<m)
ans[i][k++]=af[j-1];
break;
}
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
JLabel b=new JLabel(ans[i][j]);
Border border = BorderFactory.createLineBorder(Color.BLACK, 1);
b.setOpaque(true);
b.setBackground(Color.WHITE);
b.setBorder(border);
b.setHorizontalAlignment(SwingConstants.CENTER);
p10.add(b);
}
}
JLabel pg5=new JLabel("NUMBER OF PAGE FAULTS: "+counter);
JLabel pg6=new JLabel("NUMBER OF PAGE HITS: "+(af.length-counter));
pg5.setBounds(800,0,500,100);
pg6.setBounds(1050,0,500,100);
pg5.setFont(font);
pg6.setFont(font);
p9.add(pg5);
p9.add(pg6);
counter=0;
p1.add(p5);
p1.add(p6);
p1.add(p7);
p1.add(p8);
p1.add(p9);
p1.add(p10);
f.revalidate();
f.repaint();
}
}