-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.cpp
231 lines (200 loc) · 6.41 KB
/
scheduler.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// Created by Osayamen on 2/25/2023.
//
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <unordered_map>
#include <utility>
#include <vector>
#include "computer/print.h"
#include "utility/utility.h"
#include "scheduler/queue.h"
#include "scheduler/scheduler.h"
std::unordered_map<int, PCB> PCBs;
int initial_PID;
PCB idle_prog;
int idle_prog_PID = 0;
PCB running_pcb;
int current_mem_instr_time;
MemoryMetadata current_process_metadata;
std::mutex mtx;
const PCB NULL_PCB = {
idle_prog_PID
};
int rq_levels = 4;
int get_and_update_PID(){
return initial_PID++;
}
void process_init_PCBs(){
PCBs = std::unordered_map<int, PCB>();
}
PCB process_init_PCB(std::string &fname, MemoryMetadata &metadata){
REGS regs = REGS();
PCB p = {
get_and_update_PID(),
regs,
0,
false,
fname,
0,
metadata
};
PCBs.insert({p.PID, p});
return p;
}
void process_dispose_PCB(int pid){
PCBs.erase(pid);
}
void process_dump_PCB(){
std::stringstream dump_stream;
dump_stream << generate_header();
dump_stream << std::setw(17) <<"PCB Dump" << std::endl;
dump_stream << generate_header();
dump_stream << "Index: [ Filename:XXXXX, PID:#, PC:#, IR0:#, IR1:#, AC:#, MAR:#, MBR:# ]" << std::endl;
int i = 0;
for(const auto& pcb : PCBs){
PCB p = pcb.second;
dump_stream << i << ": [";
dump_stream << "Filename:" << p.file_name << ", ";
dump_stream << "PID:" << p.PID << ", ";
dump_stream << "PC:" << p.cpuRegisters.PC << ", ";
dump_stream << "IR0:" << p.cpuRegisters.IR0 << ", ";
dump_stream << "IR1:" << p.cpuRegisters.IR1 << ", ";
dump_stream << "AC:" << p.cpuRegisters.AC << ", ";
dump_stream << "MAR:" << p.cpuRegisters.MAR << ", ";
dump_stream << "MBR:" << p.cpuRegisters.MBR << " ";
dump_stream << "]" << std::endl;
i++;
}
std::string dump_str = dump_stream.str();
atomically_print_to_stdout(dump_str);
}
void process_init_readyQ(){
queue_init(rq_levels);
}
void process_insert_readyQ(PCB &p){
mtx.lock();
queue_insert(p);
mtx.unlock();
}
PCB process_fetch_readyQ(){
PCB p = queue_fetch();
if(p == NULL_PCB) return idle_prog;
p.finishedTQ = false;
return p;
}
void process_dump_readyQ(){
queue_dump();
}
void process_scheduler_init() {
//initialize registers to idle program state
registers = REGS();
registers.AC = 1; // needs to be greater than zero.
// load idle program
std::string idle_fname = "prog_idle.txt";
MemoryMetadata m = load_prog(const_cast<char*>(idle_fname.c_str()));
idle_prog = PCB();
idle_prog.PID = idle_prog_PID;
idle_prog.cpuRegisters = registers;
idle_prog.metadata = m;
idle_prog.file_name = idle_fname;
current_mem_instr_time = 0;
process_init_PCBs();
initial_PID = 1;
current_process_metadata = idle_prog.metadata;
running_pcb = PCB();
process_init_readyQ();
}
void process_context_switch(PCB ¤tPCB, PCB &newPCB){
std::string ctx_switch_log = std::string("Switching from Process {PID->")
.append(std::to_string(currentPCB.PID))
.append(", PC->")
.append(std::to_string(currentPCB.cpuRegisters.PC))
.append("} ")
.append("to Process {PID->")
.append(std::to_string(newPCB.PID))
.append(", PC->")
.append(std::to_string(newPCB.cpuRegisters.PC))
.append("}");
PCB temp = currentPCB;
currentPCB = std::move(newPCB);
if(temp.PID != idle_prog_PID) {
process_insert_readyQ(temp);
atomically_print_to_stdout(ctx_switch_log);
}
}
void process_submit(std::string& fname, MemoryMetadata& metadata){
PCB p = process_init_PCB(fname, metadata);
print_act({INIT_SPOOL, CID, p.PID},
"print::init_spool",
"Started spool for process " + std::to_string(p.PID));
process_insert_readyQ(p);
}
void process_set_registers(REGS ®s){
registers = regs;
}
void process_set_mem_metadata(MemoryMetadata &m){
current_process_metadata = m;
}
void process_execute(){
ExecutionStatus e = Executing; // initial state
while (!terminateFlag){
switch(e){
case Exit:
process_exit(running_pcb.PID);
running_pcb = process_fetch_readyQ();
break;
case TQExpiration: {
running_pcb.finishedTQ = true; // needed for the multi-level feedback queue.
PCB ready_pcb = process_fetch_readyQ(); // Returns the idle program when the RQ is empty.
if(ready_pcb != idle_prog){
process_context_switch(running_pcb, ready_pcb);
}
else if(running_pcb != idle_prog){ // Continue executing the user's program since the RQ is empty.
running_pcb.priority = std::min((rq_levels - 1), (running_pcb.priority + 1));
}
break;
}
case IODelay: {
running_pcb.finishedTQ = false;
PCB ready_pcb = process_fetch_readyQ();
if(ready_pcb != idle_prog){
process_context_switch(running_pcb, ready_pcb);
}
break;
}
case Executing:
running_pcb = process_fetch_readyQ();
break;
}
current_mem_instr_time = running_pcb.mem_instr_delay;
process_set_mem_metadata(running_pcb.metadata);
process_set_registers(running_pcb.cpuRegisters);
e = cpu_operation(running_pcb.PID);
running_pcb.mem_instr_delay = current_mem_instr_time;
if(running_pcb.PID != idle_prog_PID){
running_pcb.cpuRegisters = registers;
PCBs.at(running_pcb.PID) = running_pcb; //update state after execution.
}
}
scheduler_terminate();
}
void process_exit(int pid){
reclaim_memory(running_pcb.metadata.page_table);
process_dispose_PCB(pid);
print_act({END_SPOOL, CID, pid},
"print::end_spool",
"Ended spooling for process " + std::to_string(pid));
}
void scheduler_terminate(){
atomically_print_to_stdout("Terminating scheduler...");
PCBs.clear();
print_act({TERMINATE, CID},
"print::terminate",
"Print & Printer Termination complete!");
stop_print();
}
PCB get_running_PCB(){
return running_pcb;
}