Linux内核和用户空间程序之间的通信 [英] Communication between Linux kernel and user space program

查看:559
本文介绍了Linux内核和用户空间程序之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我觉得内核用户空间IPC或Unix套接字是甜的,但我没有运气由谷歌找到一个例子。

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

目前我的丑陋的解决方案是导出 chardev 键,让用户空间程序写入请求的设备文件,并从中读取结果。但是我只能发出每一个请求的open()通话,这也是造成新的问题。我真的需要一个IPC或插座性的东西。谢谢!

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!

推荐答案

有实现这个的几种方法。

There are several ways to implement this.

,最简单的是使用proc文件接口进行通信,特别是如果该消息,并将结果在尺寸小于一页。

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.

一般顺序是为​​下:


  • 实施proc_open(),proc_read()和proc_write(); proc_close();

  • 打开和关闭可以实现锁定,因此只有用户空间程序的一个实例可以
    实际访问模块请求引擎。

  • 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.

任务请求是通过对proc文件写入发送

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

写入功能将成功返回如果模块理解命令,返回该程序将初始化请求处理之前,当读proc文件如果是微不足道的处理实际上可以发生。如果处理显著复杂的话,我建议您在下半部 1 (你可以简单地启动一个工作队列)。

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).

在读取或者触发处理你想要的模块做。或为BH等待完成处理的情况下,你这样做的。您可以使用自旋锁或互斥来控制流量。

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.

这篇关于Linux内核和用户空间程序之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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