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

  for(;;){
    // Enable interrupts on this processor.
    sti();
    acquire(&ptable.lock);
    if(SCHEDFLAG==4){
            if(!isEmpty1()){
                //acquire(&ptable.lock);
                for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
                    if(ticks == p->ctime)
                        p->cptime = 99999;
                    else
                        p->cptime = p->rtime/(ticks - p->cptime);
                }
                //release(&ptable.lock);
                struct proc * minprocess = ptable.proc;
                float mincpt = 99999;
                //acquire(&ptable.lock);
                for(p = ptable.proc; p < &ptable.proc[NPROC] && isIncluded1(p); p++){
                    if (p->state != RUNNABLE)
                        continue;
                    if(p->cptime <= mincpt){
                        minprocess = p;
                        mincpt = p->cptime;
                    }
                }
                //release(&ptable.lock);
                if(minprocess->state == RUNNABLE){
                    proc = minprocess;
                    switchuvm(minprocess);
                    minprocess->state = RUNNING;
                    swtch(&cpu->scheduler, minprocess->context);
                    switchkvm();
                    proc = 0;
                }
                /*float min=FLT_MAX;
                float ppr;
                struct proc* q;
                acquire(&ptable.lock);
                for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
                    if(p->state != RUNNABLE)
                        continue;
                if(ticks==p->ctime){
                ppr=9999;
                }else{
                    ppr=p->rtime/(ticks-p->ctime);
                }
                if(ppr<=min){
                    q=p;
                    min=ppr;
                }
                }
                proc = q;
                switchuvm(q);
                q->state = RUNNING;
                swtch(&cpu->scheduler, q->context);
                switchkvm();
                proc=0;
                */
            }

                else if(!isEmpty2()){
               // acquire(&ptable.lock);
                for(p = ptable.proc; p < &ptable.proc[NPROC] && isIncluded2(p); p++){
                if(p->state != RUNNABLE)
                    continue;
                if(SCHEDFLAG==2){
                    if(isEmpty() || p!=peek())
                        continue;
                proc = p;
                switchuvm(p);
                p->state = RUNNING;
                removeData();
                swtch(&cpu->scheduler, p->context);
                switchkvm();
                proc=0;
        if(p->state==RUNNABLE){
            insert(p);
        }

                }
            }


            }else{
                 for(p = ptable.proc; p < &ptable.proc[NPROC] && isIncluded3(p); p++){
            if(p->state != RUNNABLE)
                continue;
            proc = p;
            switchuvm(p);
            p->state = RUNNING;
        //if(!isEmpty())
        //removeData();
            swtch(&cpu->scheduler, p->context);
            switchkvm();

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

            }
    }
    else if(SCHEDFLAG==3){

            //acquire(&ptable.lock);
                for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
                    if(ticks == p->ctime)
                        p->cptime = 99999;
                    else
                        p->cptime = p->rtime/(ticks - p->cptime);
                }
                //release(&ptable.lock);
                struct proc * minprocess = ptable.proc;
                float mincpt = 99999;
                //acquire(&ptable.lock);
                for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
                    if (p->state != RUNNABLE)
                        continue;
                    if(p->cptime <= mincpt){
                        minprocess = p;
                        mincpt = p->cptime;
                    }
                }
                //release(&ptable.lock);
                if(minprocess->state == RUNNABLE){
                    proc = minprocess;
                    switchuvm(minprocess);
                    minprocess->state = RUNNING;
                    swtch(&cpu->scheduler, minprocess->context);
                    switchkvm();
                    proc = 0;
                }

            /*float min=FLT_MAX;
            float ppr;
            struct proc* q;
            acquire(&ptable.lock);
         for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
            if(p->state != RUNNABLE)
                continue;
        if(ticks==p->ctime){
            ppr=9999;
        }else{
            ppr=p->rtime/(ticks-p->ctime);
            }
            if(ppr<=min){
                q=p;
                min=ppr;
            }

         }
            proc = q;
            switchuvm(q);
            q->state = RUNNING;
            swtch(&cpu->scheduler, q->context);
            switchkvm();
            proc=0;
            */
    }else{
    // 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;

      // Switch to chosen process.  It is the process's job
      // to release ptable.lock and then reacquire it
      // before jumping back to us.
        if(SCHEDFLAG==1 ){
        proc = p;
        switchuvm(p);
        p->state = RUNNING;
      //if(!isEmpty())
      //removeData();
        swtch(&cpu->scheduler, p->context);
        switchkvm();

      // Process is done running for now.
      // It should have changed its p->state before coming back.
        proc = 0;
        }else if(SCHEDFLAG==2){
            if(isEmpty() || p!=peek())
                continue;
            proc = p;
            switchuvm(p);
            p->state = RUNNING;
            removeData();
            swtch(&cpu->scheduler, p->context);
            switchkvm();
            proc=0;
        /*if(p->state==RUNNABLE){
            insert(p);
        }*/

            }
        }
    //release(&ptable.lock);
    }
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);
    if(SCHEDFLAG==4){
            if(!isEmpty1()){
                for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
                    if(ticks == p->ctime)
                        p->cptime = 99999;
                    else
                        p->cptime = p->rtime/(ticks - p->cptime);
                }
                struct proc * minprocess = ptable.proc;
                float mincpt = 99999;
                for(p = ptable.proc; p < &ptable.proc[NPROC] && isIncluded1(p); p++){
                    if (p->state != RUNNABLE)
                        continue;
                    if(p->cptime <= mincpt){
                        minprocess = p;
                        mincpt = p->cptime;
                    }
                }
                if(minprocess->state == RUNNABLE){
                    proc = minprocess;
                    switchuvm(minprocess);
                    minprocess->state = RUNNING;
                    swtch(&cpu->scheduler, minprocess->context);
                    switchkvm();
                    proc = 0;
                }
            }
                else if(!isEmpty2()){
                for(p = ptable.proc; p < &ptable.proc[NPROC] && isIncluded2(p); p++){
                if(p->state != RUNNABLE)
                    continue;
                if(SCHEDFLAG==2){
                    if(isEmpty() || p!=peek())
                        continue;
                proc = p;
                switchuvm(p);
                p->state = RUNNING;
                removeData();
                swtch(&cpu->scheduler, p->context);
                switchkvm();
                proc=0;
        if(p->state==RUNNABLE){
            insert(p);
        }
                }
            }
            }else{
                 for(p = ptable.proc; p < &ptable.proc[NPROC] && isIncluded3(p); p++){
            if(p->state != RUNNABLE)
                continue;
            proc = p;
            switchuvm(p);
            p->state = RUNNING;
            swtch(&cpu->scheduler, p->context);
            switchkvm();
            proc = 0;
                 }
            }
    }
    else if(SCHEDFLAG==3){
                for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
                    if(ticks == p->ctime)
                        p->cptime = 99999;
                    else
                        p->cptime = p->rtime/(ticks - p->cptime);
                }
                struct proc * minprocess = ptable.proc;
                float mincpt = 99999;
                for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
                    if (p->state != RUNNABLE)
                        continue;
                    if(p->cptime <= mincpt){
                        minprocess = p;
                        mincpt = p->cptime;
                    }
                }
                if(minprocess->state == RUNNABLE){
                    proc = minprocess;
                    switchuvm(minprocess);
                    minprocess->state = RUNNING;
                    swtch(&cpu->scheduler, minprocess->context);
                    switchkvm();
                    proc = 0;
                }
    }else{
        for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
            if(p->state != RUNNABLE)
                continue;
        if(SCHEDFLAG==1 ){
        proc = p;
        switchuvm(p);
        p->state = RUNNING;
        swtch(&cpu->scheduler, p->context);
        switchkvm();
        proc = 0;
        }else if(SCHEDFLAG==2){
            if(isEmpty() || p!=peek())
                continue;
            proc = p;
            switchuvm(p);
            p->state = RUNNING;
            removeData();
            swtch(&cpu->scheduler, p->context);
            switchkvm();
            proc=0;
            }
        }
    }
