-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondChanceAlgorithm.cpp
More file actions
36 lines (31 loc) · 888 Bytes
/
SecondChanceAlgorithm.cpp
File metadata and controls
36 lines (31 loc) · 888 Bytes
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
#include "SecondChanceAlgorithm.h"
#include <iostream>
using namespace std;
SecondChanceAlgorithm::SecondChanceAlgorithm(int number_of_frames, int page_size) : Algorithm(number_of_frames, page_size) {
pointer = pages.end();
}
void SecondChanceAlgorithm::_access(long long page, MemoryReference::AccessType accessType) {
for(list<PageEntity>::iterator it = pages.begin(); it != pages.end(); ++it) {
if(it->page() == page) {
it->set_reference_bit();
return;
}
}
this->page_faults++;
if (pages.size() == 0) {
pages.push_back(PageEntity (page));
pointer = pages.begin();
} else {
if(pages.size() >= number_of_frames) {
while (pointer->reference_bit()) {
pointer->reset_reference_bit();
pointer++;
if (pointer == pages.end())
pointer = pages.begin();
}
*pointer = PageEntity(page);
} else {
pages.push_back(PageEntity (page));
}
}
}