-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathb4.cpp
119 lines (92 loc) · 3.06 KB
/
b4.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
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
#include<iostream>
#include<deque>
#include<unordered_map>
#include<vector>
#include<random>
#include<time.h>
void printAnomaly(int i, int j, int faultStorage, int faultCounter){
std::cout << "Anomaly Discovered" << std::endl;
std::cout << "Sequence:" << i << std::endl;
std::cout << "\tPage Faults:" << faultStorage << " @ Frame Size: " << (j) << std::endl;
std::cout << "\tPage Faults:" << faultCounter << " @ Frame Size: " << j+1 << std::endl << std::endl;
}
int main(){
// anomalyCounter holds the total number of anomalies
int anomalyCounter = 0;
//seeding random
srand(time(NULL));
int memoryIterator = 0;
//for 100 sequences
for(int i = 0; i < 100; i++){
//generate a sequence
std::vector<int> sequence;
for(int g = 0; g < 1000; g++){
sequence.push_back(rand() %250);
}
//faultCounter should hold the number of page faults on the current iteration
int faultCounter = 0;
//faultStorage should hold the number of page faults on the previous iteration
//It is set to 9999 so that on the very first iteration it will be greater than
//the faultCounter iterator
int faultStorage = 9999;
//for page sizes from 1 to 100
for(int j = 1; j < 101; j++){
//Sanity check: making sure faultStorage is going to be the larger value on the first iteration
if(j == 1){
faultStorage = 9999;
}
//Keep the current values in a hash table
std::unordered_map<int,int> memory;
//Impliment FIFO with a std::queue
std::deque<int> q;
//For every number in the sequence
for(int h = 0; h < 1000; h++){
//check whether it is in memory
bool check = false;
for(auto e : q){
if(e == sequence.at(h)){check = true;}
}
//if it is NOT in memory,
if(check == false){
//and if the size of the queue is greater than the number of pages
if(q.size() >= j){
//delete the first element from memory
auto iterator = memory.find(q.front());
if(iterator != memory.end()){memory.erase(iterator);}
//if(memory.find(0) != memory.end()){std::cout << "fuck" << std::endl;}
//Pop the first element off of the queue
q.pop_front();
}
//(we fall back into scope of the first if statement) If it is not in memory, insert that element of the sequence into memory
memory.insert({memoryIterator, sequence.at(h)});
memoryIterator++;
//push it onto the queue
q.push_back(sequence.at(h));
//any time we have to insert something into memeory, that counts as a page fault
faultCounter++;
}
}
//If the number of faults from the previous iteration exceeds that of the current iteration,
if(faultStorage < faultCounter){
//An anomaly happened
printAnomaly(i, j, faultStorage, faultCounter);
anomalyCounter++;
}
//faultStorage gets fault Counter
faultStorage = faultCounter;
//faultCounter is zero for the next round
faultCounter = 0;
//clear out the queue
q.clear();
memoryIterator = 0;
//clear out memeory
memory.clear();
//go to the next iteration
}
//this is more tidying than anything, but clear the sequence
sequence.clear();
//start again.
}
std::cout << anomalyCounter << std::endl;
return 0;
}