常見的疑問是:下面這兩句對 timeout 有什麼影響?
set_current_state(TASK_INTERRUPTIBLE);
set_current_state(TASK_UNINTERRUPTIBLE);
schedule_timeout(((1BlockEraseTimeoutTyp) * HZ) / 1000);
差別只有一個:處於 TASK_INTERRUPTIBLE 的進程可以被訊號喚醒;TASK_UNINTERRUPTIBLE 則不會。也就是說,前者可能提早返回,後者保證至少睡滿 timeout。
TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to pass before the routine returns. The routine will return 0.
TASK_INTERRUPTIBLE - the routine may return early if a signal is delivered to the current task. In this case the remaining time in jiffies will be returned, or 0 if the timer expired in time.
實務上的判斷很簡單:需要保證睡眠時間不被打斷(例如等待硬體完成某段不可中斷的操作)就用 U 型態;希望回應使用者中斷就用 I 型態。更深的調度與進程間通訊細節,可參考《Linux 內核原始碼情景分析》上冊的相關章節。