2013-05-17 18 views
3

我知道調度是由內核完成的。讓我們假設Linux中的 進程(P1)當前正在處理器上執行。 由於當前進程並不知道關於時間片 的任何內容,並且內核當前未在處理器上執行,內核如何調度下一個進程以執行?從處理器的角度看進程調度

是否有某種中斷告訴處理器切換到執行內核或任何其他機制的目的?

回答

6

簡而言之,它是一箇中斷,它將控制權交還給內核。中斷可能由於任何原因而出現。內核通過定時器中斷得到控制的大部分時間,或者按鍵中斷都可能喚醒內核。 中斷通知使用外設系統完成IO或幾乎任何改變系統狀態的事件可能會喚醒內核。

更多關於中斷:

中斷這樣被分成頂部一半和底部一半。下半部分是爲了延遲中斷環境下的工作。

上半部:跑帶,因此禁用中斷應該是超快,儘快放棄CPU,通常

1) stores interrupt state flag and disables the interrupts(reset 
some pin on the processor),  
2) communicates with the hardware, stores state information, 
delegates remaining responsibility to bottom-half,  
3) restores the interrupt state flag and enables the interrupt((set 
some pin on the processor). 

下半部:來處理延遲工作(委託由工作上半部分)在啓用中斷的情況下運行,因此可能需要一段時間才能完成。

兩種機制用於實現下半部分處理。

1) Tasklets  
2) Work queues 

If timer is the interrupt to switch back to kernel, is the interrupt a hardware interrupt??? 

我們討論的上下文中感興趣的定時器中斷是硬件定時器中斷,

裏面的內核,這個詞定時器中斷可以或者平均值(體系結構相關的)硬件定時器中斷或軟件定時器中斷。

閱讀this的簡要概述。

更多timers

Remeber 「定時器」 是一個高級的主題,很難理解。

是硬件中斷的中斷???如果是硬件 中斷,定時器的頻率是多少?

Chapter 10. Timers and Time Management

if the interval of the timer is shorter than time slice, will kernel give the CPU back the same process, which was running early? 

這取決於許多因素例如:所使用的sheduler,系統的負載,進程優先級,這樣的事情。 最受歡迎的CFS並不真正依賴時間片搶佔的概念! CFS挑選的下一個合適的進程將獲得CPU時間。

定時器滴答,時間片和上下文切換之間的關係並不那麼直截了當。

每個進程都有自己的(動態計算的)時間片。內核跟蹤進程使用的時間片。

在SMP上,CPU特定的活動(如監視當前正在運行的進程的執行時間)是由本地APIC定時器產生的中斷完成的。 本地APIC定時器僅向其處理器發送中斷。

然而,默認的時間片,include/linux/sched/rt.h

this定義。

+0

和中斷真的經常發生。相隔幾秒運行'cat/proc/interrupts'兩次... –

+0

非常感謝你回答這個問題。對於給出的答案我有些疑問。如果定時器是切換回內核的中斷,則中斷是硬件中斷嗎?如果是硬件中斷,定時器的頻率是多少?如果定時器的時間間隔比時間片短,內核會給CPU返回相同的進程嗎? –

+0

謝謝soo soo,我已經得到了很多年來在腦海中回答的問題。我很高興在這裏發佈這個問題。再一次,謝謝你太多了!!!!! –

1

沒有什麼事情都可能發生 -

a. The current process (p1) can finish up its timeslice and then the 
    scheduler will check is there is any other process that could be run. 
    If there's no other process, the scheduler will put itself in the 
    idle state. The scheduler will assign p1 to the CPU if p1 is a CPU hoggy 
    task or p1 didn't leave the CPU voluntarily. 

    b. Another possibility is - a high priority task has jumped in. On every 
    scheduler tick, the scheduler will check if there's any process which 
    needs the CPU badly and is likely to preempt the current task. 

換句話說,一個進程可以以兩種方式離開CPU - 自願或不自願。在第一種情況下,進程將自己進入睡眠狀態,因此釋放CPU(案例a)。在另一種情況下,一個進程已被優先級更高的任務搶佔。

(注:這個答案是基於CFS任務調度當前的Linux內核的 )