Linux 进程控制——等待队列详解(二)

一、什么是睡眠

    对于一个进程"睡眠"意味着什么? 当一个进程被置为睡眠, 它被标识为处于一个特殊的状态并且从调度器的运行队列中去除. 直到发生某些事情改变了那个状态, 这个进程将不被在任何 CPU 上调度, 并且, 因此, 将不会运行. 一个睡着的进程已被搁置到系统的一边, 等待以后发生事件.

    LDD3说得很玄乎,睡眠是“自愿调度”,其实就是将当前进程的状态设置为 TASK_INTERRUPTIBLE 等状态,然后schedule() 让出CPU1,让调度器重新选择一个进程来执行。

    对于一个 Linux 驱动使一个进程睡眠是一个容易做的事情. 但是, 有几个规则必须记住以安全的方式编码睡眠.

这些规则的第一个是: 当你运行在原子上下文时不能睡眠.  

    一个另外的相关的点, 当然, 是你的进程不能睡眠除非确信其他人, 在某处的, 将唤醒它.做唤醒工作的代码必须也能够找到你的进程来做它的工作. 确保一个唤醒发生, 是深入考虑你的代码和对于每次睡眠, 确切知道什么系列的事件将结束那次睡眠.使你的进程可能被找到, 真正地, 通过一个称为等待队列的数据结构实现的. 一个等待队列就是它听起来的样子:一个进程列表, 都等待一个特定的事件.


二、如何睡眠

    在 Linux 中, 一个等待队列由一个"等待队列头"来管理, 一个 wait_queue_head_t 类型的结构, 定义在<linux/wait.h>中. 一个等待队列头可被定义和初始化, 使用:

    DECLARE_WAIT_QUEUE_HEAD(name); 

    或者动态地, 如下:

    wait_queue_head_t my_queue;
    init_waitqueue_head(&my_queue);

  1、简单睡眠    

    Linux 内核中睡眠的最简单方式是一个宏定义, 称为 wait_event(有几个变体); 它结合了处理睡眠的细节和进程在等待的条件的检查. wait_event 的形式是:

    wait_event(queue, condition)
    wait_event_interruptible(queue, condition)
    wait_event_timeout(queue, condition, timeout)
    wait_event_interruptible_timeout(queue, condition, timeout)
    这些东西如何使用?queue 是等待队列头,condition 是条件,如果调用 wait_event 前 condition == 0 ,则调用 wait_event 之后,当前进程就会休眠。
    那么它们四个又有什么不同?
    wait_event:将当前进程的状态设置为 TASK_UNINTERRUPTIBLE  ,然后 schedule()
    wait_event_interruptible:         TASK_INTERRUPTIBLE    ,然后 schedule()
    wait_event_timeout:               TASK_UNINTERRUPTIBLE  ,然后 schedule_timeout()
    wait_event_interruptible_timeout:  TASK_INTERRUPTIBLE    , 然后 schedule_timeout()
    TASK_INTERRUPTIBLE 与 TASK_UNINTERRUPTIBLE 区别在于,它的休眠是否会被信号打断,别的进程发来一个信号比如 kill ,TASK_INTERRUPTIBLE 就会醒来去处理。然而 TASK_UNINTERRUPTIBLE 不会。schedule(),进程调度,而schedule_timeout()进行调度之后,一定时间后自动唤醒
    对应于不同的进程状态,使用不同的唤醒函数:
    void wake_up(wait_queue_head_t *queue);
    void wake_up_interruptible(wait_queue_head_t *queue);
    唤醒时很有意思,比如你调用 wake_up 去唤醒一个使用 wait_event 等,进入休眠的进程,唤醒之后,它会判断 condition 是否为真,如果还是假的继续睡眠。至于为什么这个样子,后面分析代码就会明白。
  
  2、手动睡眠
    DECLARE_WAITQUEUE(name, tsk)  创建一个等待队列:
        tsk一般为当前进行current. 这个宏定义并初始化一个名为name的等待队列.
 将等待队列头 加入/移除 等待队列:

        void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
        void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
        void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);

    设置进程状态:

        set_current_state(TASK_INTERRUPTIBLE) 等

    进程调度:  
        schedule() 或者 schedule_timeout()


