/
t.c
92 lines (83 loc) · 2.08 KB
/
t.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
#include "header.h"
/**************************************************
bio.o, queue.o loader.o are in mtxlib
**************************************************/
int body(void)
{
char c, str[64];
printf("proc %d resumes to body()\n\r", running->pid);
showLists();
while(1)
{
printf("\rproc %d running : enter a key [s|f|z|a|w|q|u|p|l]: ", running->pid);
c = getc();
printf("%c\n\r", c);
switch(c)
{
case 's': tswitch(); break;
case 'q': do_exit(); break;
case 'f': kfork("/bin/u1"); break;
case 'z': do_sleep(); break;
case 'a': do_wake(); break;
case 'w': do_wait(); break;
case 'u': goUmode(); break;
case 'p': do_ps(); break;
case 'l': showLists();break;
default: break;
}
}
}
int init()
{
PROC *p; int i;
color = 0x0C;
printf("init ....");
for (i=0; i<NPROC; i++){ // initialize all procs
p = &proc[i];
p->pid = i;
p->status = FREE;
p->priority = 0;
strcpy(proc[i].name, pname[i]);
p->next = &proc[i+1];
}
freeList = &proc[0]; // all procs are in freeList
proc[NPROC-1].next = 0;
readyQueue = sleepList = 0;
/**** create P0 as running ******/
p = get_proc(&freeList, FREE);
p->status = RUNNING;
p->ppid = 0;
p->parent = p;
running = p;
nproc = 1;
running->status = READY;
printf("done\n\r");
}
int scheduler()
{
if (running->status == READY)
{
enqueue(&readyQueue, running);
}
running = dequeue(&readyQueue);
color = running->pid + 1;
}
int int80h();
int set_vector(u16 vector , u16 handler)
{
put_word(handler, 0, vector<<2);
put_word(0x1000, 0,(vector<<2) + 2);
}
main()
{
printf("MTX starts in main()\n\r");
init(); // initialize and create P0 as running
set_vector(80, (int)int80h);
kfork("/bin/u1"); // P0 kfork() P1
while(1){
printf("P0 running\n\r");
while(!readyQueue);
printf("P0 switch process\n\r");
tswitch(); // P0 switch to run P1
}
}