1 /*********** MTX Multitasking System ************/
3 #include "type.h" // type.h has PROC and all FS data structs
5 PROC proc
[NPROC
], *running
, *freeList
, *readyQueue
, *sleepList
;
7 int procsize
= sizeof(PROC
);
14 char *pname
[]={"Sun", "Mercury", "Venus", "Earth", "Mars", "Jupiter",
15 "Saturn", "Uranus", "Neptune" };
17 /**** queue.o wait.o fe.o are in mtxlib******
21 **********************************************/
30 printf("MTX initializing ....\n");
32 for (i
=0; i
< NPROC
; i
++){
34 proc
[i
].status
= FREE
;
35 proc
[i
].next
= (PROC
*)&proc
[(i
+1)];
36 strcpy(proc
[i
].name
, pname
[i
]);
38 proc
[NPROC
-1].next
= NULL
;
39 freeList
= &proc
[0]; // all PROC are FREE initially
47 p
= getproc(); // get a FREE PROC
50 p
->pri
= 0; /* lowest priority 0 */
51 p
->pid
= 0; /* process 0 or P0 */
53 p
->ppid
= running
->pid
; /* P0's parent is P0 */
59 printf("initialization complete\n");
62 int makeUimage(filename
, p
) char *filename
; PROC
*p
;
66 // make Umode image by loading /u1 into segment
67 segment
= (p
->pid
+ 1)*0x1000;
69 load(filename
, segment
);
71 /***** Fill in U mode information in proc *****/
73 /**************************************************
74 We know segment=0x2000 + index*0x1000 ====>
75 ustack is at the high end of this segment, say TOP.
76 We must make ustak contain:
77 1 2 3 4 5 6 7 8 9 10 11 12
78 flag uCS uPC ax bx cx dx bp si di es ds
79 0x0200 seg 0 0 0 0 0 0 0 0 seg seg
81 So, first a loop to set all to 0, then
82 put_word(seg, segment, -2*i); i=2,11,12;
83 **************************************************/
85 for (i
=1; i
<=12; i
++){
86 put_word(0, segment
, -2*i
);
89 put_word(0x0200, segment
, -2*1); /* flag */
90 put_word(segment
, segment
, -2*2); /* uCS */
91 put_word(segment
, segment
, -2*11); /* uES */
92 put_word(segment
, segment
, -2*12); /* uDS */
94 /* initial USP relative to USS */
101 /***********************************************************
102 kfork() creates a child proc and returns the child pid.
103 When scheduled to run, the child process resumes to body();
104 ************************************************************/
111 /*** get a PROC for child process: ***/
112 if ( (p
= getproc()) == NULL
){
113 printf("no more proc\n");
117 /* initialize the new proc and its stack */
119 p
->ppid
= running
->pid
;
121 p
->pri
= 1; // all of the same priority 1
123 // clear all SAVed registers on stack
125 p
->kstack
[SSIZE
-i
] = 0;
127 // fill in resume address
128 p
->kstack
[SSIZE
-1] = (int)goUmode
;
130 // save stack TOP address in PROC
131 p
->ksp
= &(p
->kstack
[SSIZE
- 9]);
133 enqueue(&readyQueue
, p
);
136 segment
= 0x1000*(p
->pid
+1);
137 makeUimage("/u1", p
);
139 p
->cwd
= running
->cwd
;
142 for (i
=0; i
<NFD
; i
++){
143 p
->fd
[i
] = running
->fd
[i
];
145 p
->fd
[i
]->refCount
++;
148 printf("Proc %d forked a child %d at segment=%x\n",
149 running
->pid
, p
->pid
, segment
);
156 printf("P%d switch process\n", running
->pid
);
165 printf("kfork failed\n");
167 printf("P%d return from kfork() : child = %d\n",
171 /*****************************************
172 All proc share the same body function,
173 as if called by the process itself.
174 ******************************************/
178 printf("input an event # (0-9) to sleep on : ");
186 printf("input an event # (0-9) to wakeup : ");
196 printf("------------------------------------------\n");
197 printf("I am process P%d My parent=%d\n", running
->pid
, running
->ppid
);
199 color
= 0x000A + running
->pid
;
200 printf("******************************************\n");
201 printf("freeList = "); printList(freeList
);
202 printf("readyQueue = "); printList(readyQueue
);
203 printf("sleepList = "); printList(sleepList
);
204 printf("******************************************\n");
206 printf("input a command : [s|q|f|w|u] : ");
211 case 's' : do_switch(); break;
212 case 'q' : do_exit(100); break; /* no return */
213 case 'f' : do_kfork(); break;
214 case 'w' : do_wait(0); break;
215 case 'u' : goUmode(); break;
222 int int80h(), kbinth(), s0inth();
224 int set_vec(vector
, addr
) ushort vector
, addr
;
227 location
= vector
<< 2;
228 put_word(addr
, 0, location
);
229 put_word(0x1000,0,location
+2);
232 //*************** main() ***************
237 printf("\nWelcome to the MTX Operating System\n");
244 printf("P0 forks P1\n");
246 printf("P0 switches to P1\n");
248 printf("P0 resumes: all dead, happy ending!\n");
251 //******** scheduler *******************
255 if (running
->status
== READY
)
256 enqueue(&readyQueue
, running
);
258 running
= dequeue(&readyQueue
);