-
Notifications
You must be signed in to change notification settings - Fork 0
/
processManager.cpp
1037 lines (721 loc) · 25.4 KB
/
processManager.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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Adrian Barberis
// Process Manager
// Last Edit: Wed. FEB 22, 2017
/*----------------------------- PREPROCESSORS -----------------------------*/
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <math.h>
#include <string>
#include <vector>
#include "ProcessManager.h"
#define READ_END 0
#define WRITE_END 1
#define RESOURCE_1 0
#define RESOURCE_2 1
#define RESOURCE_3 2
#define PRIORITY_LEVELS 4
/*--------------------------- GLOBALS ----------------------------------*/
//Number of processes that have finished
int numFinished = 0;
//Total time_ taken by all finished processes
int finishedTime = 0;
//Total number of processes
int numProcesses = 0;
/*--------------------------- BEGIN MAIN ----------------------------------*/
// A return value of 0 == All is Well
// A return value of 1 == something whent wrong
int main(int argc, char** argv)
{
//pipes
int cmdp[2];
//convert argv to commander pipe values
cmdp[READ_END] = atoi(argv[1]);
cmdp[WRITE_END] = atoi(argv[2]);
//used to read in function call from pipe
char function;
//used for error check
int status;
//Stuff
Time time_;
PCBTable pcbt;
BlockedState bstate;
ReadyState restate;
RunningState rustate;
// Give initital values to the parameters in the various structs
initialize(&time_, &rustate, &restate, &bstate);
//Close unused pipe ends
close(cmdp[WRITE_END]);
// Begin read loop
while(true)
{
//read in which function to execute
status = read(cmdp[READ_END], &function, sizeof(char));
//check for failure or eof
if(status == 0 || status == -1)
{
if(status == 0)
{
printf("PRMAN: Reached End of File.\nExiting...\n");
return 0;
}
else
{
printf("PRMAN: Read Error!\nExiting...\n");
return 1;
}
}
// Do stuff based on the char read into 'funtion'
switch(function)
{
case 'S': // Make a new process and add it to the PCB table
{
start(cmdp, &pcbt, &rustate, &restate, &time_);
break;
}
case 'B':
{
// Read in the resource ID that you want to block for
int rid = 0;
read(cmdp[READ_END], &rid, sizeof(int));
// Set the _resourceID value in the BlockedState struct equal to,
// the rid you read in
bstate._resourceID = rid;
// Block
block(&rustate, &pcbt, &bstate, &restate, rid);
break;
}
case 'U':
{
// Read in the resource ID that you want to unblock for
int rid = 0;
read(cmdp[READ_END], &rid, sizeof(int));
// Unblock
unblock(&bstate, &restate, &pcbt, &rustate, rid);
break;
}
case 'C':
{
// Change the value of the current process based on a specific command
calculate(cmdp, &rustate, &pcbt, &restate, &time_);
break;
}
case 'P':
{
// This method forks a child and the child calls the reporter() method
// Pretty standard stuff here
int p = fork();
if( p > 0)
{
// Wait for the child to finish
wait(NULL);
}
else if( p == 0)
{
reporter(&pcbt, &time_, &rustate, &bstate, &restate);
return 0;
}
else
{
std::cerr << "Fork Failed\n" << std::endl;
return 1;
}
break;
}
case 'Q':
{
// Run the Scheduler
scheduler(&time_, &rustate, &pcbt, &restate);
break;
}
case 'T':
{
// Calculate the AVERAGE turnaround time_
turnaround(&pcbt);
return 0;
}
}
}
// close the pipe read end and quit
close(cmdp[READ_END]);
return 0;
}
/* END MAIN */
/* ------------------------------------- FUNCTION DEFINITIONS -------------------------------------*/
/* START */
void start(int* cmdp, PCBTable *pcbt, RunningState *rustate, ReadyState *restate, Time *time_)
{
PCB process;
// There are only 3 parameters for the S function ergo read only 3 times
for(int i=0; i<3; i++)
{
if(i == 0)
{
// The first parameter read is the PID
int rs = read(cmdp[READ_END], &process._pid, sizeof(int));
if(rs < 0)
{
fprintf(stderr, "ERROR: Start process read error!\n");
return;
}
}
else if(i == 1)
{
// The second parameter read is the VALUE
int rs = read(cmdp[READ_END], &process._value, sizeof(int));
if(rs < 0)
{
fprintf(stderr, "ERROR: Start process read error!\n");
return;
}
}
else if(i == 2)
{
// The last parameter read is the RUN TIME
int rs = read(cmdp[READ_END], &process._run_time, sizeof(int));
if(rs < 0)
{
fprintf(stderr, "ERROR: Start process read error!\n");
return;
}
}
}
// Initialize the rest of the PCB
process._start_time = time_->_time;
process._quantum = 1;
process._qcounter = 0;
process._priority = 0;
process._cpu_time = 0;
process._turnaround = 0;
// push the newly made process onto the PCB Table
pcbt->_pcb_table.push_back(process);
// If there is no running process make the,
// newly created process the running process
if(!rustate->_hasrunning)
{
rustate->_process = process;
rustate->_hasrunning = true;
}
else
{
// If a running process exists,
// add the newly created process to the ready queue
restate->_queue->Enqueue(process._pid, process._priority);
}
// Increase the total process count by 1
numProcesses++;
}
//==================================================================================================||
/* CALCULATE */
void calculate(int* cmdp, RunningState *rustate, PCBTable *pcbt, ReadyState *restate, Time *time_)
{
// The C method has only 2 parameters a Char which is the command to be executed and,
// A number which is the value to be used in the command that we need to execute
// Read in only 2 parameters. Read the first as sizeof(char) and the second as sizeof(int)
char cmd;
int num;
for(int i=0; i<2; i++)
{
if(i == 0){ read(cmdp[READ_END], &cmd, sizeof(char)); }
else if(i == 1){ read(cmdp[READ_END], &num, sizeof(int)); }
}
// Switch based on the command (cmd) read in and do the appropriate arithmetic operation
switch(cmd)
{
case 'A':
{
// Add Num to the current process' _value, then store it back into _value
rustate->_process._value += num;
break;
}
case 'S':
{
// Subtract Num from the current process' _value, then store it back into _value
rustate->_process._value -= num;
break;
}
case 'M':
{
// Multiply the current process' _value by Num, then store it back into _value
rustate->_process._value *= num;
break;
}
case 'D':
{
if(num == 0)
{
// If there is a division by zero error say so and continue without changing the value
// this could also become a return statement rather than a break.
printf("PRMAN: Divide by Zero Error!\n");
break;
}
// Divide the current process' _value by Num, then store it back into _value
rustate->_process._value /= num;
break;
}
}
// Update the PCB Table
pcbt->_pcb_table[rustate->_process._pid-1] = rustate->_process;
// Make the Sheduler do some work since you "Used the CPU"
scheduler(time_, rustate, pcbt, restate);
}
//==================================================================================================||
/* BLOCK */
void block(RunningState *rustate, PCBTable *pcbt, BlockedState *bstate, ReadyState *restate, int rid)
{
// Need to differentiate between wether there is a a currently running process or not
if(rustate->_hasrunning)
{
// If there is a process currently running:
// Store the current PID for later use
int cpid = rustate->_process._pid;
// Get the rid you want to block for
//int rid = bstate->_resourceID;
// IMPORTANT NOTE: The RunningState struct stores a full on PCB object not just a PID
// This I beleive makes it easier to get accurate information without having to
// continually access the PCB Table. Note to that I don't store multiple copies,
// which would bloat things instead I pull a PCB from the PCB Table and set the RunningState,
// PCB equal to that. I then do the reverse when I want to update the PCB Table
// Thus I have a relatively constant amount of PCB objects.
// Zero out the current process' quantum counter
// Decrease the current process' priority by 1;
rustate->_process._qcounter = 0;
rustate->_process._priority -= 1;
// If the current process' priority is < 0 set it to 0
if(rustate->_process._priority < 0){ rustate->_process._priority = 0; }
//Decrease the quantum
rustate->_process._quantum = pow(2, rustate->_process._priority);
if(rustate->_process._quantum < 1){ rustate->_process._quantum = 1;}
// //Update the PCB Table
pcbt->_pcb_table[cpid-1] = rustate->_process;
// Based on the resource ID enqueue the current process PID onto,
// the appropriate resource queue. The location of the Enqueue() is given,
// by the current process' priority.
// E.G.: priority 0 = the queue at resource 1 array position 0
if(rid == RESOURCE_1)
{
bstate->_resource1->Enqueue(cpid, rustate->_process._priority);
}
else if( rid == RESOURCE_2)
{
bstate->_resource2->Enqueue(cpid, rustate->_process._priority);
}
else if(rid == RESOURCE_3)
{
bstate->_resource3->Enqueue(cpid, rustate->_process._priority);
}
// Once youve taken something off of the CPU you need to put something back on
// If the ready queue has something in it:
// 1. Dequeue to get the PID
// 2. Access the PCB Table at location [PID - 1] (Array/vector indecies start at 0)
// 3. Set the RunningState PCB object equal to the PCB object at PCB Table location [PID - 1]
if(!restate->_queue->isEmpty())
{
rustate->_process = pcbt->_pcb_table[(restate->_queue->Dequeue())-1];
}
else
{
// If there is nothing in the ready queue:
// Zero out the RunningState
//rustate->_hasrunning = false;
}
}
else
{
// If there is no process currently running then you can't actually block anything
fprintf(stderr, "No Running Process; Nothing to Block!\n");
return;
}
}
//==================================================================================================||
/* UNBLOCK */
void unblock(BlockedState *bstate, ReadyState *restate, PCBTable *pcbt, RunningState *rustate, int rid)
{
// Need to differentiate between wether there is a a currently running process or not
if(rustate->_hasrunning == true)
{
// If there is a process currently running:
// Move a process from the BlockedState queue to the ReadyState queue based,
// on the resource ID that you read in
// Note that we also check to make sure that there actually IS something in the BlockedState queue
// For us to unblock
// We enqueue at location equal to the dequeued PID's priority
// Location of the PID's info in the PCB Table is at [PID -1] (array/vector indicies start at 0)
if(rid == RESOURCE_1 && !bstate->_resource1->isEmpty())
{
int pid = bstate->_resource1->Dequeue();
restate->_queue->Enqueue(pid, pcbt->_pcb_table[pid-1]._priority);
}
else if(rid == RESOURCE_2 && !bstate->_resource2->isEmpty())
{
int pid = bstate->_resource2->Dequeue();
restate->_queue->Enqueue(pid, pcbt->_pcb_table[pid-1]._priority);
}
else if(rid == RESOURCE_3 && !bstate->_resource3->isEmpty())
{
int pid = bstate->_resource3->Dequeue();
restate->_queue->Enqueue(pid, pcbt->_pcb_table[pid-1]._priority);
}
else
{
// If the BlockedState is empty say so and return
fprintf(stderr, "A: Block State is empty; There is nothing to unblock!\n");
return;
}
}
else if(rustate->_hasrunning == false)
{
// If there is nothing running rather than move the unblocked process to the ReadyQueue,
// we put it directly onto the CPU
// Move a process from the BlockedState to the CPU:
// 1. Make sure to check that the resource from which you are trying to unblock is NOT empty
// 2. Get the PID by dequeueing from the required resource queue
// 3. Set the currently running process PCB equal to the PCB at location [PID-1] in the PCB Table
// 4. Set the running state flag to true
if(rid == RESOURCE_1 && !bstate->_resource1->isEmpty())
{
int pid = bstate->_resource1->Dequeue();
rustate->_process = pcbt->_pcb_table[pid-1];
rustate->_hasrunning = true;
}
else if(rid == RESOURCE_2 && !bstate->_resource2->isEmpty())
{
int pid = bstate->_resource2->Dequeue();
rustate->_process = pcbt->_pcb_table[pid-1];
rustate->_hasrunning = true;
}
else if(rid == RESOURCE_3 && !bstate->_resource3->isEmpty())
{
int pid = bstate->_resource3->Dequeue();
rustate->_process = pcbt->_pcb_table[pid-1];
rustate->_hasrunning = true;
}
else
{
// If the BlockedState is empty say so and return
fprintf(stderr, "B: Block State is empty; There is nothing to unblock!\n");
return;
}
}
else
{
// If this EVER triggers something whent seriously wrong
fprintf(stderr, "Something whent wrong in the Unblocker!\n");
return;
}
}
//==================================================================================================||
/* QUANTUM */
void scheduler(Time *time_, RunningState *rustate, PCBTable *pcbt, ReadyState *restate)
{
// At the very least the scheduler should increment the time_
time_->_time += 1;
// If there is nothing running and nothing in the ready queue then,
// say so, increment the time_( above ), and return
if(!rustate->_hasrunning && restate->_queue->isEmpty())
{
fprintf(stderr, "No running or ready processes!\n");
return;
}
else if(!rustate->_hasrunning && !restate->_queue->isEmpty())
{
// If there is nothing running but there is something in the reaady queue:
// 1. get the PID by Dequeueing from the ready queue
// 2. get the PCB from the PCB Table at location [PID - 1]
// 3. Set the RunningState PCB object equal to the PCB you got from the PCB Table
// 4. Set the boolean "there is something running" flag to true
int pid = restate->_queue->Dequeue();
rustate->_process = pcbt->_pcb_table[pid-1];
rustate->_hasrunning = true;
return;
}
else
{
// This is the normal functionality section
// Get the PID of the currently running process for later use
int cpid = rustate->_process._pid;
// Increment the current process' quantum counter as well as cpu time_ used so far
rustate->_process._qcounter += 1;
rustate->_process._cpu_time += 1;
// Update the PCB Table
pcbt->_pcb_table[cpid-1] = rustate->_process;
if(rustate->_process._cpu_time == rustate->_process._run_time)
{
// This is what happens when a process gets killed ( runs until its runtime )
// Calculate the turnaround time_
int turnaround = time_->_time - rustate->_process._start_time;
// set the current process' turnaround time_ equal to the one you calculated above
rustate->_process._turnaround = turnaround;
// Update the PCB Table
pcbt->_pcb_table[cpid-1] = rustate->_process;
// a process has died (we killed it, mercylessly, it was very bloody, I laughed...)
// which means that the CPU is empty and we can't have that so if the ready queue is NOT empty:
// 1. Get the PID by Dequeueing from the reaady queue
// 2. Get the PCB object from the PCB Table at location [PID-1]
// 3. Set the currently running's PCB object equal to the PCB you got from the PCB Table
// 4. Set the bollean "yes there is something running" flag to true
if(!restate->_queue->isEmpty())
{
int pid = restate->_queue->Dequeue();
rustate->_process = pcbt->_pcb_table[pid-1];
rustate->_hasrunning = true;
}
else
{
// If there is nothing in the ready queue say so and return
fprintf(stderr, "B: Ready queue is empty; Nothing to put on CPU!\n");
return;
}
numFinished++;
finishedTime += turnaround;
return;
}
else if(rustate->_process._qcounter == rustate->_process._quantum)
{
// If the currently running process has used up its quantum AND,
// its cpu time_ so far is NOT equal to its total run time_ then do things:
// Increase the currently running process' priority by 1 then if it's greater than 3 set it equal to 3
rustate->_process._priority += 1;
if(rustate->_process._priority > 3){ rustate->_process._priority = 3;}
// Increase the currently running process' quantum by 2x its previous value then,
// if it's greater than 8 set it equal to 8
rustate->_process._quantum = pow(2, rustate->_process._priority);
if(rustate->_process._quantum > 8){ rustate->_process._quantum = 8;}
// Reset the current process' quantum counter
// If this is not done the next time_ the process comes on the CPU it will start from X rather than 0
rustate->_process._qcounter = 0;
// Update the PCB Table
pcbt->_pcb_table[cpid-1] = rustate->_process;
// Push the PID of the currently running process onto the ready queue
// Its location will be equal to the currently running process' priority
restate->_queue->Enqueue(cpid, rustate->_process._priority);
// If the ready queue is NOT empty:
// 1. Get the PID by Dequeueing from the reaady queue
// 2. Get the PCB object from the PCB Table at location [PID-1]
// 3. Set the currently running's PCB object equal to the PCB you got from the PCB Table
// 4. Set the bollean "yes there is something running" flag to true
if(!restate->_queue->isEmpty())
{
int pid = restate->_queue->Dequeue();
rustate->_process = pcbt->_pcb_table[pid-1];
rustate->_hasrunning = true;
return;
}
else
{
// If there is nothing in the ready queue then say so and return
fprintf(stderr, "A: Ready queue is empty; Nothing to put on CPU!\n");
return;
}
}
else{ return; } // If you get here it means the process' quantum is not done and it will simply keep using the CPU
}
}
//==================================================================================================||
/* TURNAROUND */
const void turnaround(PCBTable *pcbt)
{
double total = 0;
for(int i = 0; i < pcbt->_pcb_table.size(); i++)
{
// total up all of the turnaround times
total += pcbt->_pcb_table[i]._turnaround;
}
// We don't want to divide by zero
if(numFinished == 0){ numFinished = 1;}
// Print the total turnaround time_ devided by the number of processes
printf("The Average Turnaround Time Is: %f\n\n", total/numFinished);
printf("Extra stuff you might want to know:\n");
printf("%d processes finished in a total of %d seconds.\n", numFinished, finishedTime);
printf("Total number of processes: %d\n\n", numProcesses);
}
//==================================================================================================||
const void reporter(PCBTable *pcbt, Time *time_, RunningState *rustate, BlockedState *bstate, ReadyState *restate)
{
// This one is big and kind of ugly all it does is print stuff out according to specifications
// Print the current time_
printf("****************************************************************\n");
printf("The current state is as follows:\n");
printf("****************************************************************\n\n");
printf("CURRENT TIME: %d\n\n", time_->_time);
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\\
// Print the currently running process
if(rustate->_hasrunning)
{
printf("RUNNING PROCESS:\n");
printf(" PID PRIORITY VALUE START TIME CPU TIME\n");
printf("< %2d%10d%13d%13d%14d >\n", rustate->_process._pid, rustate->_process._priority,
rustate->_process._value, rustate->_process._start_time, rustate->_process._cpu_time);
}
else
{
// If there is no process currently running say so
printf("RUNNING PROCESS: NONE\n");
}
printf("\n\n");
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\\
// Print all process that are currently blocked for a particular resource ID
// RESOURCE 1 AKA RESOURCE ID = 0
printf("BLOCKED PROCESSES: \n");
if(!bstate->_resource1->isEmpty())
{
printf("Queue of processes blocked for resource 0: \n");
printf(" PID PRIORITY VALUE START TIME CPU TIME\n");
for(int i=0; i<bstate->_resource1->Asize(); i++)
{
int* q = bstate->_resource1->Qstate(i);
int size = bstate->_resource1->Qsize(i);
for(int j = 0; j<size; j++)
{
printf("< %2d%10d%13d%13d%14d >\n", pcbt->_pcb_table[q[j]-1]._pid, pcbt->_pcb_table[q[j]-1]._priority,
pcbt->_pcb_table[q[j]-1]._value, pcbt->_pcb_table[q[j]-1]._start_time, pcbt->_pcb_table[q[j]-1]._cpu_time);
}
}
}
else
{
printf("Queue of processes Blocked for resource 0 is empty: \n");
}
printf("\n");
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\\
//RESOURCE 2 AKA RESOURCE ID = 1
if(!bstate->_resource2->isEmpty())
{
printf("Queue of processes blocked for resource 1: \n");
printf(" PID PRIORITY VALUE START TIME CPU TIME\n");
for(int i=0; i<bstate->_resource2->Asize(); i++)
{
int* q = bstate->_resource2->Qstate(i);
int size = bstate->_resource2->Qsize(i);
for(int j = 0; j<size; j++)
{
printf("< %2d%10d%13d%13d%14d >\n", pcbt->_pcb_table[q[j]-1]._pid, pcbt->_pcb_table[q[j]-1]._priority,
pcbt->_pcb_table[q[j]-1]._value, pcbt->_pcb_table[q[j]-1]._start_time, pcbt->_pcb_table[q[j]-1]._cpu_time);
}
}
}
else
{
printf("Queue of processes Blocked for resource 1 is empty: \n");
}
printf("\n");
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\\
//RESOURCE 3 AKA RESOURCE ID = 2
if(!bstate->_resource3->isEmpty())
{
printf("Queue of processes blocked for resource 2: \n");
printf(" PID PRIORITY VALUE START TIME CPU TIME\n");
for(int i=0; i<bstate->_resource3->Asize(); i++)
{
int* q = bstate->_resource3->Qstate(i);
int size = bstate->_resource3->Qsize(i);
for(int j = 0; j<size; j++)
{
printf("< %2d%10d%13d%13d%14d >\n", pcbt->_pcb_table[q[j]-1]._pid, pcbt->_pcb_table[q[j]-1]._priority,
pcbt->_pcb_table[q[j]-1]._value, pcbt->_pcb_table[q[j]-1]._start_time, pcbt->_pcb_table[q[j]-1]._cpu_time);
}
}
}
else
{
printf("Queue of processes Blocked for resource 2 is empty: \n");
}
printf("\n\n");
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\\
// Print the processes in the Ready Queue
// PRIORITY 0
printf("PROCESSES READY TO EXECUTE: \n");
int* q = restate->_queue->Qstate(0);
int size = restate->_queue->Qsize(0);
if(size == 0)
{
printf("Queue of processes with priority 0 is empty\n");
printf("\n");
}
else
{
printf("Queue of processes with priority 0: \n");
printf(" PID PRIORITY VALUE START TIME CPU TIME\n");
for(int j=0; j<size; j++)
{
printf("< %2d%10d%13d%13d%14d >\n", pcbt->_pcb_table[q[j]-1]._pid, pcbt->_pcb_table[q[j]-1]._priority,
pcbt->_pcb_table[q[j]-1]._value, pcbt->_pcb_table[q[j]-1]._start_time, pcbt->_pcb_table[q[j]-1]._cpu_time);
}
printf("\n");
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\\
//PRIORITY 1
q = restate->_queue->Qstate(1);
size = restate->_queue->Qsize(1);
if(size == 0)
{
printf("Queue of processes with priority 1 is empty\n");
printf("\n");
}
else
{
printf("Queue of processes with priority 1: \n");
printf(" PID PRIORITY VALUE START TIME CPU TIME\n");
for(int j=0; j<size; j++)
{
printf("< %2d%10d%13d%13d%14d >\n", pcbt->_pcb_table[q[j]-1]._pid, pcbt->_pcb_table[q[j]-1]._priority,
pcbt->_pcb_table[q[j]-1]._value, pcbt->_pcb_table[q[j]-1]._start_time, pcbt->_pcb_table[q[j]-1]._cpu_time);
}
printf("\n");
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\\
//PRIORITY 2
q = restate->_queue->Qstate(2);
size = restate->_queue->Qsize(2);
if(size == 0)
{
printf("Queue of processes with priority 2 is empty\n");
printf("\n");
}
else
{
printf("Queue of processes with priority 2:: \n");
printf(" PID PRIORITY VALUE START TIME CPU TIME\n");
for(int j=0; j<size; j++)
{
printf("< %2d%10d%13d%13d%14d >\n", pcbt->_pcb_table[q[j]-1]._pid, pcbt->_pcb_table[q[j]-1]._priority,
pcbt->_pcb_table[q[j]-1]._value, pcbt->_pcb_table[q[j]-1]._start_time, pcbt->_pcb_table[q[j]-1]._cpu_time);
}
printf("\n");
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\\
//PRIORITY 3
q = restate->_queue->Qstate(3);
size = restate->_queue->Qsize(3);
if(size == 0)
{
printf("Queue of processes with priority 3 is empty\n");
printf("\n");
}
else
{
printf("Queue of processes with priority 3: \n");
printf(" PID PRIORITY VALUE START TIME CPU TIME\n");
for(int j=0; j<size; j++)
{
printf("< %2d%10d%13d%13d%14d >\n", pcbt->_pcb_table[q[j]-1]._pid, pcbt->_pcb_table[q[j]-1]._priority,
pcbt->_pcb_table[q[j]-1]._value, pcbt->_pcb_table[q[j]-1]._start_time, pcbt->_pcb_table[q[j]-1]._cpu_time);
}
printf("\n");