Linux内核模块中内核线程之间的通信
我刚刚开始学习在 Linux 内核 2.6 上制作内核模块的技巧。我想要做的是拥有 3 个内核线程(称为从属线程),它们需要将数据发送到第四个内核线程(称为主线程),并接收各自的响应。从站可以随时请求,这意味着我需要某种队列结构和一种将响应重定向到正确线程的方法。
首先,我考虑实现自己的队列结构来对传入请求进行排队 - 但我如何向主机发出信号?我不希望主机继续轮询(就像自旋锁/信号量的情况一样)。我感觉有一种更好的方式在线程之间进行通信。
由于缺乏文档(而且搜索技能确实较差),我不知道如何实现这一点。你能指出我正确的方向吗?
I'm just beginning to learn the tricks of making a kernel module on linux kernel 2.6. What I'm looking to do is have 3 kernel threads, called the slaves, that need to send data to a 4th kernel thread, called master, and receive their respective responses. The slaves can request at any time, which means I will need some sort of a queue structure and a way to redirect responses to the correct thread.
First I looked at implementing my own queue structure to queue incoming requests - but how do I signal the master of this? I don't want the master to keep polling (as in the case of spinlocks/semaphores). I have a feeling there is a better way to communicate between threads.
Due to lack of documentation (and admittedly inferior searching skills), I'm at a loss on how to implement this. Can you point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您面临两个不同的问题:
kernel/kfifo.c
)。include/linux/eventfd.h
中的内核级 API(实现在fs/eventfd.h
中)。您可能应该为每个从属线程使用 [kfifo,事件文件] 对。主线程阻塞在
do_poll()
调用中,当被唤醒时,能够根据“发出信号”的fd使用正确的FIFO。查看fs/select.c
以了解应如何调用do_poll()
。您可能想要使用互斥锁来保护 FIFO。
You are facing two distinct problems:
kernel/kfifo.c
).include/linux/eventfd.h
(the implementation is infs/eventfd.h
).You should probably use a [kfifo, event file] pair for each slave thread. The master thread blocks in a
do_poll()
call and, when woken up, is able to use the right FIFO based on the fd that was "signaled". Take a look atfs/select.c
to have an idea about how you should calldo_poll()
.You might want to use mutexes to guard the FIFO.