-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathinterrupts.cpp
More file actions
184 lines (134 loc) · 6.98 KB
/
interrupts.cpp
File metadata and controls
184 lines (134 loc) · 6.98 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
*
* @file interrupts.cpp
* @author Sasisekhar Govind
*
*/
#include<interrupts.hpp>
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) {
std::string trace; //!< string to store single line of trace file
std::string execution = ""; //!< string to accumulate the execution output
std::string system_status = ""; //!< string to accumulate the system status output
int current_time = time;
//parse each line of the input trace file. 'for' loop to keep track of indices.
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") { //As per Assignment 1
execution += std::to_string(current_time) + ", " + std::to_string(duration_intr) + ", CPU Burst\n";
current_time += duration_intr;
} else if(activity == "SYSCALL") { //As per Assignment 1
auto [intr, time] = intr_boilerplate(current_time, duration_intr, 10, vectors);
execution += intr;
current_time = time;
execution += std::to_string(current_time) + ", " + std::to_string(delays[duration_intr]) + ", SYSCALL ISR (ADD STEPS HERE)\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, time] = intr_boilerplate(current_time, duration_intr, 10, vectors);
current_time = time;
execution += intr;
execution += std::to_string(current_time) + ", " + std::to_string(delays[duration_intr]) + ", ENDIO ISR(ADD STEPS HERE)\n";
current_time += delays[duration_intr];
execution += std::to_string(current_time) + ", 1, IRET\n";
current_time += 1;
} else if(activity == "FORK") {
auto [intr, time] = intr_boilerplate(current_time, 2, 10, vectors);
execution += intr;
current_time = time;
///////////////////////////////////////////////////////////////////////////////////////////
//Add your FORK output here
///////////////////////////////////////////////////////////////////////////////////////////
//The following loop helps you do 2 things:
// * Collect the trace of the chile (and only the child, skip parent)
// * Get the index of where the parent is supposed to start executing from
std::vector<std::string> child_trace;
bool skip = true;
bool exec_flag = false;
int parent_index = 0;
for(size_t j = i; j < trace_file.size(); j++) {
auto [_activity, _duration, _pn] = parse_trace(trace_file[j]);
if(skip && _activity == "IF_CHILD") {
skip = false;
continue;
} else if(_activity == "IF_PARENT"){
skip = true;
parent_index = j;
if(exec_flag) {
break;
}
} else if(skip && _activity == "ENDIF") {
skip = false;
continue;
} else if(!skip && _activity == "EXEC") {
skip = true;
child_trace.push_back(trace_file[j]);
exec_flag = true;
}
if(!skip) {
child_trace.push_back(trace_file[j]);
}
}
i = parent_index;
///////////////////////////////////////////////////////////////////////////////////////////
//With the child's trace, run the child (HINT: think recursion)
///////////////////////////////////////////////////////////////////////////////////////////
} else if(activity == "EXEC") {
auto [intr, time] = intr_boilerplate(current_time, 3, 10, vectors);
current_time = time;
execution += intr;
///////////////////////////////////////////////////////////////////////////////////////////
//Add your EXEC output here
///////////////////////////////////////////////////////////////////////////////////////////
std::ifstream exec_trace_file(program_name + ".txt");
std::vector<std::string> exec_traces;
std::string exec_trace;
while(std::getline(exec_trace_file, exec_trace)) {
exec_traces.push_back(exec_trace);
}
///////////////////////////////////////////////////////////////////////////////////////////
//With the exec's trace (i.e. trace of external program), run the exec (HINT: think recursion)
///////////////////////////////////////////////////////////////////////////////////////////
break; //Why is this important? (answer in report)
}
}
return {execution, system_status, current_time};
}
int main(int argc, char** argv) {
//vectors is a C++ std::vector of strings that contain the address of the ISR
//delays is a C++ std::vector of ints that contain the delays of each device
//the index of these elemens is the device number, starting from 0
//external_files is a C++ std::vector of the struct 'external_file'. Check the struct in
//interrupt.hpp to know more.
auto [vectors, delays, external_files] = parse_args(argc, argv);
std::ifstream input_file(argv[1]);
//Just a sanity check to know what files you have
print_external_files(external_files);
//Make initial PCB (notice how partition is not assigned yet)
PCB current(0, -1, "init", 1, -1);
//Update memory (partition is assigned here, you must implement this function)
if(!allocate_memory(¤t)) {
std::cerr << "ERROR! Memory allocation failed!" << std::endl;
}
std::vector<PCB> wait_queue;
/******************ADD YOUR VARIABLES HERE*************************/
/******************************************************************/
//Converting the trace file into a vector of strings.
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;
}