如何使用 Linux 工作队列 [英] How to use Linux Work Queue
问题描述
Linux 工作队列是具有进程上下文的内核级线程.我试图将它用作没有特定进程上下文的 kthread 的替代方案.但是如何将数据传递到工作队列?work_struct 有一个类型为 atomic_long_t 的数据字段.我无法传递指向该字段的指针.我该怎么做?
Linux work queues are meant to be kernel level threads with process context. I was trying to use it as an alternative to kthread which has no specific process context. But how do I pass data to work queue? work_struct has a data field which is of type atomic_long_t. I could not pass pointer to this field. How do I do it?
我也找不到工作队列的一个具体例子.你能推荐一个吗?
Also I could not find a single concrete example of work queue. Can you suggest one?
推荐答案
如果你想将数据传递给你的工作队列函数,只需将 work_struct
结构嵌入你自己的数据结构中并使用 container_of
在您的工作函数中检索它.
If you want to pass data to your work queue function, just embed the work_struct
structure inside your own data structure and use container_of
inside your work function to retrieve it.
举个简单的例子,内核中充满了它——只是git grep work_struct
.您可以查看 drivers/cpufreq/cpufreq.c
(handle_update
函数)的简单示例.下面的文章也在最后嵌入了一个示例,但它没有使用 container_of
而是依赖于结构的第一个成员与其父级具有相同地址的事实:
As for a simple example, the kernel is full of it - just git grep work_struct
. You can look at drivers/cpufreq/cpufreq.c
(handle_update
function) for a simple example. The article below also embeds an example at the end, but it does not use container_of
and instead relies on the fact that the first member of a structure has the same address as its parent:
http://www.ibm.com/developerworks/linux/library/l-tasklets/index.html
这篇关于如何使用 Linux 工作队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!