等待队列:
在 Linux 驱动程序设计中,可以使用等待队列来实现进程的阻塞.
等待队列可以看作保存进程的容器,在阻塞进程时,将进程放入等待队列;
当唤醒进程时,从等待队列中取出进程.
等待队列的 定义 和 初始化 wait_queue_head_t DECLARE_WAIT_QUEUE_HEAD :
Linux 2.6 内核提供了如下关于等待队列的操作:
1,定义等待队列.
wait_queue_head_t my_queue
2,初始化等待队列.
init_waitqueue_head ( &my_queue )
3,定义并初始化等待队列.
DECLARE_WAIT_QUEUE_HEAD ( my_queue )
等待队列的 睡眠 wait_event_interruptible :
有条件睡眠:
1, wait_event ( queue , condition )
当 condition ( 一个布尔表达式 ) 为真,立即返回;否则让进程进入 TASK_UNINTERRUPTIBLE 模式
睡眠,并挂在 queue 参数所指定的等待队列上.
2, wait_event_interruptible ( queue , condition )
当 condition ( 一个布尔表达式 ) 为真,立即返回;否则让进程进入 TASK_INTERRUPTIBLE 模式
睡眠,并挂在 queue 参数所指定的等待队列上.
3, int wait_event_killable ( wait_queue_t queue , condition )
当 condition ( 一个布尔表达式 ) 为真,立即返回;否则让进程进入 TASK_KILLABLE 模式
睡眠,并挂在 queue 参数所指定的等待队列上.
无条件睡眠:
( 老版本,不建议使用 )
sleep_on ( wait_queue_head_t *q )
让进程进入 不可中断 的睡眠,并把它放入等待队列 q.
interruptible_sleep_on ( wait_queue_head_t *q )
让进程进入 可中断 的睡眠,并把它放入等待队列 q.
等待队列中唤醒进程 wake_up :
wake_up ( wait_queue_t *q )
从等待队列 q 中唤醒状态为 TASKUNINTERRUPTIBLE ,TASK_INTERRUPTIBLE ,TASK_KILLABLE
的所有进程.
wake_up_interruptible ( wait_queue_t *q )
从等待队列 q 中唤醒状态为 TASK_INTERRUPTIBLE 的进程.
实例 --- 按键驱动程序 优化:
下面列出一个实例,方便理解和使用 等待队列:
比如我们在编写 按键驱动程序的时候,我们的 应用程序 采用 while(1) 一直去 read 按键值,这样的话 CPU 消耗占用过大;
所以,我们采用 等待队列 来优化按键驱动程序:
首先 定义并且初始化 等待队列:
在程序开头 定义并且初始化 等待队列 DECLARE_WAIT_QUEUE_HEAD :
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
并定义一个 static volatile 变量 :
static volatile int ev_press = 0;
然后 在 read 方法中 将等待队列睡眠:
在有按键按下的时候,读取按键值;
在 没有按键按下 的情况下将等待队列睡眠睡眠 wait_event_interruptible :
static int tq2440_irq_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
unsigned long err;
if (!ev_press)
{
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
else
wait_event_interruptible(button_waitq, ev_press);
}
ev_press = 0;
err = copy_to_user(buff, (const void *)key_values, min(sizeof(key_values), count));
return err ? -EFAULT : min(sizeof(key_values), count);
}
再在 中断服务程序中 唤醒等待队列:
在 中断服务程序中 唤醒等待队列 wake_up_interruptible :
在 按键按下时,进入中断服务程序,在这时候 将等待队列唤醒:
static irqreturn_t irq_interrupt(int irq, void *dev_id)
{
struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id;
int down;
down = !s3c2410_gpio_getpin(button_irqs->pin);
if (down != (key_values[button_irqs->number] & 1))
{
key_values[button_irqs->number] = '0' + down;
ev_press = 1;
wake_up_interruptible(&button_waitq);
}
return IRQ_RETVAL(IRQ_HANDLED);
}
通过 对 static volatile int ev_press 变量的值 判断,来确定 等待队列 是睡眠 还是马上读取键值 .
测试 --- 按键驱动程序:
1,insmod 驱动;
2,在后台运行 测试应用程序:
3,ps 命令查看 应用程序状态:
buttons stat 状态为 sleep;
4,cat /proc/interrupts 命令 看中断有没有被申请:
看各个按键的中断有没有被申请 KEY1-4 :
5,测试按键:
wait_event_timeout(wq, condition, timeout)
表示的有以下两个意思:
(1)当condition为真的时候,会返回
(2)当timeout到达时也会返回,不管此时condition为真为假都会返回
此时接着执行wait_event_timeout之后的代码,只要退出wait_event_timeout,进程就被置为TASK_RUNNING(因为源码中,在退出函数时,会调用到__set_current_state(TASK_RUNNING);)
这个函数是将进程睡眠,置状态为TASK_UNINTERRUPTIBLE直到condition为真,每一次调用唤 醒函数wake_up时都会检查condition,condition为假就继续等待,每次改变任何会导致condition变化的变量时候,都会调用wake_up()
函数返回0:表示timeout超时
返回一个正数:表示还没有超时,但condition变为真,返回剩余的时间
点击(此处)折叠或打开
- /**
- * wait_event_timeout - sleep until a condition gets true or a timeout elapses
- * @wq: 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_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.
- *
- * The function returns 0 if the @timeout elapsed, or the remaining
- * jiffies (at least 1) if the @condition evaluated to %true before
- * the @timeout elapsed.
- */
查看源码,wait_event_timeout( )的返回值,是调用schedule_timeout()返回的
schedule_timeout()表示的进程睡眠直到时间超时,函数就会立即返回,除非进程状态被设置
有以下两种情况:
TASK_UNINTERRUPTIBLE:此时函数就会返回0,需要等待超时时间到才行
TASK_INTERRUPTIBLE:如果一个信号唤醒这个进程,函数就会提前返回,返回值为剩余的jiffies值;也可能是返回0,此时是超时时间刚好到
点击(此处)折叠或打开
- /**
- * schedule_timeout - sleep until timeout
- * @timeout: timeout value in jiffies
- *
- * Make the current task sleep until @timeout jiffies have
- * elapsed. The routine will return immediately unless
- * the current task state has been set (see set_current_state()).
- *
- * You can set the task state as follows -
- *
- * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
- * pass before the routine returns. The routine will return 0
- *
- * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
- * delivered to the current task. In this case the remaining time
- * in jiffies will be returned, or 0 if the timer expired in time
- *
- * The current task state is guaranteed to be TASK_RUNNING when this
- * routine returns.
- *
- * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
- * the CPU away without a bound on the timeout. In this case the return
- * value will be %MAX_SCHEDULE_TIMEOUT.
- *
- * In all cases the return value is guaranteed to be non-negative