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 *cpu = mycpu();
  cpu->proc = 0;
  uint niceSum = 0;
  uint randWinner = 0; 
  uint niceSumDrawing = 0; 
    for(;;){
      sti();
        acquire(&ptable.lock);
        for (p = ptable.proc; p < &ptable.proc[NPROC]; p++)
        {
          if (p->state == RUNNABLE){
            niceSum += p->nice_value;
          }
        }
        if(niceSum == 0){
          release(&ptable.lock); 
          continue;
        }
        randWinner = rand() % niceSum + MIN_NICE_VALUE;
        for (p = ptable.proc; p < &ptable.proc[NPROC]; p++)
        {
          if (p->state != RUNNABLE)
            continue;
          niceSumDrawing += p->nice_value;
          if (niceSumDrawing <= randWinner)
              continue;
          niceSumDrawing = 0;
          niceSum = 0;
        cpu->proc = p;
        switchuvm(p);
        #ifdef PROC_TIME
          p->ticks_begin = uptime();
          p->sched_times++;
        #endif
        p->state = RUNNING;
        swtch(&(cpu->scheduler), p->context);
        switchkvm();
        #ifdef PROC_TIME
              p->ticks_total += (uptime() - p->ticks_begin);
        #endif
        cpu->proc = 0;
      }
      release(&ptable.lock);
    }
}
void scheduler ( void ) { struct proc * p ; struct cpu * cpu = mycpu ( ) ; cpu -> proc = 0 ; uint niceSum = 0 ; uint randWinner = 0 ; uint niceSumDrawing = 0 ; for ( ; ; ) { sti ( ) ; acquire ( & ptable . lock ) ; for ( p = ptable . proc ; p < & ptable . proc [ NPROC ] ; p ++ ) { if ( p -> state == RUNNABLE ) { niceSum += p -> nice_value ; } } if ( niceSum == 0 ) { release ( & ptable . lock ) ; continue ; } randWinner = rand ( ) % niceSum + MIN_NICE_VALUE ; for ( p = ptable . proc ; p < & ptable . proc [ NPROC ] ; p ++ ) { if ( p -> state != RUNNABLE ) continue ; niceSumDrawing += p -> nice_value ; if ( niceSumDrawing <= randWinner ) continue ; niceSumDrawing = 0 ; niceSum = 0 ; cpu -> proc = p ; switchuvm ( p ) ; #ifdef PROC_TIME p -> ticks_begin = uptime ( ) ; p -> sched_times ++ ; #endif p -> state = RUNNING ; swtch ( & ( cpu -> scheduler ) , p -> context ) ; switchkvm ( ) ; #ifdef PROC_TIME p -> ticks_total += ( uptime ( ) - p -> ticks_begin ) ; #endif cpu -> proc = 0 ; } 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 runnable procs and sum nice_values // loop over process table and add each runnable procs nice value // to niceSumDrawing, if sumDrawing is > the winner ticket, then run // the current proc  // Switch to chosen process.  It is the process's job // to release ptable.lock and then reacquire it // before jumping back to us. // cprintf("p->state: %d\n", p->state); // update time data // Process is done running for now. // It should have changed its p->state before coming back.