睡眠 等待 阻塞

2021-06-07 03:29:02 字數 2279 閱讀 8692

睡眠, 等待, 阻塞,應該是乙個概念

睡眠相關的**位於

/include/linux/wait.h  和 /kernel/wait.c 中

1. 進入睡眠:

/**

* wait_event - sleep until a condition gets true

* @wq: the waitqueue to wait on

* @condition: a c expression for the event to wait for

** the process is put to sleep (task_uninterruptible) until the

* @condition evaluates to true. the @condition is checked each time

* the waitqueue @wq is woken up.

** wake_up() has to be called after changing any variable that could

* change the result of the wait condition.

*/#define wait_event(wq, condition) \

do while (0)

#define __wait_event(wq, condition) \

do \

finish_wait(&wq, &__wait); \

} while (0)

條件判斷 condition 用來實現這個睡眠、喚醒機制,

等待就是進入睡眠,需要 滿足條件condition 來喚醒。

2. 喚醒

#define wake_up(x)            __wake_up(x, task_uninterruptible | task_interruptible, 1, null)

#define wake_up_nr(x, nr) __wake_up(x, task_uninterruptible | task_interruptible, nr, null)

#define wake_up_all(x) __wake_up(x, task_uninterruptible | task_interruptible, 0, null)

#define wake_up_interruptible(x) __wake_up(x, task_interruptible, 1, null)

#define wake_up_interruptible_nr(x, nr) __wake_up(x, task_interruptible, nr, null)

#define wake_up_interruptible_all(x) __wake_up(x, task_interruptible, 0, null)

#define wake_up_locked(x) __wake_up_locked((x), task_uninterruptible | task_interruptible)

#define wake_up_interruptible_sync(x) __wake_up_sync((x),task_interruptible, 1)

各種喚醒函式.

/**

* __wake_up - wake up threads blocked on a waitqueue.

* @q: the waitqueue

* @mode: which threads

* @nr_exclusive: how many wake-one or wake-many threads to wake up

* @key: is directly passed to the wakeup function

*/void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,

int nr_exclusive, void *key)

export_symbol(__wake_up);

睡眠 掛起 阻塞

執行緒從建立 執行到結束總是處於下面五個狀態之一 新建狀態 就緒狀態 執行狀態 阻塞狀態及死亡狀態。下圖是使用時間片輪轉法的作業系統程序的狀態和它們之間的轉換。掛起和睡眠是主動的,掛起恢復需要主動完成,睡眠恢復則是自動完成的,因為睡眠有乙個睡眠時間,睡眠時間到則恢復到就緒態。而阻塞是被動的,是在等待...

等待佇列 阻塞非阻塞

阻塞 裝置驅動不阻塞,使用者想獲取裝置資源只能不停的查詢,這無謂的消耗cpu資源。而阻塞訪問,不能獲取資源的程序將進入休眠,它將cpu資源 禮讓 給其他程序 喚醒程序的地方最大可能發生在中斷裡面,因為硬體資源獲得的同時往往伴隨著乙個中斷 定義頭 wait queue head t queue 初始化...

中斷阻塞睡眠interrupt的使用

關於interrupt的使用挺簡單的,一則 就知道了。package day03 中斷睡眠阻塞 author administrator public class interruptsleepblockdemo catch interruptedexception e huang,用於中斷lin的睡...