forked from sysc4001/Assignment_2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterrupts.cpp
More file actions
141 lines (114 loc) · 5.53 KB
/
interrupts.cpp
File metadata and controls
141 lines (114 loc) · 5.53 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
*
* @file interrupts.cpp
* @author Aryan Singh [101270896], Joodi Al-Asaad [101200152]
*
*/
#include <interrupts.hpp>
int PID_COUNTER = 1;
std::string snapshot_line(int time, const std::string& trace, const PCB& process, const std::vector<PCB>& wait_queue) {
return std::to_string(time) + ", Snapshot placeholder for process " + std::to_string(process.PID) + "\n";
}
std::tuple<std::string, std::string, int> simulate_trace(
std::vector<std::string> trace_file, int time, std::vector<std::string> vectors,
std::vector<int> delays, std::vector<external_file> external_files,
PCB current, std::vector<PCB> wait_queue) {
if (time > 10000) {
std::cerr << "Terminating: Simulation timeout reached.\n";
return {"", "", time};
}
std::string execution = "";
std::string system_status = "";
int current_time = time;
for (size_t i = 0; i < trace_file.size(); i++) {
auto trace = trace_file[i];
auto [activity, duration_intr, program_name] = parse_trace(trace);
if (activity == "CPU") {
execution += std::to_string(current_time) + ", " + std::to_string(duration_intr) + ", CPU Burst\n";
current_time += duration_intr;
}
else if (activity == "SYSCALL") {
auto [intr, t] = intr_boilerplate(current_time, duration_intr, 10, vectors);
execution += intr;
current_time = t;
execution += std::to_string(current_time) + ", " + std::to_string(delays[duration_intr]) + ", SYSCALL ISR\n";
current_time += delays[duration_intr];
execution += std::to_string(current_time) + ", 1, IRET\n";
current_time += 1;
}
else if (activity == "END_IO") {
auto [intr, t] = intr_boilerplate(current_time, duration_intr, 10, vectors);
execution += intr;
current_time = t;
execution += std::to_string(current_time) + ", " + std::to_string(delays[duration_intr]) + ", ENDIO ISR\n";
current_time += delays[duration_intr];
execution += std::to_string(current_time) + ", 1, IRET\n";
current_time += 1;
}
else if (activity == "FORK") {
auto [intr, t] = intr_boilerplate(current_time, 2, 10, vectors);
execution += intr;
current_time = t;
execution += std::to_string(current_time) + ", " + std::to_string(duration_intr) + ", cloning the PCB\n";
current_time += duration_intr;
PCB child(PID_COUNTER++, current.PID, current.program_name, current.size, -1);
if (!allocate_memory(&child)) {
execution += std::to_string(current_time) + ", 0, ERROR: memory allocation failed for child\n";
continue;
}
execution += std::to_string(current_time) + ", 0, scheduler called\n";
execution += std::to_string(current_time) + ", 1, IRET\n";
current_time++;
system_status += snapshot_line(current_time, trace, child, wait_queue);
// Skip recursion for child to avoid infinite loops
free_memory(&child);
execution += std::to_string(current_time) + ", 1, switch to parent and IRET\n";
current_time++;
}
else if (activity == "EXEC") {
auto [intr, t] = intr_boilerplate(current_time, 3, 10, vectors);
execution += intr;
current_time = t;
unsigned int prog_size = get_size(program_name, external_files);
execution += std::to_string(current_time) + ", " + std::to_string(duration_intr) +
", Program is " + std::to_string(prog_size) + " Mb large\n";
current_time += duration_intr;
int load_time = 15 * prog_size;
execution += std::to_string(current_time) + ", " + std::to_string(load_time) + ", loading program into memory\n";
current_time += load_time;
free_memory(¤t);
current.program_name = program_name;
current.size = prog_size;
allocate_memory(¤t);
execution += std::to_string(current_time) + ", 3, marking partition as occupied\n";
current_time += 3;
execution += std::to_string(current_time) + ", 6, updating PCB\n";
current_time += 6;
execution += std::to_string(current_time) + ", 0, scheduler called\n";
execution += std::to_string(current_time) + ", 1, IRET\n";
current_time++;
system_status += snapshot_line(current_time, trace, current, wait_queue);
// Disable recursive EXEC loading
execution += std::to_string(current_time) + ", 0, Skipped recursive EXEC load\n";
}
}
return {execution, system_status, current_time};
}
int main(int argc, char** argv) {
auto [vectors, delays, external_files] = parse_args(argc, argv);
std::ifstream input_file(argv[1]);
print_external_files(external_files);
PCB current(0, -1, "init", 1, -1);
if (!allocate_memory(¤t)) {
std::cerr << "ERROR! Memory allocation failed!" << std::endl;
}
std::vector<PCB> wait_queue;
std::vector<std::string> trace_file;
std::string trace;
while (std::getline(input_file, trace)) trace_file.push_back(trace);
auto [execution, system_status, _] = simulate_trace(trace_file, 0, vectors, delays, external_files, current, wait_queue);
input_file.close();
write_output(execution, "execution.txt");
write_output(system_status, "system_status.txt");
return 0;
}