* cormen book
[mascara-docs.git] / i86 / mtx-16-bit / mtx / samples / LAB2 / t.c
blob44eb930f330f6a27d2849009c337aef1fba4ee27
1 /************ t.c file **********************************/
2 #define NPROC 9
3 #define SSIZE 1024 /* kstack int size */
5 #define DEAD 0 /* proc status */
6 #define READY 1
8 typedef struct proc{
9 struct proc *next;
10 int ksp; /* saved sp; offset = 2 */
11 int pid;
12 int status; /* READY|DEAD, etc */
13 int kstack[SSIZE]; // kmode stack of task
14 }PROC;
16 #include "io.c" /* <===== use YOUR OWN io.c with printf() ****/
18 PROC proc[NPROC], *running;
20 int procSize = sizeof(PROC);
22 /****************************************************************
23 Initialize the proc's as shown:
24 running ---> proc[0] -> proc[1];
26 proc[1] to proc[N-1] form a circular list:
28 proc[1] --> proc[2] ... --> proc[NPROC-1] -->
29 ^ |
30 |<---------------------------------------<-
32 Each proc's kstack contains:
33 retPC, ax, bx, cx, dx, bp, si, di, flag; all 2 bytes
34 *****************************************************************/
36 int body();
38 int initialize()
40 int i, j;
41 PROC *p;
43 for (i=0; i < NPROC; i++){
44 p = &proc[i];
45 p->next = &proc[i+1];
46 p->pid = i;
47 p->status = READY;
49 if (i){ // initialize kstack[ ] of proc[1] to proc[N-1]
50 for (j=1; j<10; j++)
51 p->kstack[SSIZE - j] = 0; // all saved registers = 0
52 p->kstack[SSIZE-1]=(int)body; // called tswitch() from body
53 p->ksp = &(p->kstack[SSIZE-9]); // ksp -> kstack top
56 running = &proc[0];
57 proc[NPROC-1].next = &proc[1];
58 printf("initialization complete\n");
61 char *gasp[NPROC]={
62 "Oh! You are killing me .......",
63 "Oh! I am dying ...............",
64 "Oh! I am a goner .............",
65 "Bye! Bye! World...............",
68 int grave(){
69 printf("\n*****************************************\n");
70 printf("Task %d %s\n", running->pid,gasp[(running->pid) % 4]);
71 printf("*****************************************\n");
72 running->status = DEAD;
74 tswitch(); /* journey of no return */
77 int ps()
79 PROC *p;
81 printf("running = %d\n", running->pid);
83 p = running;
84 p = p->next;
85 printf("readyProcs = ");
86 while(p != running && p->status==READY){
87 printf("%d -> ", p->pid);
88 p = p->next;
90 printf("\n");
93 int body()
94 { char c;
95 while(1){
96 ps();
97 printf("I am Proc %d in body()\n", running->pid);
98 printf("Input a char : [s|q] ");
99 c=getc();
100 switch(c){
101 case 's': tswitch(); break;
102 case 'q': grave(); break;
103 default : break;
109 main()
111 printf("\nWelcome to the 460 Multitasking System\n");
112 initialize();
113 printf("P0 switch to P1\n");
114 tswitch();
115 printf("P0 resumes: all dead, happy ending\n");
119 int scheduler()
121 PROC *p;
122 p = running->next;
124 while (p->status != READY && p != running)
125 p = p->next;
127 if (p == running)
128 running = &proc[0];
129 else
130 running = p;
132 printf("\n-----------------------------\n");
133 printf("next running proc = %d\n", running->pid);
134 printf("-----------------------------\n");