-
Notifications
You must be signed in to change notification settings - Fork 0
/
os345.c
670 lines (566 loc) · 16.3 KB
/
os345.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
// os345.c - OS Kernel 09/12/2013
// ***********************************************************************
// ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER **
// ** **
// ** The code given here is the basis for the BYU CS345 projects. **
// ** It comes "as is" and "unwarranted." As such, when you use part **
// ** or all of the code, it becomes "yours" and you are responsible to **
// ** understand any algorithm or method presented. Likewise, any **
// ** errors or problems become your responsibility to fix. **
// ** **
// ** NOTES: **
// ** -Comments beginning with "// ??" may require some implementation. **
// ** -Tab stops are set at every 3 spaces. **
// ** -The function API's in "OS345.h" should not be altered. **
// ** **
// ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER **
// ***********************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <time.h>
#include <assert.h>
#include "os345.h"
#include "os345signals.h"
#include "os345config.h"
#include "os345lc3.h"
#include "os345fat.h"
#define my_printf printf
// **********************************************************************
// local prototypes
//
void pollInterrupts(void);
static int scheduler(void);
static int dispatcher(void);
//static void keyboard_isr(void);
//static void timer_isr(void);
int sysKillTask(int taskId);
static int initOS(void);
// **********************************************************************
// **********************************************************************
// global semaphores
Semaphore* semaphoreList; // linked list of active semaphores
Semaphore* keyboard; // keyboard semaphore
Semaphore* charReady; // character has been entered
Semaphore* inBufferReady; // input buffer ready semaphore
Semaphore* tics1sec; // 1 second semaphore
Semaphore* tics10thsec; // 1/10 second semaphore
Semaphore* tics10sec; // 10 second semaphore
// **********************************************************************
// **********************************************************************
// global system variables
TCB tcb[MAX_TASKS]; // task control block
Semaphore* taskSems[MAX_TASKS]; // task semaphore
jmp_buf k_context; // context of kernel stack
jmp_buf reset_context; // context of kernel stack
volatile void* temp; // temp pointer used in dispatcher
int scheduler_mode; // scheduler mode
int superMode; // system mode
int curTask; // current task #
long swapCount; // number of re-schedule cycles
char inChar; // last entered character
int charFlag; // 0 => buffered input
int inBufIndx; // input pointer into input buffer
int inBufLen;
int inBufLenPrev;
int inBufIndxPrev;
char inBuffer[INBUF_SIZE+1]; // character input buffer
char inBufferPrev[INBUF_SIZE + 1];
//Message messages[NUM_MESSAGES]; // process message buffers
int pollClock; // current clock()
int lastPollClock; // last pollClock
bool diskMounted; // disk has been mounted
time_t oldTime1; // old 1sec time
time_t oldTime2;
clock_t myClkTime;
clock_t myOldClkTime;
PQ readyQ;
// **********************************************************************
// **********************************************************************
// OS startup
//
// 1. Init OS
// 2. Define reset longjmp vector
// 3. Define global system semaphores
// 4. Create CLI task
// 5. Enter scheduling/idle loop
//
int main(int argc, char* argv[])
{
// save context for restart (a system reset would return here...)
int resetCode = setjmp(reset_context);
superMode = TRUE; // supervisor mode
switch (resetCode)
{
case POWER_DOWN_QUIT: // quit
powerDown(0);
printf("\nGoodbye!!");
return 0;
case POWER_DOWN_RESTART: // restart
powerDown(resetCode);
printf("\nRestarting system...\n");
case POWER_UP: // startup
break;
default:
printf("\nShutting down due to error %d", resetCode);
powerDown(resetCode);
return resetCode;
}
// output header message
printf("%s", STARTUP_MSG);
// initalize OS
if ( resetCode = initOS()) return resetCode;
// create global/system semaphores here
//?? vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
charReady = createSemaphore("charReady", BINARY, 0);
inBufferReady = createSemaphore("inBufferReady", BINARY, 0);
keyboard = createSemaphore("keyboard", BINARY, 1);
tics1sec = createSemaphore("tics1sec", BINARY, 0);
tics10thsec = createSemaphore("tics10thsec", BINARY, 0);
tics10sec = createSemaphore("tics10sec", COUNTING, 0);
//?? ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// schedule CLI task
createTask("myShell", // task name
P1_shellTask, // task
MED_PRIORITY, // task priority
argc, // task arg count
argv); // task argument pointers
// HERE WE GO................
// Scheduling loop
// 1. Check for asynchronous events (character inputs, timers, etc.)
// 2. Choose a ready task to schedule
// 3. Dispatch task
// 4. Loop (forever!)
while(1) // scheduling loop
{
// check for character / timer interrupts
pollInterrupts();
// schedule highest priority ready task
if ((curTask = scheduler()) < 0) continue;
// dispatch curTask, quit OS if negative return
if (dispatcher() < 0) break;
} // end of scheduling loop
// exit os
longjmp(reset_context, POWER_DOWN_QUIT);
return 0;
} // end main
// **********************************************************************
// **********************************************************************
// scheduler
//
static int scheduler()
{
int nextTask;
obj task;
// ?? Design and implement a scheduler that will select the next highest
// ?? priority ready task to pass to the system dispatcher.
// ?? WARNING: You must NEVER call swapTask() from within this function
// ?? or any function that it calls. This is because swapping is
// ?? handled entirely in the swapTask function, which, in turn, may
// ?? call this function. (ie. You would create an infinite loop.)
// ?? Implement a round-robin, preemptive, prioritized scheduler.
// ?? This code is simply a round-robin scheduler and is just to get
// ?? you thinking about scheduling. You must implement code to handle
// ?? priorities, clean up dead tasks, and handle semaphores appropriately.
// schedule next task
if (scheduler_mode == 0)
{
task = deQ(&readyQ, -1);
nextTask = task.taskId;
if (nextTask >= 0)
{
enQ(&readyQ, task);
}
// mask sure nextTask is valid
while (!tcb[nextTask].name)
{
if (++nextTask >= MAX_TASKS) nextTask = 0;
}
if (tcb[nextTask].signal & mySIGSTOP) return -1;
return nextTask;
}
else
{
task = deQ(&readyQ, -1);
nextTask = task.taskId;
if (nextTask >= 0)
{
enQ(&readyQ, task);
}
if (tcb[task.taskId].taskTime > 0)
{
tcb[task.taskId].taskTime--;
return nextTask;
}
else
{
if(Zero(&readyQ))
{
setSchedule(&readyQ);
task = deQ(&readyQ, -1);
nextTask = task.taskId;
if (nextTask >= 0)
{
enQ(&readyQ, task);
}
tcb[task.taskId].taskTime--;
return nextTask;
}
else
{
while (tcb[task.taskId].taskTime < 1)
{
task = deQ(&readyQ, -1);
nextTask = task.taskId;
if (nextTask >= 0)
{
enQ(&readyQ, task);
}
}
while (!tcb[nextTask].name)
{
if (++nextTask >= MAX_TASKS) nextTask = 0;
}
if (tcb[nextTask].signal & mySIGSTOP) return -1;
tcb[task.taskId].taskTime--;
return nextTask;
}
}
}
} // end scheduler
// **********************************************************************
// **********************************************************************
// dispatch curTask
//
static int dispatcher()
{
int result;
if (tcb[curTask].signal == mySIGSTOP)
{
return 0;
}
// schedule task
switch(tcb[curTask].state)
{
case S_NEW:
{
// new task
printf("\nNew Task[%d] %s", curTask, tcb[curTask].name);
tcb[curTask].state = S_RUNNING; // set task to run state
// save kernel context for task SWAP's
if (setjmp(k_context))
{
superMode = TRUE; // supervisor mode
break; // context switch to next task
}
// move to new task stack (leave room for return value/address)
temp = (int*)tcb[curTask].stack + (STACK_SIZE-8);
SET_STACK(temp);
superMode = FALSE; // user mode
// begin execution of new task, pass argc, argv
result = (*tcb[curTask].task)(tcb[curTask].argc, tcb[curTask].argv);
// task has completed
if (result) printf("\nTask[%d] returned %d", curTask, result);
else printf("\nTask[%d] returned %d", curTask, result);
tcb[curTask].state = S_EXIT; // set task to exit state
// return to kernal mode
longjmp(k_context, 1); // return to kernel
}
case S_READY:
{
tcb[curTask].state = S_RUNNING; // set task to run
}
case S_RUNNING:
{
if (setjmp(k_context))
{
// SWAP executed in task
superMode = TRUE; // supervisor mode
break; // return from task
}
if (signals()) break;
longjmp(tcb[curTask].context, 3); // restore task context
}
case S_BLOCKED:
{
// ?? Could check here to unblock task
break;
}
case S_EXIT:
{
if (curTask == 0) return -1; // if CLI, then quit scheduler
// release resources and kill task
sysKillTask(curTask); // kill current task
break;
}
default:
{
printf("Unknown Task[%d] State", curTask);
longjmp(reset_context, POWER_DOWN_ERROR);
}
}
return 0;
} // end dispatcher
// **********************************************************************
// **********************************************************************
// Do a context switch to next task.
// 1. If scheduling task, return (setjmp returns non-zero value)
// 2. Else, save current task context (setjmp returns zero value)
// 3. Set current task state to READY
// 4. Enter kernel mode (longjmp to k_context)
void swapTask()
{
assert("SWAP Error" && !superMode); // assert user mode
// increment swap cycle counter
swapCount++;
// either save current task context or schedule task (return)
if (setjmp(tcb[curTask].context))
{
superMode = FALSE; // user mode
return;
}
// context switch - move task state to ready
if (tcb[curTask].state == S_RUNNING) tcb[curTask].state = S_READY;
// move to kernel mode (reschedule)
longjmp(k_context, 2);
} // end swapTask
// **********************************************************************
// **********************************************************************
// system utility functions
// **********************************************************************
// **********************************************************************
// **********************************************************************
// **********************************************************************
// initialize operating system
static int initOS()
{
int i;
// make any system adjustments (for unblocking keyboard inputs)
INIT_OS
// reset system variables
curTask = 0; // current task #
swapCount = 0; // number of scheduler cycles
scheduler_mode = 0; // default scheduler
inChar = 0; // last entered character
charFlag = 0; // 0 => buffered input
inBufIndx = 0; // input pointer into input buffer
semaphoreList = 0; // linked list of active semaphores
diskMounted = 0; // disk has been mounted
// malloc ready queue and blocked queue
init_PQ(&readyQ);
//rq = (int*)malloc(MAX_TASKS * sizeof(int));
if (&readyQ == NULL) return 99;
// capture current time
lastPollClock = clock(); // last pollClock
time(&oldTime1);
time(&oldTime2);
// init system tcb's
for (i=0; i<MAX_TASKS; i++)
{
tcb[i].name = NULL; // tcb
taskSems[i] = NULL; // task semaphore
}
// init tcb
for (i=0; i<MAX_TASKS; i++)
{
tcb[i].name = NULL;
}
// initialize lc-3 memory
initLC3Memory(LC3_MEM_FRAME, 0xF800>>6);
// ?? initialize all execution queues
return 0;
} // end initOS
// **********************************************************************
// **********************************************************************
// Causes the system to shut down. Use this for critical errors
void powerDown(int code)
{
int i;
printf("\nPowerDown Code %d", code);
// release all system resources.
printf("\nRecovering Task Resources...");
// kill all tasks
for (i = MAX_TASKS-1; i >= 0; i--)
if(tcb[i].name) sysKillTask(i);
// delete all semaphores
while (semaphoreList)
deleteSemaphore(&semaphoreList);
// free ready queue
freeQ(&readyQ);
// ?? release any other system resources
// ?? deltaclock (project 3)
RESTORE_OS
return;
} // end powerDown
void init_obj(obj* item, int priority, int taskId)
{
item->priority = priority;
item->taskId = taskId;
}
void init_PQ(PQ* pq)
{
pq->items = (obj*)malloc(sizeof(obj) * MAX_TASKS);
pq->count = 0;
}
void enQ(PQ* pq, obj item)
{
pq->items[pq->count] = item;
for (int i = pq->count; (i > 0) && ((pq->items[i].priority) > (pq->items[i - 1].priority)); i--)
{
obj save = pq->items[i - 1];
pq->items[i - 1] = pq->items[i];
pq->items[i] = save;
}
pq->count++;
}
bool Zero(PQ* pq)
{
bool allZero = TRUE;
for (int i = 0; i < pq->count; i++)
{
if (tcb[pq->items[i].taskId].taskTime != 0)
{
allZero = FALSE;
}
}
return allZero;
}
void setSchedule(PQ* pq)
{
int j = 0;
int parent_num = 0;
int parentId[] = { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 };
int children[MAX_TASKS];
for (int i = 0; i < MAX_TASKS; i++)
{
children[i] = 0;
}
for (int i = 0; i < pq->count; i++)
{
//printf("task id: %d name: %s \n", pq->items[i].taskId, tcb[pq->items[i].taskId].name);
if (tcb[pq->items[i].taskId].parent == 0)
{
parentId[j] = pq->items[i].taskId;
children[pq->items[i].taskId]++;
j++;
}
else
{
children[tcb[pq->items[i].taskId].parent]++;
}
}
int greatest = children[parentId[0]];
for (int i = 0; i < j; i++)
{
if (children[parentId[i]] > greatest)
{
greatest = children[parentId[i]];
}
}
int units_per_parent[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int units_per_child[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (int i = 0; i < j; i++)
{
int base = greatest / (children[parentId[i]]);
units_per_parent[i] = base + (greatest % (children[parentId[i]]));
units_per_child[i] = base;
}
for (int i = 0; i < pq->count; i++)
{
if (tcb[pq->items[i].taskId].parent == 0)
{
for (int k = 0; k < j; k++)
{
if (pq->items[i].taskId == parentId[k])
{
tcb[pq->items[i].taskId].taskTime = units_per_parent[k];
}
}
}
else
{
for (int k = 0; k < j; k++)
{
if (tcb[pq->items[i].taskId].parent == parentId[k])
{
tcb[pq->items[i].taskId].taskTime = units_per_child[k];
}
}
}
}
tcb[0].taskTime = greatest;
}
obj deQ(PQ* pq, int type)
{
obj toReturn;
if (type == -1)
{
if (pq->count == 0)
{
init_obj(&toReturn, -1, -1);
return toReturn;
}
else
{
toReturn = pq->items[0];
for (int i = 0; i < pq->count; i++)
{
pq->items[i] = pq->items[i + 1];
}
pq->count--;
return toReturn;
}
}
else
{
int index = -1;
for (int i = 0; i < pq->count; i++)
{
if (pq->items[i].taskId == type)
{
index = i;
}
}
if (index == -1)
{
init_obj(&toReturn, -1, -1);
return toReturn;
}
else
{
toReturn = pq->items[index];
for (int i = index; i < pq->count; i++)
{
pq->items[i] = pq->items[i + 1];
}
pq->count--;
return toReturn;
}
}
}
void freeQ(PQ* pq)
{
free(pq->items);
}
void printQ(PQ* pq)
{
for (int i = 0; i < pq->count; i++)
{
int tid = pq->items[i].taskId;
if (tcb[tid].name)
{
printf("\n%4d/%-4d%20s%4d ", tid, tcb[tid].parent,
tcb[tid].name, tcb[tid].priority);
if (tcb[tid].signal & mySIGSTOP) my_printf("Paused");
else if (tcb[tid].state == S_NEW) my_printf("New");
else if (tcb[tid].state == S_READY) my_printf("Ready");
else if (tcb[tid].state == S_RUNNING) my_printf("Running");
else if (tcb[tid].state == S_BLOCKED) my_printf("Blocked %s",
tcb[tid].name);
else if (tcb[tid].state == S_EXIT) my_printf("Exiting");
swapTask();
}
}
}