proc.c
int
wait2()
{
  struct proc *p;
  int havekids, pid;

  acquire(&ptable.lock);
  for(;;){
    // Scan through table looking for exited children.
    havekids = 0;
    for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
      if(p->parent != proc)
        continue;
      havekids = 1;
      if(p->state == ZOMBIE){
        // Found one.
        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;

        char* wtime=0;
        char* rtime=0;
        argptr(0,&wtime,sizeof(int));
        argptr(1,&rtime,sizeof(int));

        *rtime = p->rtime;
        *wtime = (p->etime - p->ctime) - (p->rtime);

        release(&ptable.lock);
        return pid;
      }
    }

    if(!havekids || proc->killed){
      release(&ptable.lock);
      return -1;
    }

    // Wait for children to exit.  (See wakeup1 call in proc_exit.)
    sleep(proc, &ptable.lock);  //DOC: wait-sleep
  }
}
// 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
wait2()
{
  struct proc *p;
  int havekids, pid;
  acquire(&ptable.lock);
  for(;;){
    havekids = 0;
    for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
      if(p->parent != proc)
        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;
        char* wtime=0;
        char* rtime=0;
        argptr(0,&wtime,sizeof(int));
        argptr(1,&rtime,sizeof(int));
        *rtime = p->rtime;
        *wtime = (p->etime - p->ctime) - (p->rtime);
        release(&ptable.lock);
        return pid;
      }
    }
    if(!havekids || proc->killed){
      release(&ptable.lock);
      return -1;
    }
    sleep(proc, &ptable.lock);  
  }
}
int wait2 ( ) { struct proc * p ; int havekids , pid ; acquire ( & ptable . lock ) ; for ( ; ; ) { havekids = 0 ; for ( p = ptable . proc ; p < & ptable . proc [ NPROC ] ; p ++ ) { if ( p -> parent != proc ) 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 ; char * wtime = 0 ; char * rtime = 0 ; argptr ( 0 , & wtime , sizeof ( int ) ) ; argptr ( 1 , & rtime , sizeof ( int ) ) ; * rtime = p -> rtime ; * wtime = ( p -> etime - p -> ctime ) - ( p -> rtime ) ; release ( & ptable . lock ) ; return pid ; } } if ( ! havekids || proc -> killed ) { release ( & ptable . lock ) ; return -1 ; } sleep ( proc , & 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. // Wait for children to exit.  (See wakeup1 call in proc_exit.) //DOC: wait-sleep