您的位置:首页 > 运维架构 > Linux

linux join wait queue!

2012-08-06 15:21 211 查看
参考文章:http://blog.csdn.net/loyal_baby/article/details/4195946

进程通过执行下面几步将自己加入到一个等待队列中:

---------------------------------------------------------
1. 调用DECLARE_WAITQUEUE()创建一个等待队列的项
|------------------------------------------------|
|/* 'q' is the wait queue we wish to sleep on */ |
|DECLARE_WAITQUEUE(wait, current); |
|------------------------------------------------|

2. 调用add_wait_queue()把自己加入到队列中。该队列在进程等待的条件满足时唤醒它。当然我们必须在其他地方撰写相关代码,在事件发生时,对等待队列执行wake_up()操作
|-----------------------------|
|add_wait_queue(q, &wait); |
|-----------------------------|

while (!condition) { /* condition is the event that we are waiting for */ 

3. 将进程的状态变更为TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
|----------------------------------------------|
| /* or TASK_UNINTERRUPTIBLE */ |
| set_current_state(TASK_INTERRUPTIBLE); |
|----------------------------------------------|

4. 如果状态被设置为TASK_INTERRUPTIBLE,则信号可以唤醒进程(信号和事件都可以唤醒该进程)。这就是所谓的伪唤醒(唤醒不是因为事件的发生,而是由信号唤醒的),因此检查并处理信号。
    通过判定signal_pending(current)来确定是否有信号,而通过(!condition)来确定是否有事件,signal_pending(current)的作用主要是区分interruptable和Uninterruptable的。如果signal_pending(current)为真,即有信号,则处理信号后将再次让出CPU控制权(继续等待事件)。

|----------------------------------------------|
| if (signal_pending(current)) |
| /* handle signal */ |
|----------------------------------------------|

5. Tests whether the condition is true. If it is, there is no need to sleep. If it is not true, the task calls schedule().
本进程在此处交出CPU控制权,如果该进程再次被唤醒,将从while循环结尾处继续执行,因而将回到while循环的开始处while (!condition),进测等待事件是否真正发生.
|----------------------------------------------|
| schedule(); |
|----------------------------------------------|
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  join linux signal up