Linux内核中的wait_queue_head和wait_queue之间的区别 [英] The difference between wait_queue_head and wait_queue in linux kernel

查看:347
本文介绍了Linux内核中的wait_queue_head和wait_queue之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以找到许多有关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, someone can 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_head_t类型是一个相当简单的结构,在 <linux/wait.h>.它仅包含一个锁变量和一个链表 睡眠过程.列表中的单个数据项为 键入wait_queue_t,该列表是在中定义的通用列表 <linux/list.h>.

The wait_queue_head_t type is a fairly simple structure, defined in <linux/wait.h>. It contains only a lock variable and a linked list of sleeping processes. The individual data items in the list are of type wait_queue_t, and the list is the generic list defined in <linux/list.h>.

通常,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.

看看更深入的了解等待队列部分.

但是,某些高级应用程序可能需要处理 直接使用wait_queue_t变量.对于这些,值得快速浏览 实际上在像interruptible_sleep_on这样的函数中发生了什么. 以下是实施的简化版本 interruptible_sleep_on使进程进入睡眠状态:

Some advanced applications, however, can require dealing with wait_queue_t variables directly. For these, it's worth a quick look at what actually goes on inside a function like interruptible_sleep_on. The following is a simplified version of the implementation of interruptible_sleep_on to put a process to sleep:

 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之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