* added compilers lcc and bcc (linux86)
[mascara-docs.git] / i86 / mtx / mtx / samples / LAB4 / t.c
blob20567dbca0d7a4f0b1642f41e93f98100406b3f7
1 typedef unsigned char u8;
2 typedef unsigned short u16;
3 typedef unsigned long u32;
5 /*********** MTX Multitasking System ************/
6 #define NULL 0
7 #define NPROC 9
8 #define SSIZE 1024
10 #define FREE 0 // proc statuc
11 #define READY 1
12 #define SLEEP 2
13 #define ZOMBIE 3
15 typedef struct proc{
16 struct proc *next;
17 int *ksp;
19 int uss, usp; // at offsets 4, 6
21 int pid; /* pid = 0 to NPROC-1 */
22 int status;
23 int pri; /* scheduling priority */
24 int ppid; /* parent pid */
25 struct proc *parent;
27 int event;
28 int exitValue;
29 char name[32];
31 int kstack[SSIZE]; // Kmode per process stack
32 }PROC;
34 PROC proc[NPROC], *running, *freeList, *readyQueue, *sleepList;
36 int procsize = sizeof(PROC);
37 int nproc, color;
39 int body();
40 char *pname[]={"Sun", "Mercury", "Venus", "Earth", "Mars", "Jupiter",
41 "Saturn", "Uranus", "Neptune" };
43 #include "queue.c"
44 #include "wait.c"
45 #include "int.c"
48 int initialize()
50 int i; PROC *p;
52 printf("MTX initializing ....\n");
54 for (i=0; i < NPROC; i++){
55 proc[i].pid = 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
64 readyQueue = 0;
65 sleepList = 0;
67 // create P0
68 p = getproc(); // get a FREE PROC
70 p->status = READY;
71 p->pri = 0; /* lowest priority 0 */
72 p->pid = 0; /* process 0 or P0 */
73 running = p;
74 p->ppid = running->pid; /* P0's parent is P0 */
75 nproc = 1;
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 ************************************************************/
85 int kfork()
87 PROC *p;
88 int i, child;
89 u16 segment;
91 /*** get a PROC for child process: ***/
92 if ( (p = getproc()) == NULL){
93 printf("no more proc\n");
94 return(-1);
97 /* initialize the new proc and its stack */
98 p->status = READY;
99 p->ppid = running->pid;
100 p->parent = running;
101 p->pri = 1; // all of the same priority 1
104 // clear all SAVed registers on stack
105 for (i=1; i<10; i++)
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);
115 nproc++;
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
124 address 0
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);
130 return(p->pid);
134 int do_switch()
136 printf("P%d switch process\n", running->pid);
137 tswitch();
140 int do_kfork()
142 int new;
143 new = kfork();
144 if (new < 0)
145 printf("kfork failed\n");
146 else
147 printf("P%d return from kfork() : child = %d\n",
148 running->pid, new);
151 /*****************************************
152 All proc share the same body function,
153 as if called by the process itself.
154 ******************************************/
156 int body()
158 char c;
159 while(1){
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] : ");
171 c = getc();
172 printf("%c\n", c);
174 switch(c){
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;
181 default : break;
186 int int80h();
188 int set_vec(vector, addr) u16 vector, addr;
190 u16 location,cs;
191 location = vector << 2;
192 put_word(addr, 0, location);
193 put_word(0x1000,0,location+2);
198 //*************** main() ***************
199 main()
201 color = 0x000A;
202 printf("\nWelcome to the MTX Operating System\n");
203 initialize();
205 /********** WRITE YOUR C CODE TO **************
206 initialize vector 80 : (CS,PC)=(0x1000, _inth)
207 ***********************************************/
209 printf("P0 forks P1\n");
210 kfork();
211 printf("P0 switches to P1\n");
212 tswitch();
213 printf("P0 resumes: all dead, happy ending!\n");
216 //******** scheduler *******************
218 int scheduler()
220 if (running->status == READY)
221 enqueue(&readyQueue, running);
223 running = dequeue(&readyQueue);