proc.c
void
scheduler(void)
{
  struct proc *p;

  for(;;){
    // Enable interrupts on this processor.
    sti();

    // Loop over process table looking for process to run.
    acquire(&ptable.lock);

    for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){

      if(p->state != RUNNABLE)
        continue;

      if(strategy == 2 || (strategy == 3 && grtexists())){
        int bestpid = getbestproc();
	    if(p->pid != bestpid)
          continue;
      }

      if(strategy == 1 || (strategy == 3 && grtexists() == 0 && frrexists() == 1)){
        if(!isempty(queuedata_frr) && p != peek(queue_frr, queuedata_frr)){
          continue;
        }
      }


      // Switch to chosen process.  It is the process's job
      // to release ptable.lock and then reacquire it
      // before jumping back to us.
      proc = p;
      p->state = RUNNING;

      if(strategy == 1 || (strategy == 3 && grtexists() == 0 && frrexists() == 1)){
          if(test3flag == 1){
            cprintf("Printing queue...\n");
            for(int i = queuedata_frr.rear; i < queuedata_frr.front; i++){
                cprintf("Process %d is in queue.\n", queue_frr[i]->pid);
            }
          }
          removedata(queue_frr, &queuedata_frr);
      }
      switchuvm(p);

      swtch(&cpu->scheduler, p->context);
      switchkvm();

      // Process is done running for now.
      // It should have changed its p->state before coming back.
      proc = 0;
    }

    release(&ptable.lock);

  }
}
//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;
  for(;;){
    sti();
    acquire(&ptable.lock);
    for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
      if(p->state != RUNNABLE)
        continue;
      if(strategy == 2 || (strategy == 3 && grtexists())){
        int bestpid = getbestproc();
	    if(p->pid != bestpid)
          continue;
      }
      if(strategy == 1 || (strategy == 3 && grtexists() == 0 && frrexists() == 1)){
        if(!isempty(queuedata_frr) && p != peek(queue_frr, queuedata_frr)){
          continue;
        }
      }
      proc = p;
      p->state = RUNNING;
      if(strategy == 1 || (strategy == 3 && grtexists() == 0 && frrexists() == 1)){
          if(test3flag == 1){
            cprintf("Printing queue...\n");
            for(int i = queuedata_frr.rear; i < queuedata_frr.front; i++){
                cprintf("Process %d is in queue.\n", queue_frr[i]->pid);
            }
          }
          removedata(queue_frr, &queuedata_frr);
      }
      switchuvm(p);
      swtch(&cpu->scheduler, p->context);
      switchkvm();
      proc = 0;
    }
    release(&ptable.lock);
  }
}
void scheduler ( void ) { struct proc * p ; for ( ; ; ) { sti ( ) ; acquire ( & ptable . lock ) ; for ( p = ptable . proc ; p < & ptable . proc [ NPROC ] ; p ++ ) { if ( p -> state != RUNNABLE ) continue ; if ( strategy == 2 || ( strategy == 3 && grtexists ( ) ) ) { int bestpid = getbestproc ( ) ; if ( p -> pid != bestpid ) continue ; } if ( strategy == 1 || ( strategy == 3 && grtexists ( ) == 0 && frrexists ( ) == 1 ) ) { if ( ! isempty ( queuedata_frr ) && p != peek ( queue_frr , queuedata_frr ) ) { continue ; } } proc = p ; p -> state = RUNNING ; if ( strategy == 1 || ( strategy == 3 && grtexists ( ) == 0 && frrexists ( ) == 1 ) ) { if ( test3flag == 1 ) { cprintf ( " \n " ) ; for ( int i = queuedata_frr . rear ; i < queuedata_frr . front ; i ++ ) { cprintf ( " \n " , queue_frr [ i ] -> pid ) ; } } removedata ( queue_frr , & queuedata_frr ) ; } switchuvm ( p ) ; swtch ( & cpu -> scheduler , p -> context ) ; switchkvm ( ) ; 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 process to run. // Switch to chosen process.  It is the process's job // to release ptable.lock and then reacquire it // before jumping back to us. // Process is done running for now. // It should have changed its p->state before coming back.