1 typedef unsigned char u8
;
2 typedef unsigned short u16
;
3 typedef unsigned long u32
;
5 /*********** MTX Multitasking System ************/
10 #define FREE 0 // proc statuc
19 int uss
, usp
; // at offsets 4, 6
21 int pid
; /* pid = 0 to NPROC-1 */
23 int pri
; /* scheduling priority */
24 int ppid
; /* parent pid */
31 int kstack
[SSIZE
]; // Kmode per process stack
34 PROC proc
[NPROC
], *running
, *freeList
, *readyQueue
, *sleepList
;
36 int procsize
= sizeof(PROC
);
40 char *pname
[]={"Sun", "Mercury", "Venus", "Earth", "Mars", "Jupiter",
41 "Saturn", "Uranus", "Neptune" };
52 printf("MTX initializing ....\n");
54 for (i
=0; i
< NPROC
; i
++){
56 proc
[i
].status
= FREE
;
57 proc
[i
].next
= (PROC
*)&proc
[(i
+1)];
59 strcpy(proc
[i
].name
, pname
[i
]);
61 proc
[NPROC
-1].next
= NULL
;
62 freeList
= &proc
[0]; // all PROC are FREE initially
68 p
= getproc(); // get a FREE PROC
71 p
->pri
= 0; /* lowest priority 0 */
72 p
->pid
= 0; /* process 0 or P0 */
74 p
->ppid
= running
->pid
; /* P0's parent is P0 */
77 printf("initialization complete\n");
81 /***********************************************************
82 kfork() creates a child proc and returns the child pid.
83 When scheduled to run, the child process resumes to body();
84 ************************************************************/
91 /*** get a PROC for child process: ***/
92 if ( (p
= getproc()) == NULL
){
93 printf("no more proc\n");
97 /* initialize the new proc and its stack */
99 p
->ppid
= running
->pid
;
101 p
->pri
= 1; // all of the same priority 1
104 // clear all SAVed registers on stack
106 p
->kstack
[SSIZE
-i
] = 0;
108 // fill in resume address
109 p
->kstack
[SSIZE
-1] = (int)body
;
111 // save stack TOP address in PROC
112 p
->ksp
= &(p
->kstack
[SSIZE
- 9]);
114 enqueue(&readyQueue
, p
);
118 /************** YOU WRITE C CODE TO DO THESE *****************
119 1. compute new proc's segment by pid:
120 pid=1 ==> 0x2000; 2 ==> 0x3000; 3 ==> 0x4000, etc.
121 2. load /u1 to segment
123 3. Initialize new proc's ustack AS IF it did INT 80 from its virtual
126 4. newProc.uss = segment; newProc.usp = (a value)
128 printf("Proc %d forked a child %d at segment=%x\n",
129 running->pid, p->pid, segment);
136 printf("P%d switch process\n", running->pid);
145 printf("kfork failed\n");
147 printf("P%d return from kfork() : child = %d\n",
151 /*****************************************
152 All proc share the same body function,
153 as if called by the process itself.
154 ******************************************/
160 printf("------------------------------------------\n");
161 printf("I am process P%d My parent=%d\n", running
->pid
, running
->ppid
);
163 color
= 0x000A + running
->pid
;
164 printf("******************************************\n");
165 printf("freeList = "); printList(freeList
);
166 printf("readyQueue = "); printList(readyQueue
);
167 printf("sleepList = "); printList(sleepList
);
168 printf("******************************************\n");
170 printf("input a command : [s|q|f|w|u] : ");
175 case 's' : do_switch(); break;
176 case 'q' : do_exit(100); break; /* no return */
177 case 'f' : do_kfork(); break;
178 case 'w' : do_wait(0); break;
179 case 'u' : goUmode(); break;
188 int set_vec(vector
, addr
) u16 vector
, addr
;
191 location
= vector
<< 2;
192 put_word(addr
, 0, location
);
193 put_word(0x1000,0,location
+2);
198 //*************** main() ***************
202 printf("\nWelcome to the MTX Operating System\n");
205 /********** WRITE YOUR C CODE TO **************
206 initialize vector 80 : (CS,PC)=(0x1000, _inth)
207 ***********************************************/
209 printf("P0 forks P1\n");
211 printf("P0 switches to P1\n");
213 printf("P0 resumes: all dead, happy ending!\n");
216 //******** scheduler *******************
220 if (running
->status
== READY
)
221 enqueue(&readyQueue
, running
);
223 running
= dequeue(&readyQueue
);