* added compilers lcc and bcc (linux86)
[mascara-docs.git] / i86 / mtx / mtx / samples / LAB5_FS / t.c
blob536496789096bd9b9e03964ebe80e6bf801e1ad3
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);
8 int nproc, color;
9 int inkmode = 1;
11 extern MINODE *root;
13 int body();
14 char *pname[]={"Sun", "Mercury", "Venus", "Earth", "Mars", "Jupiter",
15 "Saturn", "Uranus", "Neptune" };
17 /**** queue.o wait.o fe.o are in mtxlib******
18 #include "queue.c"
19 #include "wait.c"
20 #include "forkexec.c"
21 **********************************************/
22 #include "vid.c"
23 #include "kbd.c"
24 #include "int.c"
26 int initialize()
28 int i; PROC *p;
30 printf("MTX initializing ....\n");
32 for (i=0; i < NPROC; i++){
33 proc[i].pid = 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
41 readyQueue = 0;
42 sleepList = 0;
44 fs_init();
46 // create P0
47 p = getproc(); // get a FREE PROC
49 p->status = READY;
50 p->pri = 0; /* lowest priority 0 */
51 p->pid = 0; /* process 0 or P0 */
52 running = p;
53 p->ppid = running->pid; /* P0's parent is P0 */
54 nproc = 1;
56 p->cwd = root;
57 for (i=0; i<NFD; i++)
58 p->fd[i] = 0;
59 printf("initialization complete\n");
62 int makeUimage(filename, p) char *filename; PROC *p;
64 ushort i, segment;
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 */
95 p->usp = -2*12;
96 p->uss = segment;
97 return 0;
100 int goUmode();
101 /***********************************************************
102 kfork() creates a child proc and returns the child pid.
103 When scheduled to run, the child process resumes to body();
104 ************************************************************/
105 int kfork()
107 PROC *p;
108 int i, child;
109 ushort segment;
111 /*** get a PROC for child process: ***/
112 if ( (p = getproc()) == NULL){
113 printf("no more proc\n");
114 return(-1);
117 /* initialize the new proc and its stack */
118 p->status = READY;
119 p->ppid = running->pid;
120 p->parent = running;
121 p->pri = 1; // all of the same priority 1
123 // clear all SAVed registers on stack
124 for (i=1; i<10; i++)
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);
134 nproc++;
136 segment = 0x1000*(p->pid+1);
137 makeUimage("/u1", p);
139 p->cwd = running->cwd;
140 p->cwd->refCount++;
142 for (i=0; i<NFD; i++){
143 p->fd[i] = running->fd[i];
144 if (p->fd[i] != 0)
145 p->fd[i]->refCount++;
148 printf("Proc %d forked a child %d at segment=%x\n",
149 running->pid, p->pid, segment);
151 return(p->pid);
154 int do_switch()
156 printf("P%d switch process\n", running->pid);
157 tswitch();
160 int do_kfork()
162 int new;
163 new = kfork();
164 if (new < 0)
165 printf("kfork failed\n");
166 else
167 printf("P%d return from kfork() : child = %d\n",
168 running->pid, new);
171 /*****************************************
172 All proc share the same body function,
173 as if called by the process itself.
174 ******************************************/
175 int do_sleep()
177 char c;
178 printf("input an event # (0-9) to sleep on : ");
179 c = getc() - '0';
180 sleep(c);
183 int do_wk()
185 char i;
186 printf("input an event # (0-9) to wakeup : ");
187 i = getc() - '0';
188 wakeup(i);
192 int body()
194 char c;
195 while(1){
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] : ");
207 c = getc();
208 printf("%c\n", c);
210 switch(c){
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;
217 default : break;
222 int int80h(), kbinth(), s0inth();
224 int set_vec(vector, addr) ushort vector, addr;
226 ushort location,cs;
227 location = vector << 2;
228 put_word(addr, 0, location);
229 put_word(0x1000,0,location+2);
232 //*************** main() ***************
233 main()
235 vid_init();
237 printf("\nWelcome to the MTX Operating System\n");
238 initialize();
240 set_vec(80, int80h);
241 set_vec(9, kbinth);
242 kbinit();
244 printf("P0 forks P1\n");
245 kfork();
246 printf("P0 switches to P1\n");
247 tswitch();
248 printf("P0 resumes: all dead, happy ending!\n");
251 //******** scheduler *******************
253 int scheduler()
255 if (running->status == READY)
256 enqueue(&readyQueue, running);
258 running = dequeue(&readyQueue);