-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifoPageReplacement.cpp
More file actions
158 lines (126 loc) · 3.85 KB
/
fifoPageReplacement.cpp
File metadata and controls
158 lines (126 loc) · 3.85 KB
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
#include <iostream>
#include <iomanip>
using namespace std;
class Node {
public:
int pageNumber;
Node* next;
Node(int page) {
pageNumber = page;
next = nullptr;
}
};
class FIFOPageReplacement {
private:
int capacity, size, pageFaults, pageSize;
Node* front, *rear;
public:
FIFOPageReplacement(int capacity, int pageSize) {
this->capacity = capacity;
this->size = 0;
this->pageFaults = 0;
this->pageSize = pageSize;
front = NULL;
rear = nullptr;
}
bool isPagePresent(int pageNumber) {
Node* temp = front;
while (temp) {
if (temp->pageNumber == pageNumber) return true;
temp = temp->next;
}
return false;
}
void removeOldestPage() {
if (!front) return;
Node* temp = front;
front = front->next;
delete temp;
size--;
}
void insertPage(int pageNumber) {
if (isPagePresent(pageNumber)) {
cout << "Page " << pageNumber << " already in memory.\n";
return;
}
if (size == capacity) {
cout << "Removing oldest page " << front->pageNumber << endl;
removeOldestPage();
}
Node* newNode = new Node(pageNumber);
if (!rear) {
front = newNode;
rear = newNode;
}
else {
rear->next = newNode;
rear = newNode;
}
size++;
pageFaults++;
cout << "Inserted Page " << pageNumber << "\n";
}
void displayPages() {
Node* temp = front;
cout << "\nCurrent Memory:\n";
cout << "+";
for (int i = 0; i < capacity; i++) cout << "------";
cout << "+\n|";
int count = 0;
while (temp) {
cout << " " << setw(3) << temp->pageNumber << " |";
temp = temp->next;
count++;
}
while (count < capacity) {
cout << " |";
count++;
}
cout << "\n+";
for (int i = 0; i < capacity; i++) cout << "------";
cout << "+\n";
}
int getPageFaultCount() {
return pageFaults;
}
int getPageHitsCount() {
return pageSize - pageFaults;
}
double hitRate() {
return (getPageHitsCount()/(double)pageSize)*100.0;
}
double faultRate() {
return (getPageFaultCount()/(double)pageSize)*100.0;
}
};
int main() {
cout << "\nEnter the memory size: ";
int memory;
cin >> memory;
int pageSize;
cout << "Enter the number of pages: ";
cin >> pageSize;
FIFOPageReplacement fifo(memory, pageSize);
int pages[pageSize];
cout << "Enter the pages separated by space: ";
for(int i=0; i<pageSize; i++){
cin >> pages[i];
}
cout << endl;
for (int i = 0; i < pageSize; i++) {
fifo.insertPage(pages[i]);
fifo.displayPages();
}
cout << "\n\n<----------Result Analysis---------->\n\n";
cout << "Total Page Faults: " << fifo.getPageFaultCount() << endl;
cout << "Total Page Hits: " << fifo.getPageHitsCount() << endl << endl;
cout << "Page Hit Rate = " <<fixed<<setprecision(2) << fifo.hitRate() << " %" << endl;
cout << "Page Page Rate = " <<fixed<<setprecision(2) << fifo.faultRate() << " %" << endl << endl;
cout << "-------------------------------------";
return 0;
}
// Total Page Faults = Total Number of Page Requests−Total Page Hits
// Page Fault Rate = Total Page Faults / Total Page Requests
// 1 2 3 4 2 5 1 2 3 4 5
// Belady’s Anomaly: for some page-replacement algorithms, the page-fault rate may increase
// as the number of allocated frames increases.