-
Notifications
You must be signed in to change notification settings - Fork 0
/
Page Replacemnet Technique.cpp
293 lines (248 loc) · 8.07 KB
/
Page Replacemnet Technique.cpp
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
#include<bits/stdc++.h>
using namespace std;
vector<int>reference_string;
void showSimulation(int arr[],int no_frames,int value){
cout << "Adding "<<value << ": ";
for(int i=0;i <no_frames; i++){
cout << arr[i] << " ";
}
cout << endl;
}
bool search_Value(int arr[],int no_frames,int value)
{
for(int i=0; i<no_frames; i++){
if(arr[i]==value){
return true;
}
}
return false;
}
int find_max(int arr[],int Size){
int maximum = -1,index=-1;
for(int i=0; i<Size; i++){
if(arr[i]==-1){ ///checking value which is not available in the remaining reference array
return i;
}
if(arr[i] > maximum){
maximum = arr[i];
index = i;
}
}
return (index==-1)? 0:index;
}
int find_min(int arr[],int Size){
int minimum = INT_MAX,index=-1;
for(int i=0; i<Size; i++){
if(arr[i]==-1){ ///checking value which is not available in the remaining reference array
return i;
}
if(arr[i] < minimum){
minimum = arr[i];
index = i;
}
}
return (index==-1)? 0:index;
}
void FIFO(int no_frames,int no_references){
int frames[no_frames];
for(int i=0; i<no_frames; i++){
frames[i] = -1;
}
int page_faults=0;
queue<int>q; ///for storing index
cout << "________FIFO_______" << endl;
for(int i=0,frame=0; i<no_references; i++){
if(i<no_frames){
if(search_Value(frames,no_frames,reference_string[i])){ ///if found in the frame
cout << "found : "<<reference_string[i] <<endl;
continue;
}else{
frames[frame] = reference_string[i];
q.push(frame);
frame++;
page_faults++;
showSimulation(frames,no_frames,reference_string[i]);
}
}else{
if(search_Value(frames,no_frames,reference_string[i])){ ///if found in the frame
cout << "found : "<<reference_string[i] <<endl;
continue;
}else{
int f = q.front();
q.pop();
q.push(f);
frames[f] = reference_string[i];
page_faults++;
showSimulation(frames,no_frames,reference_string[i]);
}
}
}
cout << "\nPage Faults : "<< page_faults << endl;
cout << "Page Faults Rate : "<< (page_faults/(float)no_references)*100 << "%" << endl;
}
void Optimal(int no_frames,int no_references){
int frames[no_frames];
for(int i=0; i<no_frames; i++){
frames[i] = -1;
}
int page_faults=0;
cout << "\n_______Optimal______"<<endl;
for(int i=0,frame=0; i<no_references; i++){
if(i<no_frames){
if(search_Value(frames,no_frames,reference_string[i])){ ///if found in the frame
cout << "found : "<<reference_string[i] <<endl;
continue;
}else{
frames[frame] = reference_string[i];
frame++;
page_faults++;
showSimulation(frames,no_frames,reference_string[i]);
}
}else{
if(search_Value(frames,no_frames,reference_string[i])){ ///if found in the frame
cout << "found : "<<reference_string[i] <<endl;
continue;
}else{
int isFound[no_frames];
for(int in=0; in<no_frames; in++)
isFound[in] = -1;
for(int j=0; j<no_frames; j++){ ///loop of page frame array
for(int k=i+1; k<no_references; k++){ ///loop of remaining references string
if(frames[j] == reference_string[k] && isFound[j] ==-1){
isFound[j] = k;
}
}
}
int index = find_max(isFound,no_frames);
frames[index] = reference_string[i];
page_faults++;
showSimulation(frames,no_frames,reference_string[i]);
}
}
}
cout << "\nPage Faults : "<< page_faults << endl;
cout << "Page Faults Rate : "<< (page_faults/(float)no_references)*100 << "%" << endl;
}
void LRU(int no_frames,int no_references)
{
int frames[no_frames];
for(int i=0; i<no_frames; i++){
frames[i] = -1;
}
int page_faults=0;
cout << "\n_______LRU______"<<endl;
for(int i=0,frame=0; i<no_references; i++){
if(i<no_frames){
if(search_Value(frames,no_frames,reference_string[i])){ ///if found in the frame
cout << "found : "<<reference_string[i] <<endl;
continue;
}else{
frames[frame] = reference_string[i];
frame++;
page_faults++;
showSimulation(frames,no_frames,reference_string[i]);
}
}else{
if(search_Value(frames,no_frames,reference_string[i])){ ///if found in the frame
cout << "found : "<<reference_string[i] <<endl;
continue;
}else{
int isFound[no_frames];
for(int in=0; in<no_frames; in++)
isFound[in] = -1;
for(int j=0; j<no_frames; j++){ ///loop of page frame array
for(int k=i-1; k>=0; k--){ ///loop of remaining references string
if(frames[j] == reference_string[k] && isFound[j] ==-1){
isFound[j] = k;
}
}
}
int index = find_min(isFound,no_frames);
frames[index] = reference_string[i];
page_faults++;
showSimulation(frames,no_frames,reference_string[i]);
}
}
}
cout << "\nPage Faults : "<< page_faults << endl;
cout << "Page Faults Rate : "<< (page_faults/(float)no_references)*100 << "%" << endl;
}
int main(){
int no_of_pages,no_of_page_frame,no_of_reference;
cout << "Enter the number of pages : ";
cin >> no_of_pages;
cout << "Enter the number of page references : ";
cin >> no_of_reference;
cout << "Reference String : ";
for(int i=0; i<no_of_reference; i++){
int value;
cin >> value;
reference_string.push_back(value);
}
cout << "Enter number of page frames : ";
cin >> no_of_page_frame;
while(true){
cout<<"\n\n_________Page Replacement Algorithm_________"<<endl;
cout << "1. FIFO\n2. Optimal\n3. LRU" << endl;
cout <<"\nEnter Your Response : ";
int response;
cin >> response;
switch(response){
case 1: FIFO(no_of_page_frame,no_of_reference);
break;
case 2: Optimal(no_of_page_frame,no_of_reference);
break;
case 3: LRU(no_of_page_frame,no_of_reference);
break;
default: return 0;
}
}
return 0;
}
/**
Input 0:
8
22
7 0 1 2 0 3 0 4 2 3 0 3 0 3 2 1 2 0 1 7 0 1
3
Output 0:
________FIFO_______
Page Faults : 15
Page Faults Rate : 68.1818%
_______Optimal______
Page Faults : 9
Page Faults Rate : 40.9091%
_______LRU______
Page Faults : 12
Page Faults Rate : 54.5455%
Input 1:
8
15
7 0 1 2 0 3 0 4 2 3 0 3 1 2 0
3
OutPut 1:
________FIFO_______
Page Faults : 12
Page Faults Rate : 80%
_______Optimal______
Page Faults : 8
Page Faults Rate : 53.3333%
_______LRU______
Page Faults : 12
Page Faults Rate : 80%
Input 2:
8
20
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
3
Output 2:
________FIFO_______
Page Faults : 15
Page Faults Rate : 75%
_______Optimal______
Page Faults : 9
Page Faults Rate : 45%
_______LRU______
Page Faults : 12
Page Faults Rate : 60%
*/