-
Notifications
You must be signed in to change notification settings - Fork 1
/
MFU.cpp
92 lines (75 loc) · 2.58 KB
/
MFU.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
#include <iostream>
using namespace std;
#define FRAMES_NUMBER 3
int MFU(int pages[], int size)
{
// 0) Initialize Frames With '-1'
int frames[FRAMES_NUMBER];
for (int i = 0; i < FRAMES_NUMBER; i++)
frames[i] = -1;
int paeFaults = 0; // Count The Page Faults
// Go Through All Pages
for (int pageIndex = 0; pageIndex < size; pageIndex++)
{
// 1) Find Page In The Frames
bool isFound = false;
for (int i = 0; i < FRAMES_NUMBER; i++)
if (frames[i] == pages[pageIndex])
{
isFound = true;
// Printing
cout << pages[pageIndex] << endl;
break;
}
// If Not Found
if (!isFound)
{
// 2) Find A Free Frame
bool hasFreeFrame = false;
for (int i = 0; i < FRAMES_NUMBER; i++)
if (frames[i] == -1)
{
hasFreeFrame = true;
frames[i] = pages[pageIndex];
paeFaults++;
// Printing
cout << pages[pageIndex] << "\t\t";
for (int f = 0; f < FRAMES_NUMBER; f++)
cout << frames[f] << "\t";
cout << endl;
break;
}
// 3) Page Replacement (Not Found & No Free Frame)
if (!hasFreeFrame)
{
// Array To Store The Used Count For Each Page In The Frames
int countUse[FRAMES_NUMBER] = {0};
// Assign Count For Each Page In The Frames
for (int i = 0; i < FRAMES_NUMBER; i++)
for (int p = pageIndex; p >= 0; p--)
if (pages[p] == frames[i])
countUse[i]++;
// Find The Victim Frame (With The Highest Count)
int victim = 0;
for (int i = 0; i < FRAMES_NUMBER; i++)
if (countUse[i] > countUse[victim])
victim = i;
frames[victim] = pages[pageIndex];
paeFaults++;
// Printing
cout << pages[pageIndex] << "\t\t";
for (int f = 0; f < FRAMES_NUMBER; f++)
cout << frames[f] << "\t";
cout << endl;
}
}
}
return paeFaults;
}
int main(int argc, char const *argv[])
{
int pages[] = {1, 2, 3, 1, 4, 5, 2, 1, 2, 6, 7, 3, 2};
cout << "Number Of Page Faults = " << MFU(pages, 13);
getchar();
return 0;
}