-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptimalAlgorithm.cpp
78 lines (69 loc) · 1.93 KB
/
OptimalAlgorithm.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
//This code is contributed by Sourajita Dewasi
#include <iostream>
using namespace std;
int main()
{
int noframes=3, nopages=12, frames[12], temp[12];
int flag1, flag2, flag3, i, j, k, pos, max, page_faults = 0;
cout<<"\n Optimal Page Replacement Algorithm";
int pages[12]={5,1,2,3,5,1,4,5,1,2,3,4};
for(i=0; i<noframes; ++i){
frames[i] = -1;
}
for(i=0; i<nopages; ++i){
flag1 = flag2 = 0;
for(j=0; j<noframes; ++j){
if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0){
for(j = 0; j<noframes; ++j){
if(frames[j] == -1){
page_faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
flag3 =0;
for(j = 0; j < noframes; ++j){
temp[j] = -1;
for(k = i+1; k<nopages; ++k){
if(frames[j] == pages[k]){
temp[j] = k;
break; }
}
}
for(j=0; j<noframes; ++j){
if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}
if(flag3 ==0){
max = temp[0];
pos = 0;
for(j = 1; j < noframes; ++j){
if(temp[j] > max){
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
page_faults++;
}
cout<<"\n";
for(j = 0; j < noframes; ++j){
cout<<"\t\t"<<frames[j];
}
}
cout<<"\n Total Number of Page Faults "<<page_faults;
cout<<"\n Total number of Page Hits "<<nopages-page_faults;
return 0;
}