-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOS.c
More file actions
1349 lines (1158 loc) · 44.2 KB
/
OS.c
File metadata and controls
1349 lines (1158 loc) · 44.2 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
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
/*
* Problem 3 - Discontinuities
* TCSS 422 A Spring 2016
* Bun Kak, Chris Ottersen, Mark Peters, Paul Zander
*/
#include "OS.h"
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
#define WRITE_TO_FILE false
//static const word int REGNUM = sizeof(struct regfile)/sizeof(word);
#pragma
/*global declarations for system stack*/
static word SysStack[SYSSIZE];
static int SysPointer;
static int closeable;
/*timer fields*/
static thread THREAD_timer;
static mutex MUTEX_timer;
static cond COND_timer;
static bool INTERRUPT_timer;
static bool SHUTOFF_timer;
static word clock_;
/*IO*/
static io_thread IO[IO_NUMBER];
/*Shared Resources for PCBs*/
static PCB_r empty;
static PCB_r group[MAX_SHARED_RESOURCES + 1];
/*OS only declarations for current and idle processes*/
static PCB_p current;
static PCB_p idl;
static FIFOq_p createQ;
static FIFOq_p readyQ[PRIORITIES_TOTAL];
static FIFOq_p terminateQ;
/* Launches the OS. Sets default values, initializes idle process and calls the
* mainLoopOS to simulate running the cpu. Afterwards it cleans up reports
* any errors encountered.
*/
int main(void)
{
uint64_t s = clock();
int run;
word errors[SYSTEM_RUNS] = {0};
if (SYSTEM_RUNS < 1)
printf("NO SYSTEM RUN SET\n");
for (run = 1; run <= SYSTEM_RUNS; run++) {
if (EXIT_STATUS_MESSAGE)
printf("\nSYSTEM START RUN %d of %d\n\n", run, SYSTEM_RUNS);
if (DEBUG)
printf("Main begin\n");
// nanosleeptest();
if (WRITE_TO_FILE) {
freopen("scheduleTrace.txt", "w", stdout);
}
int base_error = bootOS();
if (DEBUG)
printf("OS booted\n");
int exit = mainLoopOS(&base_error);
if (DEBUG)
printf("OS shutdown\n");
stackCleanup();
if (base_error) {
if (EXIT_STATUS_MESSAGE || OUTPUT)
printf("\n>System exited with error %d\n", base_error);
} else {
if (EXIT_STATUS_MESSAGE)
printf("\n>System exited without incident\n");
if (OUTPUT) if (EXIT_ON_MAX_PROCESSES && exit == -2)
printf(
"\n>%ld processes have been created so system has exited\n",
MAX_PROCESSES);
else if (SHUTDOWN && exit == -SHUTDOWN)
printf("\n>%d cycles have run so system has exited\n",
SHUTDOWN);
else
printf(
"\n>Of %ld processes created, all terminable ones have terminated so system has exited\n",
MAX_PROCESSES);
}
printf(">Execution ended in %.3lf seconds.\n\n",
(clock() - s) * 1.0 / CLOCKS_PER_SEC);
errors[run - 1] = base_error;
if (EXIT_STATUS_MESSAGE)
printf("\nSYSTEM END RUN %d of %d\n\n", run, SYSTEM_RUNS);
int d;
if (EXIT_STATUS_MESSAGE)
for (d = 0; d < 4; d++)
printf(
"---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
}
word mass_error = 0;
for (run = 0; run < SYSTEM_RUNS; run++)
mass_error += errors[run];
return mass_error;
}
//initializes values and returns error
int bootOS()
{
int boot_error = OS_NO_ERROR;
int t;
//system wide
srand((uint32_t) time(NULL)); // seed random with current time
SysPointer = 0; //points at next unassigned stack item; 0 is empty
closeable = 0;
//Timer
clock_ = 0;
INTERRUPT_timer = false;
SHUTOFF_timer = false;
pthread_mutex_init(&MUTEX_timer, NULL);
pthread_cond_init(&COND_timer, NULL);
pthread_create(&THREAD_timer, NULL, timer, NULL);
//shared resources
empty = (PCB_r) malloc(sizeof(struct shared_resource));
empty->members = 0;
empty = mutexPair(&boot_error);
for (t = 0; t < MAX_SHARED_RESOURCES; t++)
group[t] = empty;
//IO
for (t = 0; t < IO_NUMBER; t++) {
IO[t] = (io_thread) malloc(sizeof(struct io_thread_type));
IO[t]->waitingQ = FIFOq_construct(&boot_error);
IO[t]->INTERRUPT_iocomplete = false;
IO[t]->SHUTOFF_io = false;
pthread_mutex_init(&(IO[t]->MUTEX_io), NULL);
pthread_cond_init(&(IO[t]->COND_io), NULL);
pthread_create(&(IO[t]->THREAD_io), NULL, io, (void *) t);
}
//idl pcb has special parameters
idl = PCB_construct(&boot_error);
idl->pid = ULONG_MAX;
idl->io = false;
idl->type = undefined;
idl->priority = LOWEST_PRIORITY;
idl->state = waiting;
idl->timeCreate = 0;
idl->timeTerminate = ULONG_MAX;
idl->regs->reg.pc = 0;
idl->regs->reg.MAX_PC = 1000;
idl->regs->reg.sw = ULONG_MAX;
idl->regs->reg.term_count = 0;
idl->regs->reg.TERMINATE = 0;
for (t = 0; t < IO_NUMBER * IO_CALLS; t++) {
idl->regs->reg.IO_TRAPS[t / IO_CALLS][t % IO_CALLS] = ~0u;
}
//queues
createQ = FIFOq_construct(&boot_error);
for (t = 0; t < PRIORITIES_TOTAL; t++)
readyQ[t] = FIFOq_construct(&boot_error);
terminateQ = FIFOq_construct(&boot_error);
return boot_error;
}
/* Main loop for the operating system. Initializes queues and PC/SW values.
* Runs until exit is triggered (in assignment 2, this is when 30 PCBs have been
* created). Repeatedly creates new PCBs, simulates running, gets interrupted
* by the timer, and restarting the loop.
*
* returns error
*/
int mainLoopOS(int *error)
{
if (DEBUG)
printf("Main loop initialization\n");
int t;
bool exit_okay = false;
current = idl; //current's default state if no ready PCBs to run
current->state = running;
sysStackPush(current->regs, error);
if (STACK_DEBUG)
printf("current max_pc is %lu\n", current->regs->reg.MAX_PC);
const CPU_p const CPU = (CPU_p) malloc(sizeof(struct CPU));
CPU->regs = (REG_p) malloc(sizeof(union regfile));
REG_init(CPU->regs, error);
if (STACK_DEBUG)
printf("pre max_pc is %lu\n", CPU->regs->reg.MAX_PC);
sysStackPop(CPU->regs, error);
if (STACK_DEBUG)
printf("post max_pc is %lu\n", CPU->regs->reg.MAX_PC);
//alias CPU's registers
word *const pc = &(CPU->regs->reg.pc);
word *const MAX_PC = &(CPU->regs->reg.MAX_PC);
word *const sw = &(CPU->regs->reg.sw);
word *const term_count = &(CPU->regs->reg.term_count);
word *const TERMINATE = &(CPU->regs->reg.TERMINATE);
word(*const IO_TRAPS)[IO_NUMBER][IO_CALLS] = &(CPU->regs->reg.IO_TRAPS);
int exit = 0;
if (*error) {
printf("ERROR detected before launch! %d", *error);
return *error;
}
if (EXIT_STATUS_MESSAGE)
printf(">OS System Clock at Start %lu\n\n", clock_);
if (DEBUG)
printf("Main loop begin\n");
/**************************************************************************/
/*************************** MAIN LOOP OS *********************************/
/**************************************************************************/
do {
clock_++;
exit = createPCBs(error);
if (!EXIT_ON_MAX_PROCESSES && exit && !exit_okay) {
exit_okay = true;
exit = 0;
}
if (DEBUG)
printf("PCBs created exit = %d\n", exit);
if (current == NULL || error == NULL) {
if (error != NULL) {
*error += CPU_NULL_ERROR;
}
exit = -1;
printf("ERROR current process unassigned or error lost! %d",
*error);
} else {
/*** INCREMENT PC ***/
(*pc)++;
if (DEBUG)
printf("PCB %lu PC incremented to %lu of %lu\n", current->pid,
*pc, *MAX_PC);
/*** TERMINATE CHECK ***/
if (*pc == *MAX_PC) {
*pc -= (*pc);
(*term_count)++;
if (DEBUG)
printf("term_count now %lu / %lu\n", *term_count,
*TERMINATE);
if (*term_count == *TERMINATE) {
word pid = current->pid;
if (DEBUG)
printf("At cycle PC = %lu, terminate interrupts %lu\n",
*pc, current->pid);
sysStackPush(CPU->regs, error);
trap_terminate(error);
sysStackPop(CPU->regs, error);
if (DEBUG)
printf("Process %lu terminated\n", pid);
if (DEBUG)
printf("At cycle PC = %lu, process %lu begins\n", *pc,
current->pid);
}
}
if (current != idl && current->pid > MAX_PROCESSES) {
if (DEBUG)
printf(
"post-term: pcb with pid %lu exceeds max processes, exiting system\n",
current->pid);
break;
}
/*** TIMER CHECK ***/
if (!pthread_mutex_trylock(&MUTEX_timer)) {
if (DEBUG)
printf("Timer check \n");
bool context_switch = false;
if (INTERRUPT_timer) {
INTERRUPT_timer = false;
context_switch = true;
}
pthread_mutex_unlock(&MUTEX_timer);
if (context_switch || PCB_SCHEDULE_EVERY) {
if (DEBUG)
printf("At cycle PC = %lu, timer interrupts %lu\n", *pc,
current->pid);
(*pc)--;
sysStackPush(CPU->regs, error);
interrupt(INTERRUPT_TIMER, NULL, error);
sysStackPop(CPU->regs, error);
if (DEBUG)
printf("At cycle PC = %lu, process %lu begins\n", *pc,
current->pid);
continue;
}
if (DEBUG)
printf("No cycle PC \n");
}
if (current != idl && current->pid > MAX_PROCESSES) {
if (DEBUG)
printf(
"post-time: pcb with pid %lu exceeds max processes, exiting system\n",
current->pid);
break;
}
/*** THREAD CHECK ***/
if (current->type > regular && current->type <= (LAST_PAIR * 2)) {
bool waitforbuddy = false;
bool reentry = false;
for (t = 0; t < CALL_NUMBER; t++) {
if (*pc == current->regs->reg.CALLS[t]) {
word call = current->regs->reg.CODES[t] &
-2; //truncates 1's place
word resource =
current->regs->reg.CODES[t] & 1; //1's place
uint64_t longerror = OS_NO_ERROR;
switch (call) {
case CODE_LOCK:
if (thread_mutex_lock(current,
group[current->group]->fmutex[resource],
&longerror))
waitforbuddy = true;
break;
case CODE_UNLOCK:
reentry = true;
t *= -1;
break;
case CODE_WAIT_T:
if (group[current->group]->flag[resource]) { //flag is true
thread_cond_wait(current,
group[current->group]->fcond[resource],
group[current->group]->fmutex[resource],
&longerror);
waitforbuddy = true;
}
break;
case CODE_WAIT_F:
if (!group[current->group]->flag[resource]) {//flag is false
thread_cond_wait(current,
group[current->group]->fcond[resource],
group[current->group]->fmutex[resource],
&longerror);
waitforbuddy = true;
}
break;
case CODE_SIGNAL:
reentry = true;
break;
case CODE_READ:
(*sw) = group[current->group]->resource[resource];
break;
case CODE_WRITE:
(group[current->group]->resource[resource])++;
break;
case CODE_FLAG:
group[current->group]->flag[resource] =
(bool) !(group[current->group]->flag[resource]);
break;
}
error += (int) longerror;
break;
}
}
if (waitforbuddy || reentry) {
sysStackPush(CPU->regs, error);
if (waitforbuddy)
trap_mutexhandler(t, error);
else if (reentry)
trap_requehandler(t, error);
sysStackPop(CPU->regs, error);
continue;
}
}
/*** IO CHECK ***/
if (DEBUG)
printf("Checking IO if complete \n");
for (t = 0; t < IO_NUMBER; t++)
if (!pthread_mutex_trylock(&(IO[t]->MUTEX_io))) {
if (DEBUG)
printf("io %d Mutex locked\n", t + FIRST_IO);
if (IO[t]->INTERRUPT_iocomplete) {
sysStackPush(CPU->regs, error);
interrupt(INTERRUPT_IOCOMPLETE, (void *) t, error);
sysStackPop(CPU->regs, error);
IO[t]->INTERRUPT_iocomplete = false;
}
pthread_mutex_unlock(&(IO[t]->MUTEX_io));
pthread_cond_signal(&(IO[t]->COND_io));
if (DEBUG)
printf("io %d Mutex unlocked\n", t + FIRST_IO);
}
if (current != idl && current->pid > MAX_PROCESSES) {
if (DEBUG)
printf(
"post-iocm: pcb with pid %lu exceeds max processes, exiting system\n",
current->pid);
break;
}
/*** PCB TRAPS CHECK ***/
if (current->io)
for (t = 0; t < IO_NUMBER * IO_CALLS; t++)
if (*pc == (*IO_TRAPS)[t / IO_CALLS][t % IO_CALLS]) {
t = t / IO_CALLS;
if (DEBUG)
printf(
"Process %lu at PC %lu place in wQ of IO %d\n",
current->pid, *pc, t + FIRST_IO);
pthread_mutex_lock(&(IO[t]->MUTEX_io));
sysStackPush(CPU->regs, error);
trap_iohandler(t, error);
sysStackPop(CPU->regs, error);
pthread_mutex_unlock(&(IO[t]->MUTEX_io));
pthread_cond_signal(&(IO[t]->COND_io));
if (DEBUG)
printf("At cycle PC = %lu, process %lu begins\n",
*pc, current->pid);
break;
}
if (current != idl && current->pid > MAX_PROCESSES) {
if (EXIT_STATUS_MESSAGE)
printf(
"post-trap: pcb with pid %lu exceeds max processes, exiting system\n",
current->pid);
break;
}
if (DEBUG)
printf("PCB %lu PC cycle %lu finished\n", current->pid, *pc);
}
if (!EXIT_ON_MAX_PROCESSES) if (exit_okay && closeable == 0)
exit = -1;
else
exit = 0;
if (SHUTDOWN && clock_ >= SHUTDOWN)
exit = -SHUTDOWN;
} while (!*error && !exit);
/**************************************************************************/
/*************************** *********** **********************************/
/**************************************************************************/
if (DEBUG)
printf("Main loop OS stop\n");
sysStackPush(CPU->regs, error);
sysStackPop(current->regs, error);
if (EXIT_STATUS_MESSAGE)
printf("\n>OS System Clock at Exit: %lu\n\n\n", clock_);
// for(t = 0; t < TIMER_SLEEP; t++);
cleanup(error);
return exit;
}
/******************************************************************************/
/******************************* THREADS **************************************/
/******************************************************************************/
void *timer(void *unused)
{
if (THREAD_DEBUG)
printf("\tTIMER: begin THREAD\n");
int c;
word next = 0;
bool shutoff;
word clock = 0;
do {
do {
for (c = 0; c < TIMER_SLEEP; c++); //sleeping simulation
pthread_mutex_lock(&MUTEX_timer);
clock = clock_;
shutoff = SHUTOFF_timer;
pthread_mutex_unlock(&MUTEX_timer);
pthread_cond_signal(&COND_timer);
if (THREAD_DEBUG)
printf("\tTIMER: clock is %lu out of %lu, shutoff is %s\n",
clock, next, shutoff ? "true" : "false");
} while (clock < next && !shutoff);
pthread_mutex_lock(&MUTEX_timer);
INTERRUPT_timer = true;
next = clock_ + TIME_QUANTUM;
if (THREAD_DEBUG)
printf("\tTIMER: begin clock at %lu\n", clock);
pthread_mutex_unlock(&MUTEX_timer);
} while (!shutoff);
if (THREAD_DEBUG)
printf("\tTIMER: end clock at %lu\n", clock);
pthread_mutex_unlock(&MUTEX_timer);
pthread_cond_signal(&COND_timer);
pthread_exit(NULL);
return NULL;
}
void *io(void *tid)
{
int t = (int) tid;
//tid is int for thread number
if (THREAD_DEBUG)
printf("\t\tIO %d: begin THREAD\n", t + FIRST_IO);
int io_error = OS_NO_ERROR;
int c;
bool empty;
bool shutoff;
do {
pthread_mutex_lock(&(IO[t]->MUTEX_io));
shutoff = IO[t]->SHUTOFF_io;
empty = FIFOq_is_empty(IO[t]->waitingQ, &io_error);
pthread_mutex_unlock(&(IO[t]->MUTEX_io));
while (!empty && !shutoff) {
if (THREAD_DEBUG)
printf("\t\tIO %d: queue has %d PCBs left; beginning IO ops\n",
t + FIRST_IO, IO[t]->waitingQ->size);
word sleep = rand() % (IO_MAX_SLEEP - IO_MIN_SLEEP) + IO_MIN_SLEEP;
for (c = 0; c < sleep; c++); //sleeping simulation
pthread_mutex_lock(&(IO[t]->MUTEX_io));
shutoff = IO[t]->SHUTOFF_io;
if (!shutoff) {
IO[t]->INTERRUPT_iocomplete = true;
pthread_mutex_unlock(&(IO[t]->MUTEX_io));
pthread_cond_wait(&(IO[t]->COND_io), &(IO[t]->MUTEX_io));
empty = FIFOq_is_empty(IO[t]->waitingQ, &io_error);
}
if (THREAD_DEBUG)
printf(
"\t\tIO %d: IO ops finished; queue has %d PCBs left, shutoff is %s\n",
t + FIRST_IO, IO[t]->waitingQ->size,
shutoff ? "true" : "false");
pthread_mutex_unlock(&(IO[t]->MUTEX_io));
}
pthread_mutex_lock(&(IO[t]->MUTEX_io));
empty = FIFOq_is_empty(IO[t]->waitingQ, &io_error);
if (empty && !shutoff)
pthread_cond_wait(&(IO[t]->COND_io), &(IO[t]->MUTEX_io));
pthread_mutex_unlock(&(IO[t]->MUTEX_io));
} while (!shutoff);
if (THREAD_DEBUG)
printf("\t\tIO %d: shutting off\n", t + FIRST_IO);
pthread_exit(NULL);
return NULL;
}
/******************************************************************************/
/******************************** TRAPS ***************************************/
/******************************************************************************/
void trap_terminate(int *error)
{
sysStackPop(current->regs, error);
PCB_setState(current,
terminated); //this is the ONLY PLACE a pcb should ever be terminated
current->timeTerminate = clock_;
current->priority = current->orig_priority;
closeable--;
if (current->group) {
int g = current->group;
group[current->group]->members--;
if (group[current->group]->members == 0) {
mutexEmpty(group[current->group], error);
free(group[g]);
group[g] = empty;
}
}
char pcbstr[PCB_TOSTRING_LEN];
if (OUTPUT)
printf(">Terminated: %s\n", PCB_toString(current, pcbstr, error));
FIFOq_enqueuePCB(terminateQ, current, error);
//current = idl;
scheduler(error);
}
void trap_iohandler(const int t, int *error)
{
sysStackPop(current->regs, error);
current->state = waiting;
char pcbstr[PCB_TOSTRING_LEN];
if (OUTPUT)
printf(">I/O %d added: %s\n", t + FIRST_IO,
PCB_toString(current, pcbstr, error));
FIFOq_enqueuePCB(IO[t]->waitingQ, current, error);
if (THREAD_DEBUG)
printf("\t\tIO %d: gained PCB\n", t + FIRST_IO);
scheduler(error);
}
void trap_mutexhandler(const int t, int *error)
{
sysStackPop(current->regs, error);
current->state = blocked;
char pcbstr[PCB_TOSTRING_LEN]; //%s
if (OUTPUT)
printf(">Group %d enqueue: %s\n", t,
PCB_toString(current, pcbstr, error));
//current already enqueued in mutex/cond
if (THREAD_DEBUG)
printf("\t\tGroup %d: gained PCB\n", t);
scheduler(error);
}
void trap_requehandler(const int t, int *error)
{
PCB_p pcb = NULL;
uint64_t longerror;
if (t < 0) {
thread_mutex_unlock(current, group[current->group]->fmutex[-t],
&longerror);
} else {
thread_cond_signal(group[current->group]->fcond[t], &longerror);
}
longerror += longerror;
if (pcb != NULL) {
pcb->state = ready;
pcb->lastClock = clock_; //to track starvation
if (pcb->promoted) {
pcb->attentionCount++;
}
FIFOq_enqueuePCB(readyQ[pcb->priority], pcb, error);
char pcbstr[PCB_TOSTRING_LEN]; //%s
if (OUTPUT)
printf(">Group %d dequeue: %s\n", t,
PCB_toString(pcb, pcbstr, error));
}
}
/******************************************************************************/
/*********************** INTERRUPT SERVICE ROUTINES ***************************/
/******************************************************************************/
void interrupt(const int INTERRUPT, void *args, int *error)
{
switch (INTERRUPT) {
case NO_INTERRUPT:
current->state = running;
break;
case INTERRUPT_TIMER:
isr_timer(error);
break;
case INTERRUPT_IOCOMPLETE:
isr_iocomplete(((int) args), error);
break;
default:
*error += OS_UNKOWN_INTERRUPT_ERROR;
}
}
/* Interrupt service routine for the timer: interrupts the current PCB and saves
* the CPU state to it before calling the scheduler.
*/
void isr_timer(int *error)
{
//change the state from running to interrupted
PCB_setState(current, interrupted);
//assigns Current PCB PC and SW values to popped values of SystemStack
if (DEBUG)
printf("\t\tStack going to pop isrtimer: %d\n", SysPointer);
sysStackPop(current->regs, error);
//call Scheduler and pass timer interrupt parameter
scheduler(error);
}
void isr_iocomplete(const int t, int *error)
{
if (!FIFOq_is_empty(IO[t]->waitingQ, error)) {
PCB_p pcb = FIFOq_dequeue(IO[t]->waitingQ, error);
pcb->state = ready;
pcb->lastClock = clock_; //to track starvation
if (pcb->promoted) {
pcb->attentionCount++;
}
FIFOq_enqueuePCB(readyQ[pcb->priority], pcb, error);
char pcbstr[PCB_TOSTRING_LEN];
if (OUTPUT)
printf(">I/O %d complete: %s\n", t + FIRST_IO,
PCB_toString(pcb, pcbstr, error));
} else if (THREAD_DEBUG)
printf("ERROR! nothing to dequeue in IO %d\n", t);
}
/******************************************************************************/
/************************** SCHEDULERS/LOADERS ********************************/
/******************************************************************************/
/* Always schedules any newly created PCBs into the ready queue, then checks
* the interrupt type: if the interrupt is a timer interrupt, requeues the
* currently running process in the ready queue and sets its state to ready.
* Then calls the dispatcher.
*/
void scheduler(int *error)
{
static int schedules = 0;
PCB_p temp;
PCB_p pcb = current;
bool pcb_idl = current == idl;
bool pcb_term = current->state == terminated;
bool pcb_io = current->state == waiting;
bool pcb_mtx = current->state == blocked;
if (createQ == NULL) {
*error += FIFO_NULL_ERROR;
puts("ERROR: createQ is null");
return;
}
if (readyQ == NULL) {
*error += FIFO_NULL_ERROR;
puts("ERROR: readyQ is null");
return;
}
//enqueue any created processes
while (!FIFOq_is_empty(createQ, error)) {
temp = FIFOq_dequeue(createQ, error);
temp->state = ready;
temp->lastClock = clock_;
FIFOq_enqueuePCB(readyQ[temp->priority], temp, error);
if (OUTPUT) {
char pcbstr[PCB_TOSTRING_LEN];
printf(">Enqueued readyQ: %s\n", PCB_toString(temp, pcbstr, error));
}
}
if (DEBUG)
printf("createQ transferred to readyQ\n");
int r;
for (r = 0; r < PRIORITIES_TOTAL; r++)
if (!FIFOq_is_empty(readyQ[r], error))
break;
if (r == PRIORITIES_TOTAL) {
if (pcb_term || pcb_io || pcb_mtx)
current = idl;
PCB_setState(current, running);
sysStackPush(current->regs, error);
return;
}
schedules++;
if (!(schedules % STARVATION_CHECK_FREQUENCY)) {
awakeStarvationDaemon(error);
}
for (r = 0; r < PRIORITIES_TOTAL; r++)
if (!FIFOq_is_empty(readyQ[r], error))
break;
if (OUTPUT) {
char pcbstr[PCB_TOSTRING_LEN];
printf(">PCB: %s\n", PCB_toString(current, pcbstr, error));
char rdqstr[PCB_TOSTRING_LEN];
printf(">Switching to: %s\n",
PCB_toString(readyQ[r]->head->data, rdqstr, error));
}
//if it's a timer interrupt
if (!pcb_idl && !pcb_term && !pcb_io && !pcb_mtx) {
//todo:add stuff about locks and conds
current->state = ready;
current->lastClock = clock_;
if (current->promoted) {
current->attentionCount++;
}
FIFOq_enqueuePCB(readyQ[current->priority], current, error);
} else {
idl->state = waiting;
}
dispatcher(error);
if (OUTPUT) {
char runstr[PCB_TOSTRING_LEN];
printf(">Now running: %s\n", PCB_toString(current, runstr, error));
char rdqstr[PCB_TOSTRING_LEN];
if (!pcb_idl && !pcb_term && !pcb_io) {
if (readyQ[r]->size > 1) {
printf(">Requeued readyQ: %s\n",
PCB_toString(readyQ[r]->tail->data, rdqstr, error));
} else {
printf(">No process return required.\n");
}
} else if (pcb_idl) {
printf(">Idle process: %s\n", PCB_toString(idl, rdqstr, error));
} else if (pcb_term) {
printf(">Exited system: %s\n",
PCB_toString(terminateQ->tail->data, rdqstr, error));
} else if (pcb_io) {
printf(">Requested I/O: %s\n", PCB_toString(pcb, rdqstr, error));
} else if (pcb_mtx) {
printf(">Mutex lock/wait: %s\n", PCB_toString(pcb, rdqstr, error));
}
int stz = FIFOQ_TOSTRING_MAX;
char str[stz];
if (OUTPUT) {
printf(">Priority %d %s\n", r,
FIFOq_toString(readyQ[r], str, &stz, error));
}
}
}
/**
* Checks if any pcb's in the waiting queue need to be promoted due to starvation or
* demoted due to being promoted and recieving enough attention.
* @param error - error collects all the error signals.
*/
void awakeStarvationDaemon(int *error)
{
if (DEBUG)
puts(
"~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?");
if (DEBUG)
puts("Starvation Daemon");
int rank;
for (rank = 0; rank < PRIORITIES_TOTAL; rank++) {
Node_p curr = readyQ[rank]->head;
Node_p prev = curr;
Node_p next;
while (curr != NULL) {
if (DEBUG)
printf(">PID of inspect: %lu while Rank: %d PCB rank: "
"%hu, attentionRecieved: %lu\n",
curr->data->pid, rank, curr->data->priority,
curr->data->attentionCount);
if (DEBUG)
printf("Wait time: %lu\n", (clock_ - curr->data->lastClock));
//if current received enough attention then remove, demote and set current to next
if (curr->data->attentionCount >= PROMOTION_ALLOWANCE) {
if (DEBUG)
puts("Demoting a process");
if (DEBUG)
printf(">Demoted PID: %lu while Rank: %d PCB rank:"
" %hu attentionRecieved: %lu\n",
curr->data->pid, rank, curr->data->priority,
curr->data->attentionCount);
next = FIFOq_remove_and_return_next(curr, prev, readyQ[rank]);
curr->data->attentionCount = 0;
curr->data->promoted = false;
curr->data->priority = curr->data->orig_priority;
FIFOq_enqueue(readyQ[curr->data->priority], curr, error);
char pcbstr[PCB_TOSTRING_LEN];
if (OUTPUT)
printf(">Demoted: %s\n",
PCB_toString(curr->data, pcbstr, error));
curr = next;
if (curr == readyQ[rank]->head) {
prev = curr;
}
} else if (rank > 0 && (clock_ - curr->data->lastClock) >
STARVATION_CLOCK_LIMIT) {
//remove current, promote current process, set current to next
if (DEBUG)
puts("Promoting a process");
if (DEBUG)
printf(">Promoted PID: %lu while Rank: %d PCB rank: "
"%hu attentionRecieved: %lu\n",
curr->data->pid, rank, curr->data->priority,
curr->data->attentionCount);
next = FIFOq_remove_and_return_next(curr, prev, readyQ[rank]);
if (!curr->data->promoted) {
curr->data->attentionCount = 0;
} else {
if (DEBUG)
printf("Node PID: %lu\n promoted once again.\n",
curr->data->pid);
}
char pcbstr[PCB_TOSTRING_LEN];
if (OUTPUT)
printf(">Promoted: %s\n",
PCB_toString(curr->data, pcbstr, error));
curr->data->promoted = true;
curr->data->priority = rank - 1;
FIFOq_enqueue(readyQ[curr->data->priority], curr, error);
curr = next;
if (curr == readyQ[rank]->head) {
prev = curr;
}
} else {
prev = curr;
curr = curr->next_node;
}
}
}
if (DEBUG)
puts(
"~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?~?");
}
/* Dispatches a new current process by dequeuing the head of the ready queue,
* setting its state to running and copying its CPU state onto the stack.
*/
void dispatcher(int *error)
{
int r;
if (readyQ == NULL) {
*error += FIFO_NULL_ERROR;
printf("%s", "ERROR: readyQ is null");
} else
for (r = 0; r < PRIORITIES_TOTAL; r++)
if (!FIFOq_is_empty(readyQ[r],
error)) { //dequeue the head of readyQueue
current = FIFOq_dequeue(readyQ[r], error);
break;
}
//change current's state to running point
current->state = running;
if (DEBUG) {
char pcbstr[PCB_TOSTRING_LEN];
printf("CURRENT: \t%s\n", PCB_toString(current, pcbstr, error));
}
//copy currents's PC value to SystemStack
if (DEBUG)
printf("\t\tStack going to push dispatch: %d\n", SysPointer);
sysStackPush(current->regs, error);
}
/* Creates 0 to 5 PCBs and enqueues them into a special queue, create queue.
* Keeps track of how many PCBs have been created and sends an exit signal
* when 30 have been created.
*/