-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSRT.cpp
More file actions
322 lines (287 loc) · 13.8 KB
/
SRT.cpp
File metadata and controls
322 lines (287 loc) · 13.8 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <iostream>
#include <string>
#include <vector>
#include "Process.h"
#include "SRT.h"
#include <time.h>
#include <map>
#include <utility>
#include <algorithm>
#include "tgmath.h"
SRT::SRT(vector<Process> passedProcessList, int passedContextSwitch, float mainLambda, float someAlpha){
processList = passedProcessList;
contextSwitch = passedContextSwitch;
lambda = mainLambda;
alpha = someAlpha;
totalTime = 0;
CPUTime = 0;
contextSwitchTracker = 0;
}
void SRT::SRTAlgorithm() {
int contextSwitchTime = 0;
int burstTime = 0;
int cpu = 0; //0 when CPU is free and 1 when CPU is being used
vector<Process> tempList = processList;
vector<Process> finished; //Vector storing finished processes
map<char, int> IOBlock;
map<char, unsigned int> burstTracker; //Map keeping track of how many bursts have been completed
map<char, int> blockList; //Map to keep track of the block a process is on
map<char, unsigned int> tauTracker; // storing the Tau values
map<char, unsigned int> IOTracker;
map<char, unsigned int> procWaitTime;
Process currentCPU = processList[0];
//setting up the map
for(unsigned int i = 0; i < processList.size(); i++){
char name = processList[i].get_id();
burstTracker[name] = 0;
IOBlock[name] = 0;
blockList[name] = 0;
tauTracker[name] = 1/lambda;
procWaitTime[name] = 0;
}
int time = 0;
std::cout << "time 0ms: Simulator started for SRT [Q empty]" << std::endl;
while (true) {
if(cpu == 0){
if(queueList.size() >= 1 && contextSwitchTime <= time){
cpu = 1;
contextSwitchTracker = 1;
int burstInterval = burstTracker[queueList[0].get_id()];
burstTime = (queueList[0].get_burst_list())[burstInterval];
if(time < 1000){
std::cout << "time " << time << "ms: Process " << queueList[0].get_id() << " (tau " << tauTracker[queueList[0].get_id()] << "ms) started using the CPU for " << burstTime << "ms burst [Q ";
if (queueList.size() == 1){
std::cout << "empty]" << std::endl;
}
else{
for (unsigned int c = 1; c < queueList.size(); c++){
std::cout << queueList[c].get_id();
}
std::cout << "]" << std::endl;
}
}
burstTime = burstTime + time;
currentCPU = queueList[0];
burstTracker[queueList[0].get_id()] += 1;
queueList.erase(queueList.begin());
}
}
//Checking if a burst has finished
if(time == burstTime && burstTime != 0){
cpu = 0;
burstTime = 0;
int burstsLeft = currentCPU.get_burst_list().size() - burstTracker[currentCPU.get_id()];
contextSwitchTime += time;
if(burstTracker[currentCPU.get_id()] == currentCPU.get_burst_list().size()){
std::cout << "time " << time << "ms: Process " << currentCPU.get_id() << " terminated [Q ";
if(queueList.size() == 0){
std::cout << "empty]" << std::endl;
}
else{
for(unsigned int i = 1; i < queueList.size(); i++){
std::cout << queueList[i].get_id();
}
std::cout << "]" << std::endl;
}
if(queueList.size() > 0)
contextSwitchTracker += 1;
contextSwitchTime = 0;
if(queueList.size() > 0){
contextSwitchTime = contextSwitch + time;
}
else{
contextSwitchTime = (contextSwitch/2) + time;
}
for(unsigned int i = 0; i < processList.size(); i++){
if(processList[i].get_id() == currentCPU.get_id()){
finished.push_back(processList[i]);
}
}
}
else{
if(time < 1000){
std::cout << "time " << time <<"ms: Process " << currentCPU.get_id() << " (tau " << tauTracker[currentCPU.get_id()] << "ms) completed a CPU burst; " << burstsLeft << " bursts to go [Q ";
if(queueList.size() == 0){
std::cout << "empty]" << std::endl;
}
else{
for(unsigned int i = 0; i < queueList.size(); i++){
std::cout << queueList[i].get_id();
}
std::cout << "]" << std::endl;
}
}
if(queueList.size() > 0){
contextSwitchTracker++;
}
//Calculating Tau
int tempTau = tauTracker[currentCPU.get_id()];
tauTracker[currentCPU.get_id()] = ceil((1-alpha) * tauTracker[currentCPU.get_id()] + alpha * currentCPU.get_burst_list()[burstTracker[currentCPU.get_id()]-1]);
if(time < 1000){
std::cout << "time " << time << "ms: Recalculated tau from " << tempTau << "ms to " << tauTracker[currentCPU.get_id()] << "ms for process " << currentCPU.get_id() << " [Q ";
if(queueList.size() == 0){
std::cout << "empty]" << std::endl;
}
else{
for(unsigned int i = 0; i < queueList.size(); i++){
std::cout << queueList[i].get_id();
}
cout << "]" << endl;
}
}
int IOInterval = blockList[currentCPU.get_id()];
int IOTime = currentCPU.get_io_list()[IOInterval];
IOTime += time;
IOTime += (contextSwitch/2);
if(queueList.size() > 0){
contextSwitchTime = contextSwitch + time;
}
else{
contextSwitchTime = (contextSwitch/2) + time;
}
blockList[currentCPU.get_id()] += 1;
IOBlock[currentCPU.get_id()] = IOTime;
if(time < 1000){
std::cout << "time " << time << "ms: Process " << currentCPU.get_id() << " switching out of CPU; will block on I/O until time " << IOTime << "ms [Q ";
if (queueList.size() == 0){
std::cout << "empty]" << std::endl;
}
else{
for (unsigned int i = 0; i < queueList.size(); i++){
std::cout << queueList[i].get_id();
}
std::cout << "]" << std::endl;
}
}
}
}
if(tempList.size() > 0){
for(unsigned int i = 0; i < tempList.size(); i++){
if(time == tempList[i].get_arrival_time()){
if(queueList.size() == 0){
contextSwitchTime = (contextSwitch/2) + time;
}
//Figuring out where to add new process
unsigned int tempIter = 0;
for(tempIter = 0; tempIter < queueList.size(); tempIter++){
if(tauTracker[queueList[tempIter].get_id()] > tauTracker[tempList[i].get_id()]){
break;
}
else if(tauTracker[queueList[tempIter].get_id()] == tauTracker[tempList[i].get_id()]){
if(burstTracker[queueList[tempIter].get_id()] > burstTracker[tempList[i].get_id()]){
break;
}
else if(burstTracker[queueList[tempIter].get_id()] == burstTracker[tempList[i].get_id()]){
if(IOTracker[queueList[tempIter].get_id()] > IOTracker[tempList[i].get_id()]){
break;
}
else if(IOTracker[queueList[tempIter].get_id()] == IOTracker[tempList[i].get_id()]){
if(queueList[tempIter].get_id() > tempList[i].get_id()){
break;
}
}
}
}
}
queueList.insert(queueList.begin()+tempIter, tempList[i]);
procWaitTime[tempList[i].get_id()] = time;
if(time < 1000)
std::cout << "time " << time << "ms: Process " << tempList[i].get_id() << " (tau " << tauTracker[tempList[i].get_id()] << "ms) arrived; added to ready queue [Q ";
tempList.erase(tempList.begin()+i);
if(time < 1000){
for(unsigned int j = 0; j < queueList.size(); j++){
std::cout << queueList[j].get_id();
}
std::cout << "]" << std::endl;
}
}
}
}
for(unsigned int i = 0; i < processList.size(); i++){
if(IOBlock[processList[i].get_id()] != 0 && IOBlock[processList[i].get_id()] == time){
IOBlock[processList[i].get_id()] = 0;
Process toqueue = processList[i];
bool preempt = false;
if (tauTracker[processList[i].get_id()] < tauTracker[currentCPU.get_id()] && cpu == 1) {
preempt = true;
if (time < 1000) {
std::cout << "time " << time << "ms: Process " << processList[i].get_id() << " (tau " << tauTracker[processList[i].get_id()] << "ms) completed I/O; preempting " << currentCPU.get_id() << " [Q ";
for (unsigned int e = 0; e < queueList.size(); e++){
std::cout << queueList[e].get_id();
}
std::cout << "]" << std::endl;
}
toqueue = currentCPU;
currentCPU = processList[i];
contextSwitchTime = contextSwitch + time;
}
//Figuring out where to add new process
unsigned int tempIter = 0;
for(tempIter = 0; tempIter < queueList.size(); tempIter++){
if(tauTracker[queueList[tempIter].get_id()] > tauTracker[toqueue.get_id()]){
//std::cout << "Tau for process " << toqueue.get_id() << " is less than tau for process " << queueList[tempIter] << std::endl;
break;
}
else if(tauTracker[queueList[tempIter].get_id()] == tauTracker[toqueue.get_id()]){
if(burstTracker[queueList[tempIter].get_id()] < burstTracker[toqueue.get_id()]){
//std::cout << "Bursts for process " << queueList[tempIter] << " (" << burstTracker[queueList[tempIter]] << ") is less than bursts for process " << queueList[tempIter] << " (" << burstTracker[toqueue.get_id()] << ")" << std::endl;
break;
}
else if(burstTracker[queueList[tempIter].get_id()] == burstTracker[toqueue.get_id()]){
if(IOTracker[queueList[tempIter].get_id()] < IOTracker[toqueue.get_id()]){
//std::cout << "IOBursts for process " << toqueue.get_id() << " is less than iobursts for process " << queueList[tempIter] << std::endl;
break;
}
else if(IOTracker[queueList[tempIter].get_id()] == IOTracker[toqueue.get_id()]){
if(queueList[tempIter].get_id() < toqueue.get_id()){
//std::cout << "Process " << toqueue.get_id() << " comes before process " << queueList[tempIter] << std::endl;
break;
}
}
}
}
}
queueList.insert(queueList.begin()+tempIter, toqueue);
if(time < 1000 && !preempt){
std::cout << "time " << time << "ms: Process " << processList[i].get_id() << " (tau " << tauTracker[processList[i].get_id()] << "ms) completed I/O; added to ready queue [Q ";
for (unsigned int e = 0; e < queueList.size(); e++){
std::cout << queueList[e].get_id();
}
std::cout << "]" << std::endl;
}
IOTracker[processList[i].get_id()] += 1;
contextSwitchTime = 0;
contextSwitchTime = (contextSwitch/2) + time;
}
}
if(finished.size() == processList.size() && contextSwitchTime <= time){
waitTimes = procWaitTime;
cout << "time " << time << "ms: Simulator ended for SRT [Q empty]\n";
cout << endl;
break;
}
time++;
totalTime++;
if(cpu == 1){
CPUTime++;
}
if (time > 77150) {
break;
}
}
}
int SRT::getNumContextSwitches(){
return contextSwitchTracker;
}
double SRT::getCPUUtilization(){
//cout << "CPU time is RT" << CPUTime << " and totalTime is " << totalTime << endl;
double output = round((CPUTime/totalTime)*100*1000)/1000;
return output;
}
double SRT::getWaitTime(){
double sum = 0.0;
for( const auto &pair : waitTimes ){
sum = sum + pair.second;
}
return round((sum/waitTimes.size())*1000)/1000;
}