上面提到的采用schedule()函数的解决方法之所以还不是最佳的,其根本原因在于调用schedule()函数的进程依然处于CPU的运行队列中。为了解决这个问题,此时应该能想到内核提供的另外一种可供设备驱动程序使用的调度类的基础设施:schedule_timeout。所以,如果一个延迟1 s的函数可以用下面的这样一个简单的代码段来实现:
- delay_1s()
- {
- set_current_state(TASK_UNINTERRUPTIBLE) ;
- schedule_timeout(jiffies + HZ);
- }
上面这段代码之所以可以解决直接采用schedule()函数所带来的负面问题,主要在于在调用schedule_timeout之前先调用了set_current_state宏将当前进程的状态设置为TASK_UNINTERRUPTIBLE,这样当随后的schedule_timeout函数被调用时,后者的内部实现中调用了schedule()函数,因为当前进程之前的状态已经被设置为TASK_UNINTERRUPTIBLE,所以在schedule函数中当前进程将会被移出处理器的运行队列,因此也就解决了采用直接调用schedule函数那种方案所带来的不利影响。
一、适用情况
wait_event_interruptible_timeout和schedule_timeout的区别
当在某个硬件驱动程序中使用wait_event_interruptible_timeout时,执行的继续可以通过以下两种方式获得:
1.其他人在等待队列上调用了wake_up
2.超时到期。
而调用schedule_timeout的进程始终会在超时到期时被唤醒。
二、函数原型
wait_event_interruptible_timeout(queue, condition, timeout)
返回值:
大于零:剩余时间(以jiffy计)
零:给定的时间到期。
signed long schedule_timeout(signed long timeout);
返回值:
零:正常返回值
大于零:函数提前返回(比如响应某个信号),返回值为剩余时间
三、使用实例
1.wait_event_interrruptible_timeout
(1)初始化等待队列
int flags = 0;
wait_queue_head_t select_wait;
init_waitqueue_head(&select_wait);
(2)等待事件的发生(条件满足)
{
...
wait_event_interruptible_timeout(select_wait, flags != 0, HZ/10);
...
}
(3)唤醒等待队列
{
...
if(waitqueue_active(&select_wait))
{
flags = 1;
wake_up_interruptible( &nd->select_in_wait );
}
...
}
wait_event_interruptible_timeout依赖schedule_timeout的实现的,或者说wait_event_interruptible_timeout是由schedule_timeout实现的