-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnhancedSecondChanceAlgorithm.cpp
More file actions
44 lines (39 loc) · 1.12 KB
/
EnhancedSecondChanceAlgorithm.cpp
File metadata and controls
44 lines (39 loc) · 1.12 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
#include "EnhancedSecondChanceAlgorithm.h"
#include <iostream>
using namespace std;
EnhancedSecondChanceAlgorithm::EnhancedSecondChanceAlgorithm(int number_of_frames, int page_size) : Algorithm(number_of_frames, page_size) {
pointer = pages.end();
}
void EnhancedSecondChanceAlgorithm::_access(long long page, MemoryReference::AccessType access_type) {
for(list<PageEntity>::iterator it = pages.begin(); it != pages.end(); ++it) {
if(it->page() == page) {
if (access_type == MemoryReference::WRITE) {
it->set_modify_bit();
}
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->modify_bit()) {
if (pointer->reference_bit()) {
pointer->reset_reference_bit();
} else {
// writes page to disk!
pointer->reset_modify_bit();
}
pointer++;
if (pointer == pages.end())
pointer = pages.begin();
}
*pointer = PageEntity(page);
} else {
pages.push_back(PageEntity(page));
}
}
}