-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process.h
495 lines (429 loc) · 10.1 KB
/
Process.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
class Process_queue
{
public:
int time;
bool isInputBurst;
bool isInInputQueue = false;
bool isOutputBurst;
bool isInOutputQueue = false;
bool isCpuBurst;
bool isIn_CPU_Queue = false;
};
class Process
{
private:
int pid;
int arrivalTime;
int priority;
int share;
int exitTime = -1;
public:
int totalBurstTime = 0;
int remainingTime;
int responseTime;
int completionTime;
int executionStartTime = -1;
queue<Process_queue> burst_queue;
int getTurnAroundTime()
{
// cout<<this->exitTime<<"-"<<this->arrivalTime<<endl;
return this->exitTime - this->arrivalTime;
}
int getWaitingTime()
{
return this->getTurnAroundTime() - this->totalBurstTime + 1;
}
// TO DO
int getResponseTime()
{
return (this->executionStartTime - this->arrivalTime);
}
void setExecutionStartTime(int t)
{
this->executionStartTime = t;
}
void setExitTime(int exitTime)
{
this->exitTime = exitTime;
}
void setPid(int pid)
{
this->pid = pid;
}
void setArrivalTime(int arrivalTime)
{
this->arrivalTime = arrivalTime;
}
void setPriority(int priority)
{
this->priority = priority;
}
void setShare(int share)
{
this->share = share;
}
int getPid()
{
return this->pid;
}
int getArrivalTime()
{
return this->arrivalTime;
}
int getExitTime()
{
return this->exitTime;
}
int getPriority()
{
return this->priority;
}
int getShare()
{
return this->share;
}
Process()
{
}
};
/*
TODO : add support for showing output
*/
class Chart
{
public:
int value = -1;
bool isCpu = false;
bool isInput = false;
bool isOutput = false;
};
void printGanttChart(queue<int> cpu, queue<int> input, queue<int> output, int size, int time)
{
for (int i = 0; i <= time; i++)
{
cout << i << " ";
}
cout << endl;
Chart chart[size][time + 1];
for (int j = 0; j <= time; j++)
{
for (int i = 0; i < size; i++)
{
if (cpu.size() > 0 && cpu.front() == i + 1)
{
chart[i][j].value = cpu.front();
chart[i][j].isCpu = true;
}
if (input.size() > 0 && input.front() == i + 1)
{
chart[i][j].value = input.front();
chart[i][j].isInput = true;
}
if (output.size() > 0 && output.front() == i + 1)
{
chart[i][j].value = output.front();
chart[i][j].isOutput = true;
}
}
if (cpu.size() > 0)
cpu.pop();
if (input.size() > 0)
input.pop();
if (output.size() > 0)
output.pop();
}
for (int i = 0; i < size; i++)
{
string str = " ";
for (int j = 0; j < time; j++)
{
if (chart[i][j].value < 0)
{
cout << str;
}
if (chart[i][j].isCpu)
{
// red
cout << "\033[1;41mp" << chart[i][j].value << "\033[0m ";
// cout << "p" << chart[i][j].value << " ";
//str = "W ";
}
else if (chart[i][j].isInput)
{
// yellow
cout << "\033[1;43mI" << chart[i][j].value << "\033[0m ";
// cout << "I" << chart[i][j].value << " ";
}
else if (chart[i][j].isOutput)
{
// cyan
cout << "\033[1;46mO" << chart[i][j].value << "\033[0m ";
// cout << "O" << chart[i][j].value << " ";
}
if (j >= 10)
cout << " ";
}
cout << endl;
}
cout << endl;
}
void printChart(queue<int> q, string type)
{
for (int i = 0; i < q.size(); i++)
{
cout << i << " ";
}
cout << endl;
int size = q.size();
for (int i = 0; i < size; i++)
{
if (q.front() != -1)
{
if (type == "process")
cout << "\033[1;41mp" << q.front() << "\033[0m ";
else if (type == "input")
cout << "\033[1;43mI" << q.front() << "\033[0m ";
else
cout << "\033[1;46mO" << q.front() << "\033[0m ";
}
else
{
cout << " ";
}
if (i >= 10)
{
cout << " ";
}
q.pop();
}
}
void printTurnAroundTime(vector<Process> process_vector)
{
int total = 0;
for (int j = 0; j < process_vector.size(); j++)
{
int i = process_vector[j].getPid() - 1;
cout << "P" << j + 1 << " turn around time : " << process_vector[i].getTurnAroundTime() << endl;
total += process_vector[i].getTurnAroundTime();
}
cout << " total turn around time :" << total << endl
<< fixed << setprecision(2);
;
int size = process_vector.size();
cout << " average turn around time :" << (float)total / (float)size << endl;
}
void printWaitingTime(vector<Process> process_vector)
{
int total = 0;
for (int j = 0; j < process_vector.size(); j++)
{
int i = process_vector[j].getPid() - 1;
cout << "P" << j + 1 << " waiting time : " << process_vector[i].getWaitingTime() << endl;
total += process_vector[i].getWaitingTime();
}
cout << " total waiting time :" << total << endl
<< fixed << setprecision(2);
int size = process_vector.size();
cout << " average waiting time :" << (float)total / (float)size << endl;
}
void printResponseTime(vector<Process> process_vector)
{
int total = 0;
for (int j = 0; j < process_vector.size(); j++)
{
int i = process_vector[j].getPid() - 1;
cout << "P" << j + 1 << " response time : " << process_vector[i].getResponseTime() << endl;
total += process_vector[i].getResponseTime();
}
cout << " total response time :" << total << endl
<< fixed << setprecision(2);
int size = process_vector.size();
cout << " average response time :" << (float)total / (float)size << endl;
}
void printInput(vector<Process> process_vector)
{
for (int i = 0; i < process_vector.size(); i++)
{
cout << process_vector[i].getPid() << " " << process_vector[i].getArrivalTime() << " " << process_vector[i].getPriority() << " " << process_vector[i].getShare();
while (process_vector[i].burst_queue.size() > 0)
{
if (process_vector[i].burst_queue.front().isCpuBurst)
cout << " C ";
else
{
cout << " I/O ";
}
cout << process_vector[i].burst_queue.front().time;
process_vector[i].burst_queue.pop();
}
cout << endl;
}
}
bool haveAllProcessesExecuted(vector<Process> &process_vector)
{
for (int i = 0; i < process_vector.size(); i++)
{
if (process_vector[i].burst_queue.size() > 0)
{
return false;
}
}
return true;
}
void setExitTime(vector<Process> &process_vector, int time)
{
for (int i = 0; i < process_vector.size(); i++)
{
if ((process_vector[i].burst_queue.size() == 0) && (process_vector[i].getExitTime() == -1))
process_vector[i].setExitTime(time);
}
}
class Io
{
private:
bool isInputBurst;
queue<int> io_queue; // pid
void decrementBurst(int pid, vector<Process> &process_vector)
{
for (int i = 0; i < process_vector.size(); i++)
{
if (process_vector[i].getPid() == pid)
{
process_vector[i].burst_queue.front().time -= 1;
//if burst has become zero pop from io_queue and from burst_queue
if (process_vector[i].burst_queue.front().time <= 0)
{
process_vector[i].burst_queue.pop();
this->io_queue.pop();
}
break;
}
}
}
public:
queue<int> gantt_io_queue;
Io(string str)
{
if (str == "Input")
isInputBurst = true;
else
isInputBurst = false;
}
void execute_IO(vector<Process> &process_vector, int time)
{
// find processes waiting for input/output & add them to io_queue
for (int i = 0; i < process_vector.size(); i++)
{
// handle condition when burst queue is empty
if (process_vector[i].burst_queue.size() == 0)
continue;
if (isInputBurst)
if (process_vector[i].burst_queue.front().isInputBurst && process_vector[i].burst_queue.front().isInInputQueue == false && process_vector[i].getArrivalTime() <= time)
{
this->io_queue.push(process_vector[i].getPid());
process_vector[i].burst_queue.front().isInInputQueue = true;
if (process_vector[i].executionStartTime == -1)
process_vector[i].setExecutionStartTime(time);
}
if (!isInputBurst)
if (process_vector[i].burst_queue.front().isOutputBurst && process_vector[i].burst_queue.front().isInOutputQueue == false && process_vector[i].getArrivalTime() <= time)
{
this->io_queue.push(process_vector[i].getPid());
process_vector[i].burst_queue.front().isInOutputQueue = true;
if (process_vector[i].executionStartTime == -1)
process_vector[i].setExecutionStartTime(time);
}
}
if (this->io_queue.size() == 0)
{
this->gantt_io_queue.push(-1);
return;
}
// extract process from io_queue and decrement its input
int pid = this->io_queue.front();
this->decrementBurst(pid, process_vector);
this->gantt_io_queue.push(pid);
}
};
// if no process is executing push -1 into g_queue
// public execute_IO(){io_queue.front().burstTime--; g_queue.push(io_queue.front().pid) };
void handleInput(vector<Process> &process_vector, ifstream &file)
{
int text;
file >> text;
int processes = text;
while (processes--)
{
Process process;
file >> text;
process.setPid(text);
file >> text;
process.setArrivalTime(text);
file >> text;
process.setPriority(text);
file >> text;
process.setShare(text);
string str;
Process_queue pq;
while (file >> str)
{
if (str == "-1")
break;
else
{
file >> text;
pq.time = text;
process.totalBurstTime += text;
if (str == "C")
{
pq.isCpuBurst = true;
pq.isInputBurst = false;
pq.isOutputBurst = false;
}
else if (str == "I")
{
pq.isCpuBurst = false;
pq.isInputBurst = true;
pq.isOutputBurst = false;
}
else if (str == "O")
{
pq.isCpuBurst = false;
pq.isInputBurst = false;
pq.isOutputBurst = true;
}
process.burst_queue.push(pq);
}
}
process_vector.push_back(process);
}
}
void handleOutput(queue<int> &gantt_cpu_queue, queue<int> &input_queue,
queue<int> &output_queue, vector<Process> &process_vector, int time)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// pass inputOutput as argument to print gantt
cout << SCHEDULER << "\n";
cout << "Gantt Chart\n";
printGanttChart(gantt_cpu_queue, input_queue, output_queue, process_vector.size(), time);
cout << "CPU Chart :\n";
printChart(gantt_cpu_queue, "process");
cout << endl;
cout << "\nInput Chart :\n";
printChart(input_queue, "input");
cout << endl;
cout << "\nOutput Chart :\n";
printChart(output_queue, "output");
cout << endl;
// output turn around time
cout << endl;
printTurnAroundTime(process_vector);
cout << endl;
// output waiting time
printWaitingTime(process_vector);
cout << endl;
// output response time
printResponseTime(process_vector);
cout << endl;
}