三、内核如何实现

    以 wait_event 为例,我们看看内核都干了些什么。

[cpp]  view plain  copy
 print ?
  1. #define wait_event(wq, condition)                   \  
  2. do {                                    \  
  3.     if (condition)                          \  
  4.         break;                          \  
  5.     __wait_event(wq, condition);                    \  
  6. while (0)  
[cpp]  view plain  copy
 print ?
  1. #define __wait_event(wq, condition)                     \  
  2. do {                                    \  
  3.     DEFINE_WAIT(__wait);                        \  
  4.                                     \  
  5.     for (;;) {                          \  
  6.         prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \  
  7.         if (condition)                      \  
  8.             break;                      \  
  9.         schedule();                     \  
  10.     }                               \  
  11.     finish_wait(&wq, &__wait);                  \  
  12. while (0)  
[cpp]  view plain  copy
 print ?
  1. #define DEFINE_WAIT(name)                       \  
  2.     wait_queue_t name = {                       \  
  3.         .private    = current,              \  
  4.         .func       = autoremove_wake_function,     \  
  5.         .task_list  = LIST_HEAD_INIT((name).task_list), \  
  6.     }  
[cpp]  view plain  copy
 print ?
  1. typedef struct __wait_queue wait_queue_t;     
  2. struct __wait_queue {  
  3.     unsigned int flags;  
  4. #define WQ_FLAG_EXCLUSIVE   0x01  
  5.     void *private;  
  6.     wait_queue_func_t func;  
  7.     struct list_head task_list;  
  8. };  
举个例子:宏展开之后
[cpp]  view plain  copy
 print ?
  1. __wait_event(wq, condition);  
  2. wait_queue_t __wait = {                     \  
  3.         .private    = current,              \  
  4.         .func       = autoremove_wake_function,     \  
  5.         .task_list  = LIST_HEAD_INIT((__wait).task_list),   \  
  6.     }  
  7.     for (;;) {                          \  
  8.         prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \  
  9.         if (condition)                      \  
  10.             break;                      \  
  11.         schedule();                     \  
  12.     }                               \  
  13.     finish_wait(&wq, &__wait);  
    其实,它定义了一个叫 __wait 的等待队列,private 指向当前进程的 task_struct 结构体(唤醒的时候好知道是哪个进程),然后调用 prepare_to_wait 将等待队列头加入到等待队列中去,并设置当前进程的状态为TASK_UNINTERRUPTIBLE。然后,如果 condition 为假,则schedule(),进程调度的时候,当前进程的状态不是 TASK_RUNNING 必然要被移除 “运行队列”,也就永远不会被调度除非直到醒来。如果 condition 为真,那么finish_wait 会把之前的工作都还原,你继续执行吧,你要的条件都满足了,你还休眠个屁!

    涉及的函数代码贴一贴

[cpp]  view plain  copy
 print ?
  1. void fastcall  
  2. prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)  
  3. {  
  4.     unsigned long flags;  
  5.   
  6.     wait->flags &= ~WQ_FLAG_EXCLUSIVE;  
  7.     spin_lock_irqsave(&q->lock, flags);  
  8.     if (list_empty(&wait->task_list))  
  9.         __add_wait_queue(q, wait);  
  10.     /* 
  11.      * don't alter the task state if this is just going to 
  12.      * queue an async wait queue callback 
  13.      */  
  14.     if (is_sync_wait(wait))  
  15.         set_current_state(state);  
  16.     spin_unlock_irqrestore(&q->lock, flags);  
  17. }  
[cpp]  view plain  copy
 print ?
  1. void fastcall finish_wait(wait_queue_head_t *q, wait_queue_t *wait)  
  2. {  
  3.     unsigned long flags;  
  4.   
  5.     __set_current_state(TASK_RUNNING);  
  6.   
  7.     if (!list_empty_careful(&wait->task_list)) {  
  8.         spin_lock_irqsave(&q->lock, flags);  
  9.         list_del_init(&wait->task_list);  
  10.         spin_unlock_irqrestore(&q->lock, flags);  
  11.     }  
  12. }  
    下面来看看如何唤醒的

[cpp]  view plain  copy
 print ?
  1. #define wake_up(x) __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 1, NULL)  

[cpp]  view plain  copy
 print ?
  1. void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,  
  2.             int nr_exclusive, void *key)  
  3. {  
  4.     unsigned long flags;  
  5.   
  6.     spin_lock_irqsave(&q->lock, flags);  
  7.     __wake_up_common(q, mode, nr_exclusive, 0, key);  
  8.     spin_unlock_irqrestore(&q->lock, flags);  
  9. }  
