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();
  c->proc = 0;
  for(;;){
    sti();
    int i,j,aux_prio;
    bool exec_q0;
    acquire(&ptable.lock);
    for( i = 0 ; i < NPRIO ; i++){
      exec_q0 = false;
      for(j = 0 ; j <= ptable.count_proc[i] ; j++){
        p = ptable.queue[i][j];
        if(p->state == RUNNABLE){
          p->quanto = 0;
          aux_prio = p->priority;
          c->proc = p;
          switchuvm(p);
          p->state = RUNNING;
          swtch(&(c->scheduler), p->context);
          switchkvm();
          c->proc = 0;
          if(i == NPRIO-3){                             
            exec_q0 = true;                             
            if(aux_prio != p->priority){                
              rm_proc(p);
              ptable.count_proc[i+1]++;
              ptable.queue[i+1][ptable.count_proc[i+1]] = p;
            }
          }
          if(i == NPRIO-2){                             
            if(aux_prio != p->priority){                
              rm_proc(p);
              ptable.count_proc[i+1]++;
              ptable.queue[i+1][ptable.count_proc[i+1]] = p;
            }else{                                      
              rm_proc(p); 
              ptable.count_proc[i-1]++;
              ptable.queue[i-1][ptable.count_proc[i-1]] = p;
              p->priority--;
            }
            i=NPRIO;
            break;
          }
          if(i == NPRIO-1){                              
            if(p->quanto == 0){                          
              rm_proc(p);
              ptable.count_proc[i-1]++;
              ptable.queue[i-1][ptable.count_proc[i-1]] = p;
              p->priority--;
            }else{                                       
              rm_proc(p);
              ptable.count_proc[i]++;
              ptable.queue[i][ptable.count_proc[i]] = p;
            }
            break;
          }
        }
      }
      if((exec_q0 == true) && i == (NPRIO-3)){           
        break;
      }
    }
    release(&ptable.lock);
  }
}
void scheduler ( void ) { struct proc * p ; struct cpu * c = mycpu ( ) ; c -> proc = 0 ; for ( ; ; ) { sti ( ) ; int i , j , aux_prio ; bool exec_q0 ; acquire ( & ptable . lock ) ; for ( i = 0 ; i < NPRIO ; i ++ ) { exec_q0 = false ; for ( j = 0 ; j <= ptable . count_proc [ i ] ; j ++ ) { p = ptable . queue [ i ] [ j ] ; if ( p -> state == RUNNABLE ) { p -> quanto = 0 ; aux_prio = p -> priority ; c -> proc = p ; switchuvm ( p ) ; p -> state = RUNNING ; swtch ( & ( c -> scheduler ) , p -> context ) ; switchkvm ( ) ; c -> proc = 0 ; if ( i == NPRIO - 3 ) { exec_q0 = true ; if ( aux_prio != p -> priority ) { rm_proc ( p ) ; ptable . count_proc [ i + 1 ] ++ ; ptable . queue [ i + 1 ] [ ptable . count_proc [ i + 1 ] ] = p ; } } if ( i == NPRIO - 2 ) { if ( aux_prio != p -> priority ) { rm_proc ( p ) ; ptable . count_proc [ i + 1 ] ++ ; ptable . queue [ i + 1 ] [ ptable . count_proc [ i + 1 ] ] = p ; } else { rm_proc ( p ) ; ptable . count_proc [ i - 1 ] ++ ; ptable . queue [ i - 1 ] [ ptable . count_proc [ i - 1 ] ] = p ; p -> priority -- ; } i = NPRIO ; break ; } if ( i == NPRIO - 1 ) { if ( p -> quanto == 0 ) { rm_proc ( p ) ; ptable . count_proc [ i - 1 ] ++ ; ptable . queue [ i - 1 ] [ ptable . count_proc [ i - 1 ] ] = p ; p -> priority -- ; } else { rm_proc ( p ) ; ptable . count_proc [ i ] ++ ; ptable . queue [ i ] [ ptable . count_proc [ i ] ] = p ; } break ; } } } if ( ( exec_q0 == true ) && i == ( NPRIO - 3 ) ) { 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. // Si estamos en la cola 0 // exec_q0 permite saber si se ejecuto algun proceso en la cola 0 // Si consume un quantum lo baja de prioridad // Si estamos en la cola 1 // Si consume todo el quantum , baja de prioridad // Si termina antes de consumir todo el quantum,sube prioridad // Si estamos en la cola 2 // Si no sonsume un quantum aumentamos su prioridad // Consumió todo el quantum, mantiene su prioridad por tener la menor // Si en la cola 0 se ejecuto algun proceso, que vuelva a ejecutar la cola 0