xv6进程调度代码

来源:互联网 发布:linux安装网站安全狗 编辑:程序博客网 时间:2024/06/14 23:47


最近在研究libco,看到协程切换运行,于是想到很早之前看的xv6,于是翻了一下代码,看看进程调度,以及上下文切换的部分,怀念那段撸OS的日子



// 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.voidscheduler(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;      // 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;      switchuvm(p);      p->state = RUNNING;      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);  }}