搶占時伴隨著schedule()的執行。內核提供了一個TIF_NEED_RESCHED標志來表明是否要用schedule()調度一次。
根據搶占發生的時機分為用戶搶占和內核搶占。用戶搶占發生在內核即將返回到用戶空間的時候。內核搶占發生在返回內核空間的時候。
1、用戶搶占:內核在即將返回用戶空間時檢查進程是否設置了TIF_NEED_RESCHED標志,如果設置了,就會發生用戶搶占。用戶搶占發生的時機:從系統調用或中斷處理程序返回用戶空間的時候。
2、內核搶占:在不支持內核搶占的內核中,內核進程如果自己不主動停止,就會一直的運行下去。無法響應實時進程。搶占內核雖然犧牲了上下文切換的開銷,但獲得了更大的吞吐量和響應時間。2.6的內核添加了內核搶占,同時為了某些地方不被搶占,又添加了自旋鎖。在進程的thread_info結構中添加了preempt_count該數值為0,當進程使用一個自旋鎖時就加1,釋放一個自旋鎖時就減1.為0時表示內核可以搶占。
內核發生搶占的時機:1、從中斷處理程序返回內核空間時,內核會檢查preempt_count和TIF_NEED_RESCHED標志,如果進程設置了TIF_NEED_RESCHED標志,並且preempt_count為0,發生內核搶占。2、當內核再次用於可搶占性的時候,當進程所有的自旋鎖都釋放了,釋放程序會檢查TIF_NEED_RESCHED標志,如果設置了就會調用schedule。3、顯示調用schedule時4、內核中的進程被堵塞的時候。
進程的切換:
從一個運行的進程轉換到另一個可運行的進程時,稱為進程切換(process switch)或上下文切換(context switch).
1、硬件上下文(hardware context)
2、任務狀態段tss
3、硬件上下文的切換:linux內核中的硬件上下文不像intel原始設計的那樣存放在tss中而是用一個特定了數據結構來存儲。
4、context_switch
/*
*context_switch - switch to the new MM and the new
*thread's register state.
*/
static inline void
context_switch(struct rq *rq, structtask_struct *prev,
struct task_struct *next)
{
structmm_struct *mm, *oldmm;
prepare_task_switch(rq,prev, next);
trace_sched_switch(prev,next);
/*獲取即將調度的進程內存指針*/
mm =next->mm;
/*保存當前要替換的進程使用的內存數據結構*/
oldmm= prev->active_mm;
/*
* For paravirt, this is coupled with an exitin switch_to to
*查看本欄目更多精彩內容:http://www.bianceng.cn/OS/unix/
* combine the page table reload and the switchbackend into
* one hypercall.
*/
arch_start_context_switch(prev);
/*如果指向的即將調用的進程內存指針為null,則為內核線程*/
if(!mm) {
next->active_mm= oldmm;
/*內存描述結構使用計數加1*/
atomic_inc(&oldmm->mm_count);
enter_lazy_tlb(oldmm,next);
}else
/*不是內核線程,切換頁表*/
switch_mm(oldmm,mm, next);
if(!prev->mm) {
prev->active_mm= NULL;
rq->prev_mm= oldmm;
}
/*
* Since the runqueue lock will be released bythe next
* task (which is an invalid locking op but inthe case
* of the scheduler it's an obviousspecial-case), so we
* do an early lockdep release here:
*/
#ifndef __ARCH_WANT_UNLOCKED_CTXSW
spin_release(&rq->lock.dep_map,1, _THIS_IP_);
#endif
/*Here we just switch the register state and the stack. */
/*處理器狀態切換*/
switch_to(prev,next, prev);
barrier();
/*
* this_rq must be evaluated again because prevmay have moved
* CPUs since it called schedule(), thus the'rq' on its stack
* frame will be invalid.
*/
finish_task_switch(this_rq(),prev);
}