单单一个wait_event_interruptible()就能唤醒自己?

winmenaruto 2012-03-13 05:07:16
wait_event_interruptible()。该函数修改task的状态为TASK_INTERRUPTIBLE,意味着改进程将不会继续运行直到被唤醒,然后被添加到等待队列wq中。
在wait_event_interruptible()中首先判断condition是不是已经满足,如果是则直接返回0,否则调用__wait_event_interruptible(),并用__ret来存放返回值
---------------------------------------------------------------
#define wait_event_interruptible(wq, condition) \
({ \
int __ret = 0; \
if (!(condition)) \
__wait_event_interruptible(wq, condition, __ret);\
__ret; \
})
wait_event_interruptible() --> __wait_event_interruptible()
__wait_event_interruptible()首先定义并初始化一个wait_queue_t变量__wait,其中数据为当前进程current,并把__wait入队。

在无限循环中,__wait_event_interruptible()将本进程置为可中断的挂起状态,反复检查condition是否成立,如果成立
则退出,如果不成立则继续休眠;条件满足后,即把本进程运行状态置为运行态,并将__wait从等待队列中清除掉,从而进程能够调度运行。如果进程当前有
异步信号(POSIX的),则返回-ERESTARTSYS。

那既然这样,也就是单单一个wait_event_interruptible()就能唤醒自己 ?
我在某处
int flag = 0;
wait_event_interruptible(wq,flag !=0);
就会睡眠了,那我在其他函数里:flag =1 ;
按照上面的说法,进程就唤醒了??那要wake_up_interruptible()干嘛????
...全文
329 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvyinghong 2012-03-14
  • 打赏
  • 举报
回复
281/**
282 * wait_event_interruptible - sleep until a condition gets true
283 * @wq: the waitqueue to wait on
284 * @condition: a C expression for the event to wait for
285 *
286 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
287 * @condition evaluates to true or a signal is received.
288 * The @condition is checked each time the waitqueue @wq is woken up.
289 *
290 * wake_up() has to be called after changing any variable that could
291 * change the result of the wait condition.
292 *
293 * The function will return -ERESTARTSYS if it was interrupted by a
294 * signal and 0 if @condition evaluated to true.
295 */


看看函数丶注释,你做了任何可能改了条件判断结果的修改后,需要自己调用wake_up 来唤醒的。


__wait_event_interruptible 只是把自己调度出去,进程就休眠了,等待别人的唤醒。 你单纯的吧flag改成1他是不知道的,就像一个人一样在那睡觉呢,你要把它弄醒了,然后他才有机会检查条件是不是满足了,然后决定继续睡还是成功返回然后干活去。

4,441

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