* added compilers lcc and bcc (linux86)
[mascara-docs.git] / i86 / mtx / mtx / samples / LAB5.timer / t.c
bloba5d894581fa9d2fc94a27882534d91c68e145749
1 /*********** MTX Multitasking System ************/
3 /***********************************************************
4 #define NULL 0
5 #define NPROC 9
6 #define SSIZE 1024
7 #define NQUEUE NPROC // at most NPROC ready queues
9 #define FREE 0 // proc statuc
10 #define READY 1
11 #define SLEEP 2
12 #define ZOMBIE 3
13 #define KILLED 4
15 // For KUmode: PROC added uss,usp at offsets 4,6.
16 // Then, name[32], exitValue.
18 typedef struct proc{
19 struct proc *next;
20 int *ksp;
22 int uss, usp; // at 4, 6
24 int pid;
25 int status;
26 int pri;
27 int ppid;
29 struct proc *parent;
30 int event;
31 char *deathCry;
33 int exitValue;
34 char name[32];
36 int time; // for Umode running time slice
37 int killed;
38 int kstack[SSIZE]; // Kmode per process stack
39 }PROC;
41 **************************************************************/
42 #include "type.h"
44 struct rqueue rqueue[NQUEUE]; // at most NPROC separate ready queues
46 PROC proc[NPROC], *running, *freeList, *sleepList;
48 int procsize = sizeof(PROC);
49 int nproc, color;
51 int inkmode = 1; // ADD KUmode transition flag; start in Kmode
53 int body();
54 char *pname[]={"Sun", "Mercury", "Venus", "Earth", "Mars", "Jupiter",
55 "Saturn", "Uranus", "Neptune" };
58 /**********************************************************
59 #include "io.c"
60 #include "queue.c"
61 #include "wait.c"
62 #include "loader.c"
63 #include "forkexec.c"
64 *********************************************************/
66 #include "int.c"
67 #include "vid.c"
68 #include "timer.c"
70 int initialize()
72 int i; PROC *p;
74 printf("MTX initializing ....\n");
76 for (i=0; i < NPROC; i++){
77 proc[i].pid = i;
78 proc[i].status = FREE;
79 proc[i].next = (PROC *)&proc[(i+1)];
81 strcpy(proc[i].name, pname[i]);
83 proc[NPROC-1].next = NULL;
84 freeList = &proc[0]; // all PROC are FREE initially
86 for (i=0; i<NQUEUE; i++){ // initialize the scheduling queues
87 rqueue[i].priority = i;
88 rqueue[i].queue = 0;
90 sleepList = 0;
92 // create P0
93 p = getproc(); // get a FREE PROC
95 p->status = READY;
96 p->pri = 0; /* lowest priority 0 */
97 p->pid = 0; /* process 0 or P0 */
98 running = p;
99 p->ppid = running->pid; /* P0's parent is P0 */
100 nproc = 1;
102 printf("initialization complete\n");
106 int body()
108 char c;
109 while(1){
110 printf("------------------------------------------\n");
111 printf("I am process P%d My parent=%d\n", running->pid, running->ppid);
113 //color = 0x0001 + running->pid;
114 printf("******************************************\n");
115 printf("freeList = "); printList(freeList);
116 printQ();
117 printsleep();
118 printf("******************************************\n");
120 printf("input a command : [s|q|f|w|u] : ");
121 c = getc();
122 printf("%c\n", c);
124 switch(c){
125 case 's' : do_switch(); break;
126 case 'q' : do_exit(100); break; /* no return */
127 case 'f' : do_kfork(); break;
128 case 'w' : do_wait(0); break;
129 case 'u' : goUmode(); break;
131 default : break;
136 int int80h();
138 int set_vec(vector, addr) ushort vector, addr;
140 ushort location,cs;
141 location = vector << 2;
142 put_word(addr, 0, location);
143 put_word(0x1000,0,location+2);
146 int tinth();
148 //*************** main() ***************
149 main()
152 vid_init();
153 printf("vid_init : console display driver initialized\n");
155 printf("\nWelcome to the MTX Operating System\n");
157 initialize();
159 set_vec(80, int80h);
161 printf("P0 forks P1\n");
162 kfork();
164 lock();
165 set_vec( 8, tinth);
166 timer_init();
168 printf("P0 switches to P1\n");
170 while(1){
171 if (rqueue[1].queue)
172 tswitch();
174 printf("P0 resumes: all dead, happy ending!\n");
177 //******** scheduler *******************
179 int scheduler()
181 PROC *p;
182 int i;
184 if (running->status == READY)
185 enqueue(running);
187 for (i=NQUEUE-1; i>=0; i--){
188 running = dequeue(&rqueue[i].queue);
189 if (running)
190 break;
193 running->time = 5;
194 color = 0x09 + running->pid;
195 printf("next running=%d time : ", running->pid);