release(&ptable.lock);
  }
}
void scheduler ( void ) { struct proc * p ; for ( ; ; ) { sti ( ) ; acquire ( & ptable . lock ) ; if ( SCHEDFLAG == 4 ) { if ( ! isEmpty1 ( ) ) { for ( p = ptable . proc ; p < & ptable . proc [ NPROC ] ; p ++ ) { if ( ticks == p -> ctime ) p -> cptime = 99999 ; else p -> cptime = p -> rtime / ( ticks - p -> cptime ) ; } struct proc * minprocess = ptable . proc ; float mincpt = 99999 ; for ( p = ptable . proc ; p < & ptable . proc [ NPROC ] && isIncluded1 ( p ) ; p ++ ) { if ( p -> state != RUNNABLE ) continue ; if ( p -> cptime <= mincpt ) { minprocess = p ; mincpt = p -> cptime ; } } if ( minprocess -> state == RUNNABLE ) { proc = minprocess ; switchuvm ( minprocess ) ; minprocess -> state = RUNNING ; swtch ( & cpu -> scheduler , minprocess -> context ) ; switchkvm ( ) ; proc = 0 ; } } else if ( ! isEmpty2 ( ) ) { for ( p = ptable . proc ; p < & ptable . proc [ NPROC ] && isIncluded2 ( p ) ; p ++ ) { if ( p -> state != RUNNABLE ) continue ; if ( SCHEDFLAG == 2 ) { if ( isEmpty ( ) || p != peek ( ) ) continue ; proc = p ; switchuvm ( p ) ; p -> state = RUNNING ; removeData ( ) ; swtch ( & cpu -> scheduler , p -> context ) ; switchkvm ( ) ; proc = 0 ; if ( p -> state == RUNNABLE ) { insert ( p ) ; } } } } else { for ( p = ptable . proc ; p < & ptable . proc [ NPROC ] && isIncluded3 ( p ) ; p ++ ) { if ( p -> state != RUNNABLE ) continue ; proc = p ; switchuvm ( p ) ; p -> state = RUNNING ; swtch ( & cpu -> scheduler , p -> context ) ; switchkvm ( ) ; proc = 0 ; } } } else if ( SCHEDFLAG == 3 ) { for ( p = ptable . proc ; p < & ptable . proc [ NPROC ] ; p ++ ) { if ( ticks == p -> ctime ) p -> cptime = 99999 ; else p -> cptime = p -> rtime / ( ticks - p -> cptime ) ; } struct proc * minprocess = ptable . proc ; float mincpt = 99999 ; for ( p = ptable . proc ; p < & ptable . proc [ NPROC ] ; p ++ ) { if ( p -> state != RUNNABLE ) continue ; if ( p -> cptime <= mincpt ) { minprocess = p ; mincpt = p -> cptime ; } } if ( minprocess -> state == RUNNABLE ) { proc = minprocess ; switchuvm ( minprocess ) ; minprocess -> state = RUNNING ; swtch ( & cpu -> scheduler , minprocess -> context ) ; switchkvm ( ) ; proc = 0 ; } } else { for ( p = ptable . proc ; p < & ptable . proc [ NPROC ] ; p ++ ) { if ( p -> state != RUNNABLE ) continue ; if ( SCHEDFLAG == 1 ) { proc = p ; switchuvm ( p ) ; p -> state = RUNNING ; swtch ( & cpu -> scheduler , p -> context ) ; switchkvm ( ) ; proc = 0 ; } else if ( SCHEDFLAG == 2 ) { if ( isEmpty ( ) || p != peek ( ) ) continue ; proc = p ; switchuvm ( p ) ; p -> state = RUNNING ; removeData ( ) ; 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. //acquire(&ptable.lock); //release(&ptable.lock); //acquire(&ptable.lock); //release(&ptable.lock); /*float min=FLT_MAX;
                float ppr;
                struct proc* q;
                acquire(&ptable.lock);
                for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
                    if(p->state != RUNNABLE)
                        continue;
                if(ticks==p->ctime){
                ppr=9999;
                }else{
                    ppr=p->rtime/(ticks-p->ctime);
                }
                if(ppr<=min){
                    q=p;
                    min=ppr;
                }
                }
                proc = q;
                switchuvm(q);
                q->state = RUNNING;
                swtch(&cpu->scheduler, q->context);
                switchkvm();
                proc=0;
                */ // acquire(&ptable.lock); //if(!isEmpty()) //removeData(); // Process is done running for now. // It should have changed its p->state before coming back. //acquire(&ptable.lock); //release(&ptable.lock); //acquire(&ptable.lock); //release(&ptable.lock); /*float min=FLT_MAX;
            float ppr;
            struct proc* q;
            acquire(&ptable.lock);
         for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
            if(p->state != RUNNABLE)
                continue;
        if(ticks==p->ctime){
            ppr=9999;
        }else{
            ppr=p->rtime/(ticks-p->ctime);
            }
            if(ppr<=min){
                q=p;
                min=ppr;
            }

         }
            proc = q;
            switchuvm(q);
            q->state = RUNNING;
            swtch(&cpu->scheduler, q->context);
            switchkvm();
            proc=0;
            */ // Loop over process table looking for process to run. //acquire(&ptable.lock); // Switch to chosen process.  It is the process's job // to release ptable.lock and then reacquire it // before jumping back to us. //if(!isEmpty()) //removeData(); // Process is done running for now. // It should have changed its p->state before coming back. /*if(p->state==RUNNABLE){
            insert(p);
        }*/ //release(&ptable.lock);