proc.c
//PAGEBREAK: 42
// Per-CPU process scheduler.
// Each CPU calls scheduler() after setting itself up.
// Scheduler never returns.  It loops, doing:
//  - choose a process to run
//  - swtch to start running that process
//  - eventually that process transfers control
//      via swtch back to the scheduler.
42
Per-CPU process scheduler.
Each CPU calls scheduler() after setting itself up.
Scheduler never returns.  It loops, doing:
choose a process to run
swtch to start running that process
eventually that process transfers control
via swtch back to the scheduler.
42 Per - CPU process scheduler . Each CPU calls scheduler () after setting itself up . Scheduler never returns . It loops doing : choose a process to run swtch to start running that process eventually that process transfers control via swtch back to the scheduler .
void
scheduler(void)
{
  struct proc *p;
  struct cpu *c = mycpu();
  long total_tickets = 0;
  long counter = 0;
  long winner = 0;
  c->proc = 0;
  for(;;){
    sti();
    acquire(&ptable.lock);
    total_tickets = lottery_total();
    winner = random_at_most(total_tickets);
    counter = 0;
    for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
      if(p->state != RUNNABLE)
        continue;
        counter += p->tickets;
        if (counter < winner) {
            continue;
      }
      c->proc = p;
      switchuvm(p);
      p->state = RUNNING;
      swtch(&(c->scheduler), p->context); 
      switchkvm();
       p->ticks += 1;
      c->proc = 0;
      break;
    }
    release(&ptable.lock);
  }
}
void scheduler ( void ) { struct proc * p ; struct cpu * c = mycpu ( ) ; long total_tickets = 0 ; long counter = 0 ; long winner = 0 ; c -> proc = 0 ; for ( ; ; ) { sti ( ) ; acquire ( & ptable . lock ) ; total_tickets = lottery_total ( ) ; winner = random_at_most ( total_tickets ) ; counter = 0 ; for ( p = ptable . proc ; p < & ptable . proc [ NPROC ] ; p ++ ) { if ( p -> state != RUNNABLE ) continue ; counter += p -> tickets ; if ( counter < winner ) { continue ; } c -> proc = p ; switchuvm ( p ) ; p -> state = RUNNING ; swtch ( & ( c -> scheduler ) , p -> context ) ; switchkvm ( ) ; p -> ticks += 1 ; c -> proc = 0 ; break ; } release ( & ptable . lock ) ; } }
PAGEBREAK: 42
Per-CPU process scheduler.
PAGEBREAK : 42 Per - CPU process scheduler .
// Enable interrupts on this processor. // Loop over process table looking for process to run. // start added ---------------- // finish added --------------- //  start added ------------- // finish added ------------ // Switch to chosen process.  It is the process's job // to release ptable.lock and then reacquire it // before jumping back to us. // switch to the chossen process  // start added ----------- // finish added ------------ // Process is done running for now. // It should have changed its p->state before coming back. // start added ----------- // finish added ------------