Linux kernel 5.x wait_event_interruptible_timeout()

本文详细介绍了Linux内核函数wait_event_interruptible_timeout的使用,包括其参数、功能及返回值含义。通过分析内核源码注释,阐述了该函数如何在等待队列中进行等待,直到条件满足或超时,同时讨论了信号中断的影响。此外,还给出了基本的使用步骤,包括等待队列头的声明、初始化和唤醒操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看看内核源码的注释,这往往是在研究内核函数必读的一段文字,这将会给我们理解内核代码执行逻辑带来很大的帮助,一定要花时间好好理解;

函数在内核代码被定义为一个宏:

#define __wait_event_interruptible_timeout(wq_head, condition, timeout)         \
        ___wait_event(wq_head, ___wait_cond_timeout(condition),                 \
                      TASK_INTERRUPTIBLE, 0, timeout,                           \
                      __ret = schedule_timeout(__ret))

/**

  • wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
  • @wq_head: the waitqueue to wait on
  • @condition: a C expression for the event to wait for
  • @timeout: timeout, in jiffies
  • The process is put to sleep (TASK_INTERRUPTIBLE) until the
  • @condition evaluates to true or a signal is received.
  • The @condition is checked each time the waitqueue @wq_head is woken up.
  • wake_up() has to be called after changing any variable that could
  • change the result of the wait condition.
  • Returns:
  • 0 if the @condition evaluated to %false after the @timeout elapsed,
  • 1 if the @condition evaluated to %true after the @timeout elapsed,
  • the remaining jiffies (at least 1) if the @condition evaluated
  • to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
  • interrupted by a signal.
    */

参数一:等待队列头,struct wait_queue_head_t
参数二:等待条件
参数三:延时时间(10*HZ表示10s)

等待进程被唤醒条件:一直睡眠直到条件成立或者是定时时间到

代码注释详细解释了不同情况下函数的返回值:

返回值state
0condition在timeout之后为状态为false
1condition在timeout之后为状态为ture
jiffiescondition在timeout之前为被置为ture
-ERESTARTSYS(-512)进程被信号中断(kill)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

基本的使用方法:

1.声明等待队列头
 wait_queue_head_t wait_queue
2.初始化等待队列头
 init_waitqueue_head(&wait_queue)
3.条件不成立让wait queue进入等待队列
 wait_event_interruptible_timeout(wait_queue,condition,timeout)
4.条件满足或者是延时时间到
 wake_up_interruptible(&wait_queue)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值