Linux内核模块中内核线程之间的通信

发布于 2024-12-06 04:46:54 字数 288 浏览 1 评论 0原文

我刚刚开始学习在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻想少年梦 2024-12-13 04:46:54

您面临两个不同的问题:

  1. 奴隶和主人之间的实际通信。您可以使用内核中的 FIFO 实现 (kernel/kfifo.c)。
  2. 您需要在不忙等待/轮询的情况下对主设备进行解复用。您可以像在用户空间中一样,通过“事件文件描述符”(eventfd)上的 poll/epoll 来完成此操作。查看 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:

  1. The actual communication between the slaves and the master. You can use the FIFO implementation in the kernel (kernel/kfifo.c).
  2. You need demultiplexing for the master without busy-waiting/polling. You can do it just like in userspace, via poll/epoll on an "event file descriptor" (eventfd). Take a look at the kernel-level API in include/linux/eventfd.h (the implementation is in fs/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 at fs/select.c to have an idea about how you should call do_poll().

You might want to use mutexes to guard the FIFO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文