[cpp]  view plain  copy
 print ?
  1. static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,  
  2.                  int nr_exclusive, int sync, void *key)  
  3. {  
  4.     struct list_head *tmp, *next;  
  5.   
  6.     list_for_each_safe(tmp, next, &q->task_list) {  
  7.         wait_queue_t *curr = list_entry(tmp, wait_queue_t, task_list);  
  8.         unsigned flags = curr->flags;  
  9.   
  10.         if (curr->func(curr, mode, sync, key) &&  
  11.                 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)  
  12.             break;  
  13.     }  
  14. }  
   此时会调用到,我们在等待队列里指定的那个 func 函数,也就是 autoremove_wake_function
[cpp]  view plain  copy
 print ?
  1. int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)  
  2. {  
  3.     int ret = default_wake_function(wait, mode, sync, key);  
  4.   
  5.     if (ret)  
  6.         list_del_init(&wait->task_list);  
  7.     return ret;  
  8. }  
[cpp]  view plain  copy
 print ?
  1. int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,  
  2.               void *key)  
  3. {  
  4.     return try_to_wake_up(curr->private, mode, sync);  
  5. }  
   最终调用到 default_wake_function 来唤醒 等待队列里 private 里指定的那个进程。然后,移除将等待队列头移除等待队列。try_to_wake_up ,会将 要唤醒进程的 进程状态设置为 TASK_RUNNING ,然后放到 “运行队列”中。

有意思的是:

    我们休眠时,schedule() 在哪里? TMD 居然在 for 循环里

[cpp]  view plain  copy
 print ?
  1. for (;;) {                          \  
  2.     prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \  
  3.     if (condition)                      \  
  4.         break;                      \  
  5.     schedule();                     \  
  6. }  
    唤醒之后,那么又开始了 prepare_to_wait ,判断 condition ....显然 condition 为真,才会真正的 唤醒。
    理解了他们,对于手动休眠也就很明白了。手动休眠就不用判断什么 condition 了。
[cpp]  view plain  copy
 print ?
  1. DECLARE_WAITQUEUE(name, tsk)  
  2. tsk一般为当前进行current. 这个宏定义并初始化一个名为name的等待队列.  
  3.   
  4. #define DECLARE_WAITQUEUE(name, tsk)                    \  
  5.     wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)  
  6. #define __WAITQUEUE_INITIALIZER(name, tsk) {                \  
  7.     .private    = tsk,                        \  
  8.     .func        = default_wake_function,            \  
  9.     .task_list    = { NULL, NULL } }  
  10.       
  11. void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);  
  12. void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);  
  13. void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);  
  14.   
  15. void fastcall add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)  
  16. {  
  17.     unsigned long flags;  
  18.   
  19.     wait->flags &= ~WQ_FLAG_EXCLUSIVE;  
  20.     spin_lock_irqsave(&q->lock, flags);  
  21.     __add_wait_queue(q, wait);  
  22.     spin_unlock_irqrestore(&q->lock, flags);  
  23. }  
  24.   
  25. set_current_state(TASK_INTERRUPTIBLE);    
  26. schedule();  
   简单明了:

    1、创建等待队列、等待队列头

    2、将等待队列头加入到等待队列中去

    3、设置当前进程的进程状态

    4、进程调度~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值