proc.c
// Wait for a child process to exit and return its pid.
// Return -1 if this process has no children.
Wait for a child process to exit and return its pid.
Return -1 if this process has no children.
Wait for a child process to exit and return its pid . Return - 1 if this process has no children .
int
wait(void)
{
  struct proc *p;
  int havekids, pid;
  struct proc *curproc = myproc();
  acquire(&ptable.lock);
  for(;;){
    havekids = 0;
    for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
      if(p->parent != curproc)
        continue;
      havekids = 1;
      if(p->state == ZOMBIE){
        pid = p->pid;
        kfree(p->kstack);
        p->kstack = 0;
        freevm(p->pgdir);
        p->pid = 0;
        p->parent = 0;
        p->name[0] = 0;
        p->killed = 0;
        p->state = UNUSED;
        rm_proc(p);
        release(&ptable.lock);
        return pid;
      }
    }
    if(!havekids || curproc->killed){
      release(&ptable.lock);
      return -1;
    }
    sleep(curproc, &ptable.lock);  
  }
}
int wait ( void ) { struct proc * p ; int havekids , pid ; struct proc * curproc = myproc ( ) ; acquire ( & ptable . lock ) ; for ( ; ; ) { havekids = 0 ; for ( p = ptable . proc ; p < & ptable . proc [ NPROC ] ; p ++ ) { if ( p -> parent != curproc ) continue ; havekids = 1 ; if ( p -> state == ZOMBIE ) { pid = p -> pid ; kfree ( p -> kstack ) ; p -> kstack = 0 ; freevm ( p -> pgdir ) ; p -> pid = 0 ; p -> parent = 0 ; p -> name [ 0 ] = 0 ; p -> killed = 0 ; p -> state = UNUSED ; rm_proc ( p ) ; release ( & ptable . lock ) ; return pid ; } } if ( ! havekids || curproc -> killed ) { release ( & ptable . lock ) ; return -1 ; } sleep ( curproc , & ptable . lock ) ; } }
Wait for a child process to exit and return its pid.
Wait for a child process to exit and return its pid .
// Scan through table looking for exited children. // Found one. // No point waiting if we don't have any children. // Wait for children to exit.  (See wakeup1 call in proc_exit.) //DOC: wait-sleep