-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbelady.cpp
More file actions
104 lines (96 loc) · 2.58 KB
/
Copy pathbelady.cpp
File metadata and controls
104 lines (96 loc) · 2.58 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
#include <iostream>
#include <queue>
#include <deque>
#include <random>
#include <thread>
#include <time.h>
#include <stdlib.h>
std::deque<std::deque<int> > generateRandomPageSequence()
{
srand(time(NULL));
std::deque<std::deque<int> > v;
for (int i = 0; i < 100; i++) {
std::deque<int> a;
for (int j = 0; j < 1000; j++) {
a.push_back(rand() % 250);
}
v.push_back(a);
}
return v;
}
bool checkForMiss(std::deque<int> q, int n) {
for (auto e : q) {
if (e == n) {
return false;
}
}
return true;
}
int getMisses(std::deque<std::deque<int> > &seq, unsigned int sequence, unsigned int frameSize) {
int misses = 0;
std::deque<int> qu;
qu.clear();
for (unsigned int j = 0; j < seq.at(sequence).size(); j++) {
if (qu.size() > frameSize) {
bool miss = checkForMiss(qu, seq.at(sequence).at(j));
if (miss) {
misses++;
qu.pop_front();
qu.push_back(seq.at(sequence).at(j));
}
}
else if(checkForMiss(qu, seq.at(sequence).at(j))){
qu.push_back(seq.at(sequence).at(j));
}
}
return misses;
}
std::deque<std::deque<int> > getResults(std::deque<std::deque<int> > s) {
std::deque<std::deque<int> > res;
for (unsigned int i = 0; i < 100; i++) {
std::deque<int> subRes;
for (unsigned int j = 0; j < 100; j++) {
subRes.push_back(getMisses(s, j, i));
}
res.push_back(subRes);
}
return res;
}
void displayResults(std::deque<std::deque<int> > r) {
int anomalyCounter = 0;
std::cout << "Length of memory reference string: 1000" << std::endl;
std::cout << "Frames of physical memory: 100" << std::endl;
for (unsigned int i = 0; i < r.size(); i++) {
//
for (unsigned int j = 0; j < (r.at(0).size() - 1); j++) {
if (r.at(i).at(j) < r.at(i).at(j + 1)) {
anomalyCounter++;
std::cout << "Anomaly Discovered!" << std::endl;
std::cout << "\tSequence :" << (i + 1) << std::endl;
std::cout << "\tPage Faults: " << r.at(i).at(j) << " @ Frame Size: " << (j + 1) << std::endl;
std::cout << "\tPage Faults: " << r.at(i).at(j + 1) << " @ Frame Size: " << (j + 2) << std::endl << std::endl;
}
}
}
std::cout << "Anomaly detected " << anomalyCounter << " times." << std::endl;
}
int main() {
srand(time(NULL));
auto sequences = generateRandomPageSequence();
auto results = getResults(sequences);
displayResults(results);
}
/*
*
* notes from class on 11/28
*
* Anomaly only occurs on FIFO
*
* break down{
* 1000 touches of memory, these touches select numbers between 1 and 250
* Simulate that 1000 length string over 1 frame of memory
* Simulate same string over two ... three ... four ... 100 count page faults that occur.
* ONLY ONE STRING :)
* }
*
*/