问题描述
我可以找到许多有关wait_queue_head
的示例.它作为信号,创建wait_queue_head
,有人可以睡觉使用它,直到有人踢它.
I can find many examples regarding wait_queue_head
.It works as a signal, create a wait_queue_head
, someonecan sleep using it until someother kicks it up.
但是我找不到使用wait_queue
本身的很好的例子,据推测与它非常相关.
But I can not find a good example of using wait_queue
itself, supposedly very related to it.
有人可以举个例子,还是在他们的掩盖下?
Could someone gives example, or under the hood of them?
推荐答案
来自 Linux设备驱动程序:
通常,wait_queue_t
结构是通过以下方式在堆栈上分配的: 像interruptible_sleep_on
这样的功能;结构最终出现在 堆栈,因为它们只是在变量中被声明为自动变量 相关功能.通常,程序员无需处理 他们.
Normally the wait_queue_t
structures are allocated on the stack by functions like interruptible_sleep_on
; the structures end up in the stack because they are simply declared as automatic variables in the relevant functions. In general, the programmer need not deal with them.
看看更深入的了解等待队列部分.
void simplified_sleep_on(wait_queue_head_t *queue)
{
wait_queue_t wait;
init_waitqueue_entry(&wait, current);
current->state = TASK_INTERRUPTIBLE;
add_wait_queue(queue, &wait);
schedule();
remove_wait_queue (queue, &wait);
}
此处的代码创建了一个新的wait_queue_t变量(wait, 在堆栈上分配)并对其进行初始化.任务状态为 设置为TASK_INTERRUPTIBLE,表示它处于可中断状态 睡觉.然后将等待队列条目添加到队列( wait_queue_head_t *参数).然后计划被调用, 放弃处理器给其他人.时间表只返回 当其他人唤醒了进程并将其状态设置为 TASK_RUNNING.届时,等待队列条目将从 排队,睡觉就结束了
The code here creates a new wait_queue_t variable (wait, which gets allocated on the stack) and initializes it. The state of the task is set to TASK_INTERRUPTIBLE, meaning that it is in an interruptible sleep. The wait queue entry is then added to the queue (the wait_queue_head_t * argument). Then schedule is called, which relinquishes the processor to somebody else. schedule returns only when somebody else has woken up the process and set its state to TASK_RUNNING. At that point, the wait queue entry is removed from the queue, and the sleep is done
等待队列中涉及的数据结构的内部:
The internals of the data structures involved in wait queues:
更新:对于认为图片是我自己的用户-这是另外一次指向 Linux设备驱动程序图像取自
Update:for the users who think the image is my own - here is one more time the link to the Linux Device Drivers where the image is taken from
这篇关于Linux内核中的wait_queue_head和wait_queue之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!