* reordered a little bit
[mascara-docs.git] / i86 / mtx-16-bit / mtx / samples / LAB4 / wait.c
blob4986229a0c975a20e933dc9562efd2cbee1a4a43
1 /*********************** wait.c file ***********************************/
3 int grave() /* this grave() does NOT free the ZOMBIE proc */
5 printf("\n*************************************\n");
6 printf("Process %d %c %s", running->pid, 7,gasp[running->pid % 4]);
7 printf("\n*************************************\n");
9 tswitch();
12 int wait(exitValue) int *exitValue;
14 PROC *p;
15 int i, found;
17 while(1){
18 found = 0;
19 for (i=0; i<NPROC; i++){
20 p = &proc[i];
21 if (p->ppid == running->pid && p->status != FREE){
22 found = 1;
23 /* lay the dead child to rest */
24 if (p->status == ZOMBIE){
25 *exitValue = p->exitValue;
26 p->status = FREE; /* free its PROC */
27 putproc(p);
28 nproc--;
29 return(p->pid); /* return its pid */
33 if (!found) /* no child */
34 return(-1);
35 sleep(running); /* has kids still alive */
39 int enterSleep(p) PROC *p;
41 PROC *q;
42 if (sleepList == 0)
43 sleepList = p;
44 else{
45 q = sleepList;
46 while (q->next)
47 q = q->next;
48 q->next = p;
50 p->next = 0;
53 int sleep(event) int event;
55 printf("\n*************************************\n");
56 printf("Proc %d going to sleep on event=%x", running->pid, event);
57 printf("\n*************************************\n");
58 running->status = SLEEP;
59 running->event = event;
60 // enter sleepList FIFO
61 enterSleep(running);
62 tswitch();
66 /* wake up ALL tasks sleeping on event */
67 int wakeup(event) int event;
69 PROC *p, *q; int i;
71 p = q = sleepList;
73 while(p){
74 if (p->event != event){
75 q = p;
76 p = p->next;
77 continue;
80 // p->event==event
81 if (p==sleepList){
82 sleepList = p->next;
83 p->status = READY;
84 p->event = 0;
85 enqueue(&readyQueue, p);
86 printf("wakeup proc %d\n", p->pid);
87 p = q = sleepList;
88 continue;
90 // not the first element
91 q->next = p->next; // delete p from list
92 p->event = 0;
93 p->status = READY;
94 enqueue(&readyQueue, p);
95 printf("wakeup proc %d\n", p->pid);
96 p = q->next;
101 // YOU WRITE C CODE TO DO THESE
103 int do_exit(v) int v;
105 int i; PROC *p;
106 printf("proc %d in kexit(): exitValue=%d\n", running->pid, v);
108 (1). if caller is P1 && there are other procs still existing, do NOT die;
110 (2). Send children (dead or alive) to P1;
111 Wakeup P1 if any child is a ZOMBIE;
113 (3). Record exitValue and become a ZOMBIE;
115 (4). wakeup parent;
117 (5). call grave() to tswitch();
121 int do_wait(uptr) int *uptr; // 0 for Kmode, nonzero for umode
123 wait for a ZOMBIE child:
124 write ZOMBIE's exitValue to U space;
125 return ZOMBIE's pid to U space;