-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphase1.c
1047 lines (867 loc) · 27.9 KB
/
phase1.c
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
/* ------------------------------------------------------------------------
phase1.c
University of Arizona
Computer Science 452
Fall 2015
------------------------------------------------------------------------ */
//When to enable/disable interrupts?
//What to do with sentinel? All processes done?
#include "phase1.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "kernel.h"
/* ------------------------- Prototypes ----------------------------------- */
int sentinel (char *);
extern int start1 (char *);
void dispatcher(void);
void launch();
static void checkDeadlock();
int isInKernelMode();
int isInterruptEnabled();
int enableInterrupts();
void disableInterrupts();
int enterKernelMode();
int enterUserMode();
unsigned int getNextPid();
int isProcessTableFull();
void initProcessTable();
void initReadyLists();
void addProcToReadyLists();
void cleanProcess(procPtr);
void dumpProcesses();
int zap(int pid);
int isZapped(void);
int getpid(void);
int blockMe(int block_status);
int unblockProc(int pid);
int readCurStartTime(void);
void timeSlice(void);
int readtime(void);
void clockHandler(int dev, void * arg);
int countProcesses();
void illegalInstructionHandler(int dev, void *arg);
void timeSlice(void);
int readCurStartTime(void);
int onReadyList(int pid, int priority);
/* -------------------------- Globals ------------------------------------- */
// Patrick's debugging global variable...
int debugflag = 0;
// the process table
procStruct ProcTable[MAXPROC];
// Process lists
// static procPtr ReadyList;
static procPtr ReadyLists[SENTINELPRIORITY]; //linked list (queue) for each priority
// current process ID
procPtr Current = NULL;
// the next pid to be assigned
unsigned int nextPid = 0;
/* -------------------------- Functions ----------------------------------- */
/* ------------------------------------------------------------------------
Name - startup
Purpose - Initializes process lists and clock interrupt vector.
Start up sentinel process and the test process.
Parameters - argc and argv passed in by USLOSS
Returns - nothing
Side Effects - lots, starts the whole thing
----------------------------------------------------------------------- */
void startup(int argc, char *argv[])
{
int result; /* value returned by call to fork1() */
// Initialize the clock interrupt handler
USLOSS_IntVec[USLOSS_CLOCK_INT] = clockHandler;
USLOSS_IntVec[USLOSS_ILLEGAL_INT] = illegalInstructionHandler;
/* initialize the process table */
if (DEBUG && debugflag)
USLOSS_Console("startup(): initializing process table, ProcTable[]\n");
initProcessTable();
// Initialize the Ready list, etc.
if (DEBUG && debugflag)
USLOSS_Console("startup(): initializing the Ready list\n");
initReadyLists();
// startup a sentinel process
if (DEBUG && debugflag)
USLOSS_Console("startup(): calling fork1() for sentinel\n");
result = fork1("sentinel", sentinel, NULL, USLOSS_MIN_STACK,
SENTINELPRIORITY);
if (result < 0) {
if (DEBUG && debugflag) {
USLOSS_Console("startup(): fork1 of sentinel returned error, ");
USLOSS_Console("halting...\n");
}
USLOSS_Halt(1);
}
// start the test process
if (DEBUG && debugflag)
USLOSS_Console("startup(): calling fork1() for start1\n");
result = fork1("start1", start1, NULL, 2 * USLOSS_MIN_STACK, 1);
if (result < 0) {
USLOSS_Console("startup(): fork1 for start1 returned an error, ");
USLOSS_Console("halting...\n");
USLOSS_Halt(1);
}
USLOSS_Console("startup(): Should not see this message! ");
USLOSS_Console("Returned from fork1 call that created start1\n");
return;
} /* startup */
/* ------------------------------------------------------------------------
Name - finish
Purpose - Required by USLOSS
Parameters - none
Returns - nothing
Side Effects - none
----------------------------------------------------------------------- */
void finish(int argc, char *argv[])
{
if (DEBUG && debugflag)
USLOSS_Console("in finish...\n");
} /* finish */
/* ------------------------------------------------------------------------
Name - fork1
Purpose - Gets a new process from the process table and initializes
information of the process. Updates information in the
parent process to reflect this child process creation.
Parameters - the process procedure address, the size of the stack and
the priority to be assigned to the child process.
Returns - the process id of the created child or -1 if no child could
be created or if priority is not between max and min priority.
Side Effects - ReadyList is changed, ProcTable is changed, Current
process information changed
------------------------------------------------------------------------ */
int fork1(char *name, int (*startFunc)(char *), char *arg,
int stacksize, int priority)
{
// test if in kernel mode; halt if in user mode
if ( !isInKernelMode() ) {
USLOSS_Console("fork1(): called while in user mode, by process %d. Halting...\n", getNextPid()-1);
USLOSS_Halt(1);
}
disableInterrupts();
int procSlot = -1;
if (DEBUG && debugflag)
USLOSS_Console("fork1(): creating process %s\n", name);
if (name == NULL || startFunc == NULL) {
fprintf(stderr, "fork1(): Name and/or start function cannot be null.\n");
enableInterrupts();
return -1;
}
// test if trying to apply sentinel priority to non-sentinel process
if (strcmp(name, "sentinel") != 0 && priority == SENTINELPRIORITY) {
if (DEBUG && debugflag)
fprintf(stderr, "fork1(): Cannot assign sentinel prority to process other than the sentinel. Halting...\n");
USLOSS_Halt(1);
}
// check for priority out of range
if (priority > SENTINELPRIORITY || priority < MAXPRIORITY) {
if (DEBUG && debugflag)
fprintf(stderr, "fork1(): Priority out of range.\n");
enableInterrupts();
return -1;
}
// Return if stack size is too small
if ( stacksize < USLOSS_MIN_STACK ){
if (DEBUG && debugflag)
USLOSS_Console("fork1(): Requested Stack size too small.\n");
enableInterrupts();
return -2;
}
// Is there room in the process table? What is the next PID?
if (isProcessTableFull()){
if (DEBUG && debugflag)
USLOSS_Console("fork1(): Process Table is full.\n");
enableInterrupts();
return -1;
}
int pid = getNextPid();
procSlot = (pid - 1) % MAXPROC;
if (DEBUG && debugflag)
USLOSS_Console("fork1(): %s's pid is %d\n", name, pid);
// fill-in entry in process table */
if ( strlen(name) >= (MAXNAME - 1) ) {
USLOSS_Console("fork1(): Process name is too long. Halting...\n");
USLOSS_Halt(1);
}
strcpy(ProcTable[procSlot].name, name);
ProcTable[procSlot].pid = pid;
ProcTable[procSlot].priority = priority;
ProcTable[procSlot].startFunc = startFunc;
ProcTable[procSlot].stack = (char *) malloc(stacksize * sizeof(char));
ProcTable[procSlot].stackSize = stacksize;
ProcTable[procSlot].status = READY;
ProcTable[procSlot].numJoins = 0;
ProcTable[procSlot].numKids = 0;
ProcTable[procSlot].numLiveKids = 0;
ProcTable[procSlot].startTime = -1;
ProcTable[procSlot].totalTimeUsed = 0;
if ( arg == NULL ) {
ProcTable[procSlot].startArg[0] = '\0';
}
else if ( strlen(arg) >= (MAXARG - 1) ) {
USLOSS_Console("fork1(): argument too long. Halting...\n");
USLOSS_Halt(1);
}
else {
strcpy(ProcTable[procSlot].startArg, arg);
}
// Initialize context for this process, but use launch function pointer for
// the initial value of the process's program counter (PC)
//must enable interrupts before running contextinit
enableInterrupts();
USLOSS_ContextInit(&(ProcTable[procSlot].state),
ProcTable[procSlot].stack,
ProcTable[procSlot].stackSize,
NULL,
launch);
disableInterrupts();
// for future phase(s)
p1_fork(ProcTable[procSlot].pid);
//append this new process to current's list of children
if (Current != NULL) {
if (Current->childProcPtr == NULL) {
Current->childProcPtr = &ProcTable[procSlot];
}
else {
procPtr prev = NULL;
procPtr curr = Current->childProcPtr;
while (curr != NULL) {
prev = curr;
curr = curr->nextSiblingPtr;
}
prev->nextSiblingPtr = &ProcTable[procSlot];
}
ProcTable[procSlot].parentPtr = Current;
Current->numKids++;
Current->numLiveKids++;
}
// More stuff to do here...
addProcToReadyLists(procSlot, priority);
if (0 != strcmp(ProcTable[procSlot].name, "sentinel")) { // do not call dispatcher when creating sentinel
if (DEBUG && debugflag)
USLOSS_Console("fork1(): calling dispatcher()\n");
enableInterrupts();
dispatcher();
}
enableInterrupts();
return pid;
} /* fork1 */
/* ------------------------------------------------------------------------
Name - launch
Purpose - Dummy function to enable interrupts and launch a given process
upon startup.
Parameters - none
Returns - nothing
Side Effects - enable interrupts
------------------------------------------------------------------------ */
void launch()
{
int result;
if (DEBUG && debugflag)
USLOSS_Console("launch(): started\n");
// Enable interrupts
result = enableInterrupts();
if (result == -1) {
fprintf(stderr, "launch(): failed to enable interrupts.\n");
}
// Call the function passed to fork1, and capture its return value
result = Current->startFunc(Current->startArg);
if (DEBUG && debugflag)
USLOSS_Console("Process %d returned to launch\n", Current->pid);
quit(result);
} /* launch */
/* ------------------------------------------------------------------------
Name - join
Purpose - Wait for a child process (if one has been forked) to quit. If
one has already quit, don't wait.
Parameters - a pointer to an int where the termination code of the
quitting process is to be stored.
Returns - the process id of the quitting child joined on.
-1 if the process was zapped in the join
-2 if the process has no children
Side Effects - If no child process has quit before join is called, the
parent is removed from the ready list and blocked.
------------------------------------------------------------------------ */
int join(int *status)
{
if ( !isInKernelMode() ) {
USLOSS_Console("join(): called while in user mode, by process %d. Halting...\n", Current->pid);
USLOSS_Halt(1);
}
disableInterrupts();
if (Current->childProcPtr == NULL && Current->quitList == NULL) {
if (DEBUG && debugflag)
USLOSS_Console("join(): %d has no children\n", Current->pid);
enableInterrupts();
return -2; // has no children
}
if (Current->numJoins == Current->numKids) {
if (DEBUG && debugflag)
USLOSS_Console("join(): already joined for each child\n");
enableInterrupts();
return -2; // already joined for each child
}
if (Current->quitList != NULL) { // child has already quit
if (DEBUG && debugflag)
USLOSS_Console("Join(): Child has already quit\n");
}
else {
if (DEBUG && debugflag)
USLOSS_Console("Join(): Must wait for child\n");
Current->status = JOINBLOCKED;
enableInterrupts();
dispatcher();
disableInterrupts();
}
procPtr quitChild = Current->quitList;
Current->quitList = Current->quitList->quitNext;
*status = quitChild->quitStatus;
Current->numJoins++;
int pid = quitChild->pid;
cleanProcess(quitChild);
enableInterrupts();
// dispatcher(); // FIXME: needed?
if (isZapped()) {
return -1;
}
return pid;
} /* join */
/* ------------------------------------------------------------------------
Name - quit
Purpose - Stops the child process and notifies the parent of the death by
putting child quit info on the parents child completion code
list.
Parameters - the code to return to the grieving parent
Returns - nothing
Side Effects - changes the parent of pid child completion status list.
------------------------------------------------------------------------ */
void quit(int status)
{
if ( !isInKernelMode() ) {
USLOSS_Console("quit(): called while in user mode, by process %d. Halting...\n", Current->pid);
USLOSS_Halt(1);
}
disableInterrupts();
procPtr temp = Current->childProcPtr;
while (temp != NULL) { // Report error if trying to terminate a process who still has running children
if (temp->status != QUIT && temp->status != EMPTY) {
fprintf(stderr, "quit(): process %d, '%s', has active children. Halting...\n", Current->pid, Current->name);
USLOSS_Halt(1);
}
temp = temp->nextSiblingPtr;
}
// If parent exsts, add quitting child to their quit list, remove the quitting child from their child list, and unblock them if need be
if (Current->parentPtr != NULL){
Current->parentPtr->numLiveKids--;
// Add to quitlist
if (Current->parentPtr->quitList == NULL) {
Current->parentPtr->quitList = Current;
}
else {
procPtr curr = Current->parentPtr->quitList;
procPtr prev = Current->parentPtr->quitList;
while (curr != NULL) {
prev = curr;
curr = curr->quitNext;
}
prev->quitNext = Current;
}
// Remove from child list
procPtr curr = Current->parentPtr->childProcPtr;
procPtr prev = NULL;
if (curr->pid == Current->pid) {
Current->parentPtr->childProcPtr = Current->nextSiblingPtr;
}
else {
while (curr->pid != Current->pid) {
if (curr == NULL) {
fprintf(stderr, "quit(): Can't find self in parent's child list to remove self.\n");
USLOSS_Halt(1);
}
prev = curr;
curr = curr->nextSiblingPtr;
}
prev->nextSiblingPtr = curr->nextSiblingPtr;
}
// Unblock blocked parent
if (Current->parentPtr->status == JOINBLOCKED) {
Current->parentPtr->status = READY;
}
}
// Cleanup process table of all children of the now quit parent (if a parent)
if (Current->childProcPtr != NULL) {
procPtr curr = Current->childProcPtr;
while (curr != NULL) {
procPtr next = curr->nextSiblingPtr;
cleanProcess(curr);
curr = next;
}
}
// Unblock all processes that have zapped me
if (Current->zapperList != NULL) {
procPtr curr = Current->zapperList;
while (curr != NULL) {
curr->status = READY;
curr = curr->zapperNext;
}
}
Current->status = QUIT;
Current->quitStatus = status;
p1_quit(Current->pid);
Current = NULL;
enableInterrupts();
dispatcher();
} /* quit */
/* ------------------------------------------------------------------------
Name - dispatcher
Purpose - dispatches ready processes. The process with the highest
priority (the first on the ready list) is scheduled to
run. The old process is swapped out and the new process
swapped in.
Parameters - none
Returns - nothing
Side Effects - the context of the machine is changed
----------------------------------------------------------------------- */
void dispatcher(void)
{
// test if in kernel mode; halt if in user mode
if ( !isInKernelMode() ) {
USLOSS_Console("dispatcher(): called while in user mode, by process %d. Halting...\n", Current->pid);
USLOSS_Halt(1);
}
disableInterrupts();
//check that sentinel exists
if (ReadyLists[SENTINELPRIORITY - 1] == NULL){
USLOSS_Console("dispatcher(): Sentinel does not exist in ready list\n");
USLOSS_Halt(1);
}
procPtr nextProcess = NULL;
procPtr temp = NULL;
int i;
for( i = 0; i < SENTINELPRIORITY; i++){ //loop through each priority
temp = ReadyLists[i];
// USLOSS_Console("-----------PRIORITY %d---------------\n", i+1);
while (temp != NULL && temp->status != READY && temp->status != RUNNING){
//int nextProcPid = temp->nextProcPtr == NULL? -1 : temp->nextProcPtr->pid;
// USLOSS_Console("name = %s, pid = %d, status = %d, nextProcPid = %d\n", temp->name, temp->pid, temp->status, nextProcPid);
temp = temp->nextProcPtr;
}
// if (temp != NULL){
// int nextProcPid = temp->nextProcPtr == NULL? -1 : temp->nextProcPtr->pid;
// USLOSS_Console("name = %s, pid = %d, status = %d, nextProcPid = %d\n", temp->name, temp->pid, temp->status, nextProcPid);
// } else {
// USLOSS_Console("null\n");
// }
if (temp != NULL){
nextProcess = temp;
// USLOSS_Console("------------------------------------\n");
break;
}
// USLOSS_Console("------------------------------------\n");
}
if (DEBUG && debugflag)
USLOSS_Console("dispatcher(): found process %s (pid %d) at priority %d\n", temp->name, temp->pid, i+1);
if (Current == NULL){ //possibly
p1_switch(-1, nextProcess->pid);
} else {
p1_switch(Current->pid, nextProcess->pid);
}
//get old and new contexts
USLOSS_Context * oldContext = Current == NULL ? NULL : &Current->state;
USLOSS_Context * newContext = &nextProcess->state;
if (Current != NULL){
Current->totalTimeUsed = Current->totalTimeUsed + (readtime() - Current->startTime);
Current->startTime = -1; //FIXME: maybe
}
//reset current
Current = nextProcess;
Current->status = RUNNING;
Current->startTime = readtime();
enableInterrupts();
//call to context switch
USLOSS_ContextSwitch(oldContext, newContext);
} /* dispatcher */
/* ------------------------------------------------------------------------
Name - sentinel
Purpose - The purpose of the sentinel routine is two-fold. One
responsibility is to keep the system going when all other
processes are blocked. The other is to detect and report
simple deadlock states.
Parameters - none
Returns - nothing
Side Effects - if system is in deadlock, print appropriate error
and halt.
----------------------------------------------------------------------- */
int sentinel (char *dummy)
{
if (DEBUG && debugflag)
USLOSS_Console("sentinel(): called\n");
if ( !isInKernelMode() ) {
USLOSS_Console("sentinel(): called while in user mode, by process %d. Halting...\n", Current->pid);
USLOSS_Halt(1);
}
while (1)
{
checkDeadlock();
USLOSS_WaitInt();
}
} /* sentinel */
/* check to determine if deadlock has occurred... */
static void checkDeadlock()
{
int i;
int blocked = 1;
for( i = 0; i < MINPRIORITY; i++){ //loop through each priority
procPtr temp = ReadyLists[i];
if (temp != NULL) {
procPtr proc = ReadyLists[i];
while (proc != NULL) {
if (proc->status == READY || proc->status == RUNNING) {
fprintf(stderr, "checkDeadlock(): found another process (name: %s, pid: %d, status: %d) on the ready list.\n", proc->name, proc->pid, proc->status);
USLOSS_Halt(1);
}
blocked = blocked && (proc->status == JOINBLOCKED || proc->status == ZAPBLOCKED || proc->status > MEBLOCKED); //FIXME: maybe not 100% sure about this
proc = proc->nextProcPtr;
}
}
}
if (blocked) {
USLOSS_Console("checkDeadlock(): numProc = %d. Only Sentinel should be left. Halting...\n", countProcesses());
}
else {
USLOSS_Console("All processes completed.\n");
}
USLOSS_Halt(0);
} /* checkDeadlock */
/*
* Disables the interrupts.
*/
void disableInterrupts()
{
// turn the interrupts OFF iff we are in kernel mode
// if not in kernel mode, print an error message and
// halt USLOSS
if (isInKernelMode()) {
unsigned int psr = USLOSS_PsrGet();
unsigned int op = 0xfffffffd;
int result = USLOSS_PsrSet(psr & op);
if (result == USLOSS_ERR_INVALID_PSR) {
fprintf(stderr, "Failed to set PSR to kernel mode.");
USLOSS_Halt(0);
}
}
else {
fprintf(stderr, "Failed to disable interrupts as not in kernel mode.");
USLOSS_Halt(0);
}
// TODO: May need more than just switching the bit?
} /* disableInterrupts */
/*
* Returns 1 if in kernel mode, else 0.
*/
int isInKernelMode() {
unsigned int psr = USLOSS_PsrGet();
unsigned int op = 0x1;
return psr & op;
}
int enterKernelMode() {
unsigned int psr = USLOSS_PsrGet();
unsigned int op = 0x1;
int result = USLOSS_PsrSet(psr | op);
if (result == USLOSS_ERR_INVALID_PSR) {
return -1;
}
else {
return 0;
}
// TODO: May need more than just switching the bit?
}
int enterUserMode() {
unsigned int psr = USLOSS_PsrGet();
unsigned int op = 0xfffffffe;
int result = USLOSS_PsrSet(psr & op);
if (result == USLOSS_ERR_INVALID_PSR) {
return -1;
}
else {
return 0;
}
// TODO: May need more than just switching the bit?
}
/*
* Returns 1 if interrupts are enabled, else 0.
*/
int isInterruptEnabled() {
unsigned int psr = USLOSS_PsrGet();
unsigned int op = 0x2;
return (psr & op) >> 1;
}
int enableInterrupts() {
unsigned int psr = USLOSS_PsrGet();
unsigned int op = 0x2;
int result = USLOSS_PsrSet(psr | op);
if (result == USLOSS_ERR_INVALID_PSR) {
return -1;
}
else {
return 0;
}
// TODO: May need more than just switching the bit?
}
/*
Checks if process table is full
*/
int isProcessTableFull(){
for (int i = 0; i < MAXPROC; i++){
if (ProcTable[i].status == EMPTY){
return 0;
}
}
return 1;
}
/*
Scans for available pid
*/
unsigned int getNextPid(){
// USLOSS_Console("---next pid = %d before loop--\n", nextPid);
do {
nextPid++;
} while (ProcTable[(nextPid - 1) % MAXPROC].status != EMPTY);
// USLOSS_Console("---next pid = %d after loop--\n", nextPid);
return nextPid;
}
void initProcessTable(){
for (int i = 0; i < MAXPROC; i++){
ProcTable[i].status = EMPTY;
}
}
void initReadyLists(){
// TODO: do something maybe
}
void addProcToReadyLists(int procSlot, int priority){
int rl_index = priority - 1;
if (ReadyLists[rl_index] == NULL){
ReadyLists[rl_index] = &ProcTable[procSlot];
} else {
procPtr prev = NULL;
procPtr curr = ReadyLists[rl_index];
while (curr != NULL){
prev = curr;
curr = curr->nextProcPtr;
}
prev->nextProcPtr = &ProcTable[procSlot];
}
if (DEBUG && debugflag)
USLOSS_Console("fork1(): adding %s to readylist at priority %d\n", ReadyLists[rl_index]->name, priority);
}
void cleanProcess(procPtr proc) {
if (DEBUG && debugflag)
USLOSS_Console("cleanProcess(): removing %d from ReadyList\n", proc->pid);
//remove proc from ready list
if (ReadyLists[proc->priority -1] != NULL){
if (ReadyLists[proc->priority -1]->pid == proc->pid){
ReadyLists[proc->priority -1] = proc->nextProcPtr;
} else {
procPtr fore = ReadyLists[proc->priority -1]->nextProcPtr;
procPtr aft = ReadyLists[proc->priority -1];
while (fore != NULL && fore->pid != proc->pid){
aft = fore;
fore = fore->nextProcPtr;
}
aft->nextProcPtr = proc->nextProcPtr;
}
}
proc->nextProcPtr = NULL;
proc->childProcPtr = NULL;
proc->nextSiblingPtr = NULL;
proc->quitList = NULL;
proc->quitNext = NULL;
proc->parentPtr = NULL;
proc->quitStatus = 0;
proc->status = EMPTY;
proc->zapped = 0;
proc->zapperList = NULL;
proc->zapperNext = NULL;
proc->startTime = -1;
proc->totalTimeUsed = 0;
}
// its PID, parent’s PID, priority, process status (e.g. empty, running, ready, blocked, etc.), number of children, CPU time consumed, and na
void dumpProcesses() {
char * statuses[6];
statuses[EMPTY] = "EMPTY";
statuses[READY] = "READY";
statuses[RUNNING] = "RUNNING";
statuses[JOINBLOCKED] = "JOINBLOCKED";
statuses[ZAPBLOCKED] = "ZAPBLOCKED";
statuses[QUIT] = "QUIT";
USLOSS_Console(" SLOT PID NAME PARENTPID PRIORITY STATUS NUM CHILDREN NUM LIVE KIDS NUM JOINS TIME USED \n");
USLOSS_Console("------ ----- -------------- ----------- ---------- ------------ -------------- ------------- ----------- -----------\n");
for (int i = 0; i < MAXPROC; i++){
procPtr temp = &ProcTable[i];
int parentpid = temp->parentPtr == NULL? -1 : temp->parentPtr->pid;
if (temp->status > MEBLOCKED)
USLOSS_Console("%6d %5d %14s %11d %10d %12d %14d %13d %11d %11d\n", i, temp->pid, temp->name, parentpid, temp->priority, temp->status, temp->numKids, temp->numLiveKids, temp->numJoins, temp->totalTimeUsed);
else
USLOSS_Console("%6d %5d %14s %11d %10d %12s %14d %13d %11d %11d\n", i, temp->pid, temp->name, parentpid, temp->priority, statuses[temp->status], temp->numKids, temp->numLiveKids, temp->numJoins, temp->totalTimeUsed);
}
}
int zap(int pid) {
if (pid == Current->pid) {
fprintf(stderr, "zap(): process %d tried to zap itself. Halting...\n", Current->pid);
USLOSS_Halt(1);
}
int procSlot = (pid - 1) % MAXPROC;
if (ProcTable[procSlot].status == EMPTY || ProcTable[procSlot].pid != pid) {
fprintf(stderr, "zap(): process being zapped does not exist. Halting...\n");
USLOSS_Halt(1);
}
if(ProcTable[procSlot].status == QUIT) {
if (isZapped()) {
return -1;
}
else {
return 0;
}
}
ProcTable[procSlot].zapped = 1;
if (ProcTable[procSlot].zapperList == NULL) {
ProcTable[procSlot].zapperList = Current;
}
else {
procPtr prev = NULL;
procPtr curr = ProcTable[procSlot].zapperList;
while (curr != NULL) {
prev = curr;
curr = curr->zapperNext;
}
prev->zapperNext = Current;
}
Current->status = ZAPBLOCKED;
dispatcher();
if (isZapped()) {
return -1;
}
return 0;
}
int isZapped(void) {
return Current->zapped;
}
int getpid(void) {
return Current->pid;
}
int blockMe(int block_status) {
if ( !isInKernelMode() ) {
USLOSS_Console("blockMe(): called while in user mode, by process %d. Halting...\n", Current->pid);
USLOSS_Halt(1);
}
disableInterrupts();
if (block_status <= MEBLOCKED){
USLOSS_Console("blockMe(): cannot block process with status (%d) <= 10. Halting...\n", block_status);
USLOSS_Halt(1);
}
Current->status = block_status;
dispatcher();
if (isZapped()){
enableInterrupts();
return -1;
}
enableInterrupts();
return 0;
}
int unblockProc(int pid) {
if ( !isInKernelMode() ) {
USLOSS_Console("unblockProc(): called while in user mode, by process %d. Halting...\n", Current->pid);
USLOSS_Halt(1);
}
disableInterrupts();
//returns -2 if proc is the current process, does not exist, not me-blocked, or blocked on status <= 10
if (pid == Current->pid){
if (DEBUG && debugflag)
USLOSS_Console("unblockProc(): attempting to unblock Current process (pid %d).\n", pid);
enableInterrupts();
return -2;
}
procPtr proc = &ProcTable[(pid - 1) % MAXPROC];
if (proc->status == EMPTY){
if (DEBUG && debugflag)
USLOSS_Console("unblockProc(): attempting to unblock non existant process (pid %d does not exist).\n", pid);
enableInterrupts();
return -2;
}
if (proc->status <= MEBLOCKED){
if (DEBUG && debugflag)
USLOSS_Console("unblockProc(): attempting to unblock process %d with status (%d) <= 10 (not meblocked)\n", pid, proc->status);
enableInterrupts();
return -2;
}
if (DEBUG && debugflag)
USLOSS_Console("unblockProc(): unblocking process %d.\n", pid);
proc->status = READY; //change status to ready
if (!onReadyList(pid, proc->priority)){ //add to readylist if not already there
if (DEBUG && debugflag)
USLOSS_Console("unblockProc(): process %d not found on ready list[%d], adding now.\n", pid, proc->priority);
addProcToReadyLists((pid - 1) % MAXPROC, proc->priority); //procSlot, priority
}
dispatcher();
if (isZapped()){
enableInterrupts();
return -1;
}
enableInterrupts();
return 0;
}
int readtime(void) {
int status = 0;
int dev_status = USLOSS_DeviceInput(USLOSS_CLOCK_DEV, 0, &status);//
if (dev_status == USLOSS_DEV_OK) {
return status;
} else {
USLOSS_Console("Failed to read current time. Halting...\n");
USLOSS_Halt(1);
return -1; //so it compiles without warning
}
}
void clockHandler(int dev, void *arg) {
if (DEBUG && debugflag)
USLOSS_Console("clockHandler(): clock interrupt occurred");
timeSlice();