Linux内核与用户空间程序之间的通信

发布于 2024-12-15 11:57:59 字数 306 浏览 4 评论 0原文

我目前正在编写一个 Linux 内核模块,在实现与用户空间程序的通信时遇到问题。

该内核模块需要接收用户空间程序发出的任务,并在完成后将结果发送回用户空间程序。当内核模块正在执行其工作时,用户空间程序应该被阻止。

我认为内核-用户空间 IPC 或 Unix 套接字会很不错,但我没能找到 Google 的示例。

目前我丑陋的解决方案是导出 chardev 并让用户空间程序向设备文件写入请求,并从中读取结果。但每次 open() 调用我只能发出一个请求,这会导致新问题。我真的需要一个IPC或者类似socket的东西。谢谢!

I'm currently writing a Linux kernel module, and have problems implementing its communication with user space programs.

This kernel module needs to receive tasks issued by a user space program, and send results back to user space program after completion. The user space program should be blocked while the kernel module is doing its job.

I think a kernel-user space IPC or an Unix socket would be sweet, but I had no luck finding an example by Google.

Currently my ugly solution is to export a chardev and let user space program write requests to the device file, and read results from it. But I can only issue one request per open() call, and this is causing new problems. I really need an IPC or socket-like thing. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

深居我梦 2024-12-22 11:57:59

有几种方法可以实现这一点。

最简单的方法是使用 proc 文件接口进行通信,特别是当消息和结果的大小小于一页时。

一般顺序如下:

  • 实现 proc_open()、proc_read() 和 proc_write(); proc_close();
  • open和close可以实现锁定,这样只有一个用户空间程序实例可以
    实际访问模块请求引擎。

  • 任务请求是通过写入proc文件发送的,

  • 如果模块理解该命令,则写入函数将成功返回,之前返回程序将初始化请求处理,如果不重要的话,处理实际上可以在读取 proc 文件时进行。如果处理非常复杂,那么我建议您阅读下半部分1(你可以简单地启动一个工作队列)。

  • 读取会触发“您希望模块执行的处理”。或者等待 BH 完成处理(如果您这样做)。您可以使用自旋锁或互斥体来控制流程。

  • 内核处理完成后返回结果。

There are several ways to implement this.

The easiest is to use the proc file interface to communicate, especially if the message and the result are less than one page in size.

General Sequence would be as under:

  • Implement proc_open(), proc_read() and proc_write(); proc_close();
  • Open and close can implement locking so that only one instance of userspace program can
    actually access the module request engine.

  • the task request is sent via a write to the proc file,

  • The write function will return successfully if the module understands the command, before returning the program will initialize the request processing, the processing can actually take place when the proc file is read if it is trivial. If the processing is significantly complex then i suggest that you read up on bottom halves1 (you can simply start a working queue).

  • The read either triggers the "processing you want the module to do". or waits for the BH to finish the processing in case you do it that way. You can use a spinlock or a mutex to control flow.

  • The kernel processing returns the result upon completion.

一瞬间的火花 2024-12-22 11:57:59

不使用普通套接字、proc fs 并实现新的系统调用,而是使用 netlink 套接字,它提供用户空间程序和内核模块之间的全双工通信。

Instead of using normal sockets, proc fs and implementing a new system call, Use netlink sockets which offers full duplex communication between user space programs and kernel modules.

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