forked from michaelschuff/Sum23OpSysProj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCFSCPU.h
429 lines (356 loc) · 11.1 KB
/
FCFSCPU.h
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#ifndef FCFSCPU_H
#define FCFSCPU_H
#include "FCFSprocess.h"
#include <math.h>
#include <numeric>
#include <climits>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fstream>
extern unsigned long CUTOFF;
class FCFSCPU {
public:
//INFO
vector<FCFSProcess*> processes;
int ctxSwitchTime;
//STATE
unsigned long time=0;
int readyQCounter = 0;
//LOCATIONS
priority_queue<FCFSProcess*,vector<FCFSProcess*>,FCFSArrivalTimeCompare> incoming;
priority_queue<FCFSProcess*,vector<FCFSProcess*>,FCFSCompare> readyQ;
priority_queue<FCFSProcess*,vector<FCFSProcess*>,FCFSIOBurstTimeCompare> IOBursts;
FCFSProcess* cpu = NULL;
FCFSProcess* cpuOut = NULL;
int ctxOutTime = INT_MAX;
FCFSProcess* cpuIn = NULL;
int ctxInTime = INT_MAX;
//METRICS
int numIOCTXSwitches = 0;
int numCPUCTXSwitches = 0;
int numCPUBoundProcesses = 0;
int numIOBoundProcesses = 0;
int cpuRunning = 0;
FCFSCPU(vector<FCFSProcess*> procs, int switchTime) {
ctxSwitchTime = switchTime;
for (size_t i = 0; i < procs.size(); i++) {
incoming.push(procs[i]);
if (procs[i]->isCPUBound)
numCPUBoundProcesses+=procs[i]->totalCPUBursts;
else
numIOBoundProcesses+=procs[i]->totalCPUBursts;
}
processes = procs;
}
int getNextEvent() {
if (cpu == NULL && ctxOutTime == INT_MAX && ctxInTime == INT_MAX && !readyQ.empty()) {
int flag = 5;
return flag;
}
int CPU_TIME = INT_MAX;
if (cpu != NULL && ctxOutTime == INT_MAX)
CPU_TIME = cpu->nextFinish();
int CPU_OUT_TIME = INT_MAX;
if (ctxOutTime != INT_MAX)
CPU_OUT_TIME = ctxOutTime;
int CPU_IN_TIME = INT_MAX;
if (ctxInTime != INT_MAX)
CPU_IN_TIME = ctxInTime;
int IO_FINISH = INT_MAX;
if (!IOBursts.empty())
IO_FINISH = IOBursts.top()->nextFinish();
int INCOMING_FINISH = INT_MAX;
if (!incoming.empty())
INCOMING_FINISH = incoming.top()->arrivalTime;
int CPU_HALF_IN = INT_MAX;
if (ctxInTime != INT_MAX && cpuIn == NULL)
CPU_HALF_IN = ctxInTime - ceil((ctxSwitchTime / 2) / 2.0);
int CPU_HALF_OUT = INT_MAX;
if (ctxOutTime != INT_MAX && cpuOut == NULL)
CPU_HALF_OUT = ctxOutTime - ceil((ctxSwitchTime / 2) / 2.0);
int min = INT_MAX;
int flag = -1;
if (CPU_HALF_OUT < min) {
min = CPU_HALF_OUT;
flag = -3;
}
if (CPU_HALF_IN < min) {
min = CPU_HALF_IN;
flag = -2;
}
if (CPU_TIME < min) {
min = CPU_TIME;
flag = 0;
}
if (CPU_OUT_TIME < min) {
min = CPU_OUT_TIME;
flag = 1;
}
if (CPU_IN_TIME < min) {
min = CPU_IN_TIME;
flag = 2;
}
if (IO_FINISH < min) {
min = IO_FINISH;
flag = 3;
}
if (INCOMING_FINISH < min) {
min = INCOMING_FINISH;
flag = 4;
}
return flag;
}
void run() {
printf("time %ldms: Simulator started for FCFS ",time);
printReady();
while (!readyQ.empty() || !incoming.empty() || !IOBursts.empty() || cpu!=NULL || cpuOut != NULL || cpuIn != NULL) {
int flag = getNextEvent();
// printf("flag: %d\n",flag);
if (flag == -3) {
elapseTime(ctxOutTime - ceil((ctxSwitchTime / 2) / 2.0),flag);
cpuOut = cpu;
cpu = NULL;
if (cpuOut->isCPUBound)
numCPUCTXSwitches++;
else
numIOCTXSwitches++;
} else if (flag == -2) {
elapseTime(ctxInTime - ceil((ctxSwitchTime / 2) / 2.0),flag);
cpuIn = readyQ.top();
readyQ.pop();
} else if (flag == 0) {
// CPU FINISH
elapseTime(cpu->nextFinish(),flag);
// //UPDATE METRICS
// if (cpu->isCPUBound)
// numCPUCTXSwitches++;
// else
// numIOCTXSwitches++;
ctxOutTime = ctxSwitchTime/2;
if (cpu->shouldTerminate()) {
printTime();
printf("Process %c terminated ", idtoc(cpu->ID));
printReady();
} else {
if (time < CUTOFF) printTime();
if (time < CUTOFF) printf("Process %c completed a CPU burst; %d burst%s to go ", idtoc(cpu->ID), cpu->totalCPUBursts - cpu->completedCPUBursts, cpu->totalCPUBursts - cpu->completedCPUBursts == 1 ? "" : "s");
if (time < CUTOFF) printReady();
if (time < CUTOFF) printTime();
if (time < CUTOFF) printf("Process %c switching out of CPU; blocking on I/O until time %ldms ", idtoc(cpu->ID), cpu->nextFinish() + time + ctxSwitchTime/2);
if (time < CUTOFF) printReady();
}
}
else if (flag == 1) {
elapseTime(ctxOutTime,flag);
if (!cpuOut->shouldTerminate())
IOBursts.push(cpuOut);
ctxOutTime = INT_MAX;
cpuOut = NULL;
}
else if (flag == 2) {
elapseTime(ctxInTime,flag);
cpu = cpuIn;
cpuIn = NULL;
ctxInTime = INT_MAX;
cpu->priority = 0;
if (time < CUTOFF) printTime();
if (time < CUTOFF) printf("Process %c started using the CPU for %dms burst ", idtoc(cpu->ID), cpu->nextFinish());
if (time < CUTOFF) printReady();
}
else if (flag == 3) {
FCFSProcess* p = IOBursts.top();
int t = p->nextFinish();
IOBursts.pop();
p->elapseTime(t,flag);
elapseTime(t,flag);
p->priority = readyQCounter;
readyQCounter++;
readyQ.push(p);
if (time < CUTOFF) printTime();
if (time < CUTOFF) printf("Process %c completed I/O; added to ready queue ", idtoc(p->ID));
if (time < CUTOFF) printReady();
}
else if (flag == 4) {
FCFSProcess* p = incoming.top();
incoming.pop();
int t = p->arrivalTime;
p->elapseTime(t,flag);
elapseTime(t,flag);
p->priority = readyQCounter;
readyQCounter++;
readyQ.push(p);
if (time < CUTOFF) printTime();
if (time < CUTOFF) printf("Process %c arrived; added to ready queue ", idtoc(p->ID));
if (time < CUTOFF) printReady();
}
else if (flag == 5) {
ctxInTime = ctxSwitchTime / 2;
}
}
printTime();
printf("Simulator ended for FCFS ");
printReady();
long CPUBOUND_cpu_burst_time = 0;
long IOBOUND_cpu_burst_time = 0;
for (size_t i = 0; i < processes.size(); i++) {
if (processes[i]->isCPUBound)
CPUBOUND_cpu_burst_time += processes[i]->time_using_cpu;
else
IOBOUND_cpu_burst_time += processes[i]->time_using_cpu;
}
long CPU_turnaround = 0;
long IO_turnaround = 0;
for (size_t i = 0; i < processes.size(); i++) {
if (processes[i]->isCPUBound)
CPU_turnaround += processes[i]->total_turnaround_time;
else
IO_turnaround += processes[i]->total_turnaround_time;
}
long CPU_wait = 0;
long IO_wait = 0;
for (size_t i = 0; i < processes.size(); i++) {
if (processes[i]->isCPUBound)
CPU_wait += processes[i]->total_wait_time;
else
IO_wait += processes[i]->total_wait_time;
}
double cu = time? (ceil((100.0 * cpuRunning / time)*1000.0))/1000.0 : 0.0;
double cbt1 = (numIOBoundProcesses+numCPUBoundProcesses) ? (ceil(((IOBOUND_cpu_burst_time + CPUBOUND_cpu_burst_time)/(double)(numIOBoundProcesses+numCPUBoundProcesses))*1000.0))/1000.0 : 0.0;
double cbt2 = numCPUBoundProcesses ? (ceil(CPUBOUND_cpu_burst_time/(double)numCPUBoundProcesses*1000.0))/1000.0 : 0.0;
double cbt3 = numIOBoundProcesses ? (ceil(IOBOUND_cpu_burst_time/(double)numIOBoundProcesses*1000.0))/1000.0 : 0.0;
double awt1 = (numIOBoundProcesses+numCPUBoundProcesses) ? ( ceil((CPU_wait + IO_wait)/(double)(numIOBoundProcesses+numCPUBoundProcesses)*1000.0))/1000.0 : 0.0;
double awt2 = numCPUBoundProcesses ? ( ceil(CPU_wait/(double)numCPUBoundProcesses*1000.0))/1000.0 : 0.0;
double awt3 = numIOBoundProcesses ? ( ceil(IO_wait/(double)numIOBoundProcesses*1000.0))/1000.0 : 0.0;
double att1 = (numIOBoundProcesses+numCPUBoundProcesses) ? ( ceil((CPU_turnaround + IO_turnaround)/(double)(numIOBoundProcesses+numCPUBoundProcesses)*1000.0))/1000.0 : 0.0;
double att2 = numCPUBoundProcesses ? ( ceil(CPU_turnaround/(double)numCPUBoundProcesses*1000.0))/1000.0 : 0.0;
double att3 = numIOBoundProcesses ? ( ceil(IO_turnaround/(double)numIOBoundProcesses*1000.0))/1000.0 : 0.0;
ofstream output;
output.open("simout.txt", ios::out | ios::trunc);
output.setf(ios::fixed,ios::floatfield);
output.precision(3);
output << "Algorithm FCFS\n";
output << "-- CPU utilization: " << cu << "%\n";
output << "-- average CPU burst time: " << cbt1 << " ms (" << cbt2 << " ms/" << cbt3 << " ms)\n";
output << "-- average wait time: " << awt1 << " ms (" << awt2 << " ms/" << awt3 << " ms)\n";
output << "-- average turnaround time: " << att1 << " ms (" << att2 << " ms/" << att3 << " ms)\n";
output << "-- number of context switches: " << numIOCTXSwitches+numCPUCTXSwitches << " (" << numCPUCTXSwitches << "/" << numIOCTXSwitches << ")\n";
output << "-- number of preemptions: 0 (0/0)\n\n";
output.close();
}
void elapseTime(int t, int flag) {
time += t;
elapseTimeCPU(t, flag);
elapseTimeIO(t, flag);
elapseTimeIncoming(t, flag);
elapseWaitTimeReady(t);
if (cpu != NULL && ctxOutTime == INT_MAX)
cpuRunning += t;
elapseTurnaroundTime(t);
}
void elapseTimeIO(int t, int flag) {
vector<FCFSProcess*> procs;
int ID = -1;
if (!readyQ.empty())
ID = readyQ.top()->ID;
while (!IOBursts.empty()) {
FCFSProcess* p = IOBursts.top();
IOBursts.pop();
p->elapseTime(t,ID == p->ID ? flag : -1);
procs.push_back(p);
}
for (size_t i = 0; i < procs.size(); i++) {
IOBursts.push(procs[i]);
}
}
void elapseTimeIncoming(int t, int flag) {
vector<FCFSProcess*> procs;
while (!incoming.empty()) {
FCFSProcess* p = incoming.top();
incoming.pop();
p->elapseTime(t,flag);
procs.push_back(p);
}
for (size_t i = 0; i < procs.size(); i++) {
incoming.push(procs[i]);
}
}
void elapseTimeCPU(int t, int flag) {
if (ctxOutTime != INT_MAX)
ctxOutTime -= t;
if (cpu != NULL && ctxOutTime == INT_MAX) {
cpu->elapseTime(t,flag);
cpu->time_using_cpu += t;
}
if (ctxInTime != INT_MAX)
ctxInTime -= t;
}
void elapseWaitTimeReady(int t) {
vector<FCFSProcess*> procs;
int ID = -1;
if (!readyQ.empty() && ctxInTime!= INT_MAX && cpuIn == NULL)
ID = readyQ.top()->ID;
while (!readyQ.empty()) {
FCFSProcess* p = readyQ.top();
readyQ.pop();
if (p->ID != ID)
p->elapseWaitTime(t);
procs.push_back(p);
}
for (size_t i = 0; i < procs.size(); i++) {
readyQ.push(procs[i]);
}
}
void elapseTurnaroundTime(int t) {
vector<FCFSProcess*> procs;
while (!readyQ.empty()) {
FCFSProcess* p = readyQ.top();
readyQ.pop();
p->elapseTurnaroundTime(t);
procs.push_back(p);
}
for (size_t i = 0; i < procs.size(); i++) {
readyQ.push(procs[i]);
}
if (cpuOut != NULL)
cpuOut->elapseTurnaroundTime(t);
if (cpu != NULL) {
cpu->elapseTurnaroundTime(t);
}
if (cpuIn != NULL)
cpuIn->elapseTurnaroundTime(t);
}
void printTime() {
printf("time %ldms: ", time);
}
void printReady() {
priority_queue<FCFSProcess*,vector<FCFSProcess*>,FCFSCompare> copy = readyQ;
if (copy.empty()) {
printf("[Q <empty>]\n");
} else {
printf("[Q ");
while (!copy.empty()) {
if (copy.size() != 1)
printf("%c ",idtoc(copy.top()->ID));
else
printf("%c",idtoc(copy.top()->ID));
copy.pop();
}
printf("]\n");
}
}
template<class S>
void printQueue(priority_queue<FCFSProcess*, vector<FCFSProcess*>, S> queue) {
priority_queue<FCFSProcess*,vector<FCFSProcess*>,S> copy = queue;
while (!copy.empty()) {
printf("%c ",idtoc(copy.top()->ID));
copy.pop();
}
printf("\n");
}
};
#endif