-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathinterrupts_student1_student2_EP.cpp
More file actions
119 lines (91 loc) · 4.15 KB
/
interrupts_student1_student2_EP.cpp
File metadata and controls
119 lines (91 loc) · 4.15 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
/**
* @file interrupts.cpp
* @author Sasisekhar Govind
* @brief template main.cpp file for Assignment 3 Part 1 of SYSC4001
*
*/
#include<interrupts_student1_student2.hpp>
void FCFS(std::vector<PCB> &ready_queue) {
std::sort(
ready_queue.begin(),
ready_queue.end(),
[]( const PCB &first, const PCB &second ){
return (first.arrival_time > second.arrival_time);
}
);
}
std::tuple<std::string /* add std::string for bonus mark */ > run_simulation(std::vector<PCB> list_processes) {
std::vector<PCB> ready_queue; //The ready queue of processes
std::vector<PCB> wait_queue; //The wait queue of processes
std::vector<PCB> job_list; //A list to keep track of all the processes. This is similar
//to the "Process, Arrival time, Burst time" table that you
//see in questions. You don't need to use it, I put it here
//to make the code easier :).
unsigned int current_time = 0;
PCB running;
//Initialize an empty running process
idle_CPU(running);
std::string execution_status;
//make the output table (the header row)
execution_status = print_exec_header();
//Loop while till there are no ready or waiting processes.
//This is the main reason I have job_list, you don't have to use it.
while(!all_process_terminated(job_list) || job_list.empty()) {
//Inside this loop, there are three things you must do:
// 1) Populate the ready queue with processes as they arrive
// 2) Manage the wait queue
// 3) Schedule processes from the ready queue
//Population of ready queue is given to you as an example.
//Go through the list of proceeses
for(auto &process : list_processes) {
if(process.arrival_time == current_time) {//check if the AT = current time
//if so, assign memory and put the process into the ready queue
assign_memory(process);
process.state = READY; //Set the process state to READY
ready_queue.push_back(process); //Add the process to the ready queue
job_list.push_back(process); //Add it to the list of processes
execution_status += print_exec_status(current_time, process.PID, NEW, READY);
}
}
///////////////////////MANAGE WAIT QUEUE/////////////////////////
//This mainly involves keeping track of how long a process must remain in the ready queue
/////////////////////////////////////////////////////////////////
//////////////////////////SCHEDULER//////////////////////////////
FCFS(ready_queue); //example of FCFS is shown here
/////////////////////////////////////////////////////////////////
}
//Close the output table
execution_status += print_exec_footer();
return std::make_tuple(execution_status);
}
int main(int argc, char** argv) {
//Get the input file from the user
if(argc != 2) {
std::cout << "ERROR!\nExpected 1 argument, received " << argc - 1 << std::endl;
std::cout << "To run the program, do: ./interrutps <your_input_file.txt>" << std::endl;
return -1;
}
//Open the input file
auto file_name = argv[1];
std::ifstream input_file;
input_file.open(file_name);
//Ensure that the file actually opens
if (!input_file.is_open()) {
std::cerr << "Error: Unable to open file: " << file_name << std::endl;
return -1;
}
//Parse the entire input file and populate a vector of PCBs.
//To do so, the add_process() helper function is used (see include file).
std::string line;
std::vector<PCB> list_process;
while(std::getline(input_file, line)) {
auto input_tokens = split_delim(line, ", ");
auto new_process = add_process(input_tokens);
list_process.push_back(new_process);
}
input_file.close();
//With the list of processes, run the simulation
auto [exec] = run_simulation(list_process);
write_output(exec, "execution.txt");
return 0;
}