int kwait(int *status) // wait for ZOMBIE child { PROC *p; int i, hasChild = 0; while(1){ // search PROCs for a child for (i=1; i<NPROC; i++){ // exclude P0 p = &proc[i]; if (p->status != FREE && p->ppid == running->pid){ hasChild = 1; // has child flag if (p->status == ZOMBIE){ // lay the dead child to rest *status = p->exitCode; // collect its exitCode p->status = FREE; // free its PROC put_proc(&freeList, p); // to freeList nproc--; // once less processes return(p->pid); // return its pid } } } if (!hasChild) return -1; // no child, return ERROR ksleep(running); // still has kids alive: sleep on PROC address } }
int kwait(int *status) { PROC *p; int i, hasChild = 0; while(1) { for(i = 1; i < NPROC; i++) { p = &proc[i]; if(p->status != FREE && p->ppid == running->pid) { hasChild = 1; if(p->status == ZOMBIE) { *status = p->exitCode; p->status = FREE; put_proc(&freeList, p); nproc--; return(p->pid); } } } if(!hasChild) return -1; //no children return ERROR ksleep(running); //stil had kids alive: sleep on PROC address } }
static void port_put(port *p, char *data, int length) { char *(*put_proc)(port *, char *, int); char *error; if (p->status & OUTPUT_CLOSED) { var_set_variable("error", "Attempt to write to a port whose output has been closed"); return; } put_proc = p->put; if (!put_proc) { var_set_variable("error", "Attempt to write to a port which does not support writing"); return; } error = put_proc(p, data, length); if (error) var_set_variable("error", error); }
int kwait(int *status) { PROC *p; int i, found = 0; while(1){ //found = 0; for (i=0; i<NPROC; i++){ p = &proc[i]; if (p->ppid == running->pid && p->status != FREE){ found = 1; /* lay the dead child to rest */ if (p->status == ZOMBIE){ *status = p->exitCode; p->status = FREE; /* free its PROC */ put_proc(p); nproc--; return(p->pid); /* return its pid */ } } } if (!found) /* no child */ return(-1); ksleep(running); /* has kids still alive */ } }
//free's up a zombies resources and puts it back in the ready queue int kwait(int *status) // wait for ZOMBIE child { PROC *p; int i, found = 0; while(1) { for (i = 0; i < NPROC; i++) { p = &proc[i]; if (p->ppid == running->pid && p->status != FREE) { found = 1; //lay the dead child to rest if(p->status == ZOMBIE) { *status = p->exitCode; p->status = FREE;//free its PROC put_proc(&freeList, p); nproc--; return (p->pid);//return its pid } } } if (!found)//no child return -1; ksleep(running);//has kids still alive } }
int ksleep(int event) { //printf("ksleep():\n\r"); running->event = event; // record event in PROC.event running->status = SLEEP; // change status to SLEEP //add proc to sleepList put_proc(&sleepList, running); //printf("after put_proc: pid %d status %d \n\r", running->pid, running->status); // give up CPU tswitch(); }
// resurrects all zombie processes. resurrect() { int i, counter = 0; PROC *p; for (i=0; i < NPROC; i++) { p = &proc[i]; // make it easier to access this proc :) // if the process is a zombie, make it free of its plague and stick it back on the free processes list. if (p->status == ZOMBIE) { p->status = FREE; put_proc(p); counter++; } } printf("%d processes resurrected!\n", counter); }
int body() { char c; printf("proc %d resumes to body()\n", running->pid); while(1){ color = running->pid + 7; printf("proc %d running : enter a key : ", running->pid); c = getc(); switch(c){ case 'f' : do_kfork(); break; case 's' : tswitch(); break; case 'q': running->status = ZOMBIE; running->priority = 0; put_proc(&freeList, running); tswitch(); break; case 'p': printf("*****readyQueue: "); printQueue(readyQueue); printf("\n"); printf("*****freeList: "); printQueue(freeList); printf("\n"); break; default: printf("ERROR: invalid key\n"); break; } } }