-
Notifications
You must be signed in to change notification settings - Fork 3
/
OPR.cpp
102 lines (89 loc) · 2.12 KB
/
OPR.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
#include <iostream>
using namespace std;
int optimizeFrame(int a[], int startIndex, int pages, int frames, int check[])
{
int max = -1, cur, index;
for (int j = 0; j < frames; j++)
{
cur = a[j];
for (int k = startIndex; k < pages; k++)
{
if (cur == a[k])
{
check[j] = k;
break;
}
}
}
for (int j = 0; j < frames; j++)
{
if (check[j] == -1)
return j;
if (check[j] > max)
{
max = check[j];
index = j;
}
}
return index;
}
int checkPageHit(int frame[], int d, int frames)
{
for (int k = 0; k < frames; k++)
{
if (frame[k] == d)
return 1;
}
return 0;
}
int main()
{
int frames, pages, t, i, temp;
float pageHits = 0, pageFaults;
cout << "Number of Pages : ";
cin >> pages;
cout << "Number of Frames : ";
cin >> frames;
int *a = new int[pages];
int *frame = new int[frames];
int *check = new int[frames];
for (t = 0; t < pages; t++)
cin >> a[t];
for (t = 0; t < frames; t++)
{
check[t] = -1;
}
static int count = 0;
int replaceIndex;
for (t = 0; t < pages; t++)
{
if (t < frames)
{
frame[t] = a[t];
continue;
}
else
{
if (count > frames)
count = 0;
temp = a[t];
if (checkPageHit(frame, temp, frames))
{
pageHits++;
continue;
}
else
{
replaceIndex = optimizeFrame(a, t, pages, frames, check);
frame[replaceIndex] = a[t];
count++;
continue;
}
}
}
cout << pageHits;
cout << "Page Hits : " << pageHits << endl;
cout << "Page Faults : " << (pages - pageHits) << endl;
cout << "Hit Ratio : " << float(float(pageHits / pages)) * 100 << "%" << endl;
cout << "Miss Ratio : " << float(float(float(pages - pageHits) / pages) * 100) << "%" << endl;
}