SHARE
TWEET

xv6 scheduler proc.c

a guest Oct 22nd, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void
  2. scheduler(void)
  3. {
  4.   struct proc *p;
  5.   int foundproc = 1;
  6.  
  7.   for(;;){
  8.     // Enable interrupts on this processor.
  9.     sti();
  10.  
  11.    
  12.     int tickets_passed=0;
  13.     int totalTickets = 0;
  14.  
  15.     for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
  16.       if(p->state != RUNNABLE)
  17.         continue;
  18.       totalTickets = totalTickets + p->tickets;  
  19.     }
  20.  
  21.     long winner = random_at_most(totalTickets);
  22.  
  23.  
  24.     if (!foundproc) hlt();
  25.     foundproc = 0;
  26.  
  27.     // Loop over process table looking for process to run.
  28.     acquire(&ptable.lock);
  29.     for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
  30.       if(p->state != RUNNABLE)
  31.         continue;
  32.       tickets_passed += p->tickets;
  33.       if(tickets_passed<winner){
  34.         continue;
  35.       }
  36.      // cprintf("tickets are : %d ,  rand no is %ld\n",p->tickets , random_at_most(10000));
  37.       // Switch to chosen process.  It is the process's job
  38.       // to release ptable.lock and then reacquire it
  39.       // before jumping back to us.
  40.       foundproc = 1;
  41.       proc = p;
  42.       switchuvm(p);
  43.       p->state = RUNNING;
  44.       swtch(&cpu->scheduler, proc->context);
  45.       switchkvm();
  46.  
  47.       // Process is done running for now.
  48.       // It should have changed its p->state before coming back.
  49.       proc = 0;
  50.       break;
  51.     }
  52.     release(&ptable.lock);
  53.  
  54.   }
  55. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top