Linux 的行程管理由許多部分合力構成:行程控制塊、行程調度、中斷處理、任務佇列、計時器、bottom half 佇列、系統呼叫以及行程間通訊等。其中調度是決定系統效率的核心。
行程調度大體分成即時與非即時兩類。即時行程可以使用基於動態優先級的輪轉法(RR),也可以使用先進先出(FIFO);非即時行程則一律採用動態優先級的輪轉法。究竟走哪一套,是由行程控制塊中的屬性決定的,核心裡並沒有專門的服務在背後代為安排。
調度的執行者是 schedule():任何行程從系統呼叫返回時都會轉入 schedule(),中斷處理函式完成回應任務後,同樣會走進 schedule()。
核心以 task_struct 表示行程控制塊,這個結構佔 1680 個位元組。行程常見的狀態有:
所有行程以 PCB 串成雙向鏈結串列,next_task 與 prev_task 是前後向指標,頭尾都是 init_task。此外行程還會依 PID 插入雜湊表,用來加快搜尋速度。
schedule() 的工作是從 run-queue 中挑出一個就緒行程。每個行程在自己的 task_struct 裡有一個調度策略(policy 屬性),可能是 SCHED_RR、SCHED_FIFO,兩者屬即時策略;或 SCHED_OTHER,也就是普通策略。
使用者行程由 do_fork() 建立,它也是 fork 系統呼叫的執行者:建立新行程、繼承父行程資源、初始化時鐘與訊號等資料,完成後由父行程掛到就緒佇列並取回子行程 PID。新建行程的初始狀態是 TASK_UNINTERRUPTIBLE,在 do_fork() 結束前被父行程喚醒後轉為 TASK_RUNNING,等到 schedule() 依 CPU 演算法選中它,才真正拿到 CPU。
若行程採輪轉法,時間片用完(10ms 的整數倍)時,時鐘中斷會觸發 timer_interrupt() 掀起新一輪調度,把目前行程掛回就緒佇列尾端。而正在執行卻申請不到資源的行程,會呼叫 sleep_on() 或 interruptible_sleep_on() 進入睡眠;狀態為 TASK_INTERRUPTIBLE 的睡眠行程在資源可用、收到訊號或計時中斷時被喚醒,狀態變回 TASK_RUNNING 並重回就緒佇列。
以下摘錄自 2.4.23 核心的 schedule():
asmlinkage void schedule(void)
{
struct schedule_data * sched_data;
struct task_struct *prev, *next, *p;
struct list_head *tmp;
int this_cpu, c;
spin_lock_prefetch(&runqueue_lock);
BUG_ON(!current->active_mm);
need_resched_back:
prev = current;
this_cpu = prev->processor;
if (unlikely(in_interrupt())) {
printk("Scheduling in interrupt\n");
BUG();
}
release_kernel_lock(prev, this_cpu);
sched_data = & aligned_data[this_cpu].schedule_data;
spin_lock_irq(&runqueue_lock);
if (unlikely(prev->policy == SCHED_RR))
if (!prev->counter) {
prev->counter = NICE_TO_TICKS(prev->nice);
move_last_runqueue(prev);
}
switch (prev->state) {
case TASK_INTERRUPTIBLE:
if (signal_pending(prev)) {
prev->state = TASK_RUNNING;
break;
}
default:
del_from_runqueue(prev);
case TASK_RUNNING:;
}
prev->need_resched = 0;
repeat_schedule:
next = idle_task(this_cpu);
c = -1000;
list_for_each(tmp, &runqueue_head) {
p = list_entry(tmp, struct task_struct, run_list);
if (can_schedule(p, this_cpu)) {
int weight = goodness(p, this_cpu, prev->active_mm);
if (weight > c)
c = weight, next = p;
}
}
if (unlikely(!c)) {
struct task_struct *p;
spin_unlock_irq(&runqueue_lock);
read_lock(&tasklist_lock);
for_each_task(p)
p->counter = (p->counter >> 1) + NICE_TO_TICKS(p->nice);
read_unlock(&tasklist_lock);
spin_lock_irq(&runqueue_lock);
goto repeat_schedule;
}
sched_data->curr = next;
task_set_cpu(next, this_cpu);
spin_unlock_irq(&runqueue_lock);
if (unlikely(prev == next)) {
prev->policy &= ~SCHED_YIELD;
goto same_process;
}
kstat.context_swtch++;
prepare_to_switch();
{
struct mm_struct *mm = next->mm;
struct mm_struct *oldmm = prev->active_mm;
if (!mm) {
BUG_ON(next->active_mm);
next->active_mm = oldmm;
atomic_inc(&oldmm->mm_count);
enter_lazy_tlb(oldmm, next, this_cpu);
} else {
BUG_ON(next->active_mm != mm);
switch_mm(oldmm, mm, next, this_cpu);
}
if (!prev->mm) {
prev->active_mm = NULL;
mmdrop(oldmm);
}
}
switch_to(prev, next, prev);
__schedule_tail(prev);
same_process:
reacquire_kernel_lock(current);
if (current->need_resched)
goto need_resched_back;
return;
}
這段程式大致依序做了幾件事:
理解這段流程,就能明白早期核心為何在大行程數、長時間片情境下容易出現延遲——每次調度都要完整掃過就緒佇列,這也正是後續版本引入 O(1) 調度器與 CFS 的動機。