-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
76 lines (65 loc) · 2.38 KB
/
main.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
#include <iostream>
#include <memory>
#include <queue>
#include "constants.hpp"
#include "System.hpp"
double rand_zero_to_one(){
return (double)rand() / RAND_MAX;
}
int main(){
srand(time(NULL));
System system = System(PHYSICAL_PAGE_COUNT);
std::queue<std::shared_ptr<Process>> process_queue = std::queue<std::shared_ptr<Process>>();
int time_remain = 0;
int processes_finished = 0;
int id = 0;
for(int i = 0; i < TIME;){
if(rand_zero_to_one() < 0.1){
id++;
int burst_time = rand() % 20;
std::shared_ptr<Process> p = std::shared_ptr<Process>(new Process(id, WORKING_SET_SIZE, VIRTUAL_PAGE_COUNT, burst_time));
process_queue.push(p);
}
if(!process_queue.empty()){
std::shared_ptr<Process> process = process_queue.front();
process_queue.pop();
time_remain = QUANTUM;
if(rand_zero_to_one() < 0.05){
process->changeWorkingSet();
}
while(time_remain != 0){
int virtual_page_address = 0;
if(rand_zero_to_one() < 0.9){
int virtual_page_index = rand() % process->working_set_size;
virtual_page_address = process->virtual_pages[virtual_page_index].virtual_page_address;
}
else{
int virtual_page_index = rand() % (process->virtual_page_count - process->working_set_size) + process->working_set_size;
virtual_page_address = process->virtual_pages[virtual_page_index].virtual_page_address;
}
if(rand_zero_to_one() < 0.7){
system.read(process, virtual_page_address);
}
else{
system.write(process, virtual_page_address);
}
process->burst_time -= 1;
time_remain -= 1;
if(process->burst_time == 0){
processes_finished += 1;
break;
}
i++;
if (i == TIME) break;
}
if(process->burst_time == 0){
system.kill(process);
}
else{
process_queue.push(process);
}
}
}
std::cout << system.get_page_fault_count() << std::endl;
return 0;
}