-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMFU_PageReplacement.cpp
More file actions
46 lines (40 loc) · 1.91 KB
/
MFU_PageReplacement.cpp
File metadata and controls
46 lines (40 loc) · 1.91 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
/* This code implements the MFU (Most Frequently Used) page replacement algorithm.
* When a page is accessed, it checks if it is already in memory. If it is, the frequency count for the page is incremented.
* If it is not in memory, and the memory is full, the page with the highest frequency count is evicted.
* The new page is added to memory, and the page fault counter is incremented.
*/
#include <iostream>
#include "MFU_PageReplacement.h"
MFU_PageReplacement::MFU_PageReplacement(int capacity) : PageReplacement(capacity) {}
void MFU_PageReplacement::accessPage(int pageNum) {
if (pageSet.find(pageNum) != pageSet.end()) {
std::cout << "***********************************************************************************" << std::endl;
std::cout << " ---> Page " << pageNum << " is already in memory.\n";
frequencyMap[pageNum]++;
}
else {
if (pages.size() >= capacity) {
int evictedPage = -1;
int maxFrequency = -1;
// Find the page with the highest frequency count
for (auto i=pageSet.begin(); i != pageSet.end(); ++i) {
int page = *i;
if (frequencyMap[page] > maxFrequency) {
maxFrequency = frequencyMap[page];
evictedPage = page;
}
}
// Evict the page with the highest frequency count
pageSet.erase(evictedPage);
frequencyMap.erase(evictedPage);
std::cout << " ---> Page " << evictedPage << " is evicted from memory.\n";
}
// Add the new page to memory
pages.push(pageNum);
pageSet.insert(pageNum);
frequencyMap[pageNum] = 1;
std::cout << "***********************************************************************************" << std::endl;
std::cout << " ---> Page " << pageNum << " is loaded into memory.\n";
pageFaults++;
}
}