36void37trap(structtrapframe*tf)~~~103// Force process to give up CPU on clock tick.
104// If interrupts were on while locks held, would need to check nlock.
105if(myproc()&&myproc()->state==RUNNING&&106tf->trapno==T_IRQ0+IRQ_TIMER)107yield();
yield()
proc.c
384// Give up the CPU for one scheduling round.
385void386yield(void)387{388acquire(&ptable.lock);//DOC: yieldlock
389myproc()->state=RUNNABLE;390sched();391release(&ptable.lock);392}
322void323scheduler(void)324{325structproc*p;326structcpu*c=mycpu();327c->proc=0;328329for(;;){330// Enable interrupts on this processor.
331sti();332333// Loop over process table looking for process to run.
334acquire(&ptable.lock);335for(p=ptable.proc;p<&ptable.proc[NPROC];p++){336if(p->state!=RUNNABLE)337continue;338339// Switch to chosen process. It is the process's job
340// to release ptable.lock and then reacquire it
341// before jumping back to us.
342c->proc=p;343switchuvm(p);344p->state=RUNNING;345346swtch(&(c->scheduler),p->context);347switchkvm();348349// Process is done running for now.
350// It should have changed its p->state before coming back.
351c->proc=0;352}353release(&ptable.lock);354355}356}
L329: 無限ループになっている L335-337: 次に動かすプロセス(RUNNEABLEなプロセス)pを探す L343: pのTSS(Task State Segment)をTRにセットして、割り込みなどがあった時にpのカーネルスタックになるようにする L342: myproc()で実行中のプロセスを取得できるようにセット