用户程序和 Linux 内核模块之间的通信有哪些选项?
我是 Linux 内核模块编程的新手。从我目前阅读的材料来看,我发现用户程序有 3 种方式请求服务或与 Linux 内核模块通信
- /dev 中的设备文件
- /proc 文件系统中的文件
- ioctl() 调用
问题:对于用户程序和 Linux 内核模块之间的通信,我们还有哪些其他选项?
I am a new comer to Linux Kernel Module programming. From the material that I have read so far, I have found that there are 3 ways for a user program to request services or to communicate with a Linux Kernel Module
- a device file in /dev
- a file in /proc file system
- ioctl() call
Question: What other options do we have for communication between user program and linux kernel module?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
基本上,许多标准 IPC 机制 — cf. http://en.wikipedia.org/wiki/Inter-process_communication - 可以使用:
文件和内存映射文件:设备文件(如上所述)或 /dev、procfs、sysfs、debugfs 中的类似特殊文件,或者您自己的文件系统、具有读/写、ioctl 的笛卡尔积、 mmap
可能的信号(与 kthread 一起使用)
套接字:使用选择的协议:TCP、UDP(参见
knfsd
,但可能不太容易)、PF_LOCAL 或 Netlink(许多子接口 - 基本 netlink、genetlink、Connector , ...)此外,
4。 系统调用(虽然不能真正从模块中使用)
5。 网络接口(类似于 tun)。
中找到。
Basically, many standard IPC mechanisms — cf. http://en.wikipedia.org/wiki/Inter-process_communication — can be used:
File and memory-mapped file: a device file (as above) or similarly special file in /dev, procfs, sysfs, debugfs, or a filesystem of your own, cartesian product with read/write, ioctl, mmap
Possibly signals (for use with a kthread)
Sockets: using a protocol of choice: TCP, UDP (cf.
knfsd
, but likely not too easy), PF_LOCAL, or Netlink (many subinterfaces - base netlink, genetlink, Connector, ...)Furthermore,
4. System calls (not really usable from modules though)
5. Network interfaces (akin to tun).
Working examples of Netlink — just to name a few — can be found for example in
这包括带有示例的所有类型:)
http://people.ee.ethz。 ch/~arkeller/linux/kernel_user_space_howto.html
This includes all types with examples :)
http://people.ee.ethz.ch/~arkeller/linux/kernel_user_space_howto.html
这个 Linux 文档给出了内核和用户空间交互的一些方式(通信)。他们是以下几位。
Procfs
、sysfs
和类似机制。这还包括 /dev 条目,以及内核空间在用户空间中公开文件的所有方法(/proc、/dev 等条目基本上是从内核空间公开的文件)。Socket
的机制。Netlink
是一种套接字,专门用于用户空间和内核空间之间的通信。系统调用
。上行调用
。内核在用户空间执行代码。例如产生一个新进程。mmap
- 将内核内存区域映射到用户空间的内存。这允许内核和用户空间读/写同一内存区域。除此之外,下面的列表还添加了一些我所知道的其他机制。
中断
。用户空间可以引发中断来与内核空间对话。例如,某些 CPU 使用int80
进行系统调用(而其他 CPU 可能使用不同的机制,例如syscall
指令)。内核必须提前定义相应的中断处理程序。vDSO/vsyscall
- 这些是 Linux 内核中用于优化某些系统调用执行的机制。其想法是拥有一个共享内存区域,当进程进行系统调用时,用户空间库从该区域获取数据,而不是实际调用相应的系统调用。这节省了上下文切换开销。This Linux document gives some of the ways in which the kernel and user space can interact(communicate). They are the following.
Procfs
,sysfs
, and similar mechanisms. This includes/dev
entries as well, and all the methods in which kernel space exposes a file in user space (/proc, /dev, etc. entries are basically files exposed from the kernel space).Socket
based mechanisms.Netlink
is a type of socket, which is meant specially for communication between user space and kernel space.System calls
.Upcalls
. The kernel executes a code in user space. For example spawning a new process.mmap
- Memory mapping a region of kernel memory to user space. This allows both the kernel, and the user space to read/write to the same memory area.Other than these, the following list adds some other mechanisms I know.
Interrupts
. The user space can raise interrupts to talk to kernel space. For example some CPUs useint80
to make system calls (while others may use a different mechanism likesyscall
instruction). The kernel has to define the corresponding interrupt handler in advance.vDSO/vsyscall
- These are mechanisms in Linux kernel to optimize execution of some system calls. The idea is to have a shared memory region, and when a process makes a system call, the user space library gets data from this region, instead of actually calling the corresponding system call. This saves context switch overhead.一切可运行的例子
太多的谈话让我感到无聊!
文件操作:
实现文件操作的文件类型:
文件操作系统调用本身
打开
,读取
、写入
、关闭
、lseek
。另请参阅:如何添加轮询函数到内核模块代码?投票
。另请参阅:如何使用 ioctl () 来操作我的内核模块?ioctl
。另请参阅:如何使用 ioctl () 来操作我的内核模块?mmap
。另请参阅:如何映射 Linux内核缓冲区到用户空间?netlink 套接字。另请参阅:如何使用 netlink 套接字进行通信与内核模块
Runnable examples of everything
Too much talk is making me bored!
file operations:
file types that implement file operations:
file operation syscalls themselves
open
,read
,write
,close
,lseek
. See also: How to add poll function to the kernel module code?poll
. See also: How do I use ioctl() to manipulate my kernel module?ioctl
. See also: How do I use ioctl() to manipulate my kernel module?mmap
. See also: How to mmap a Linux kernel buffer to user space?netlink sockets. See also: How to use netlink socket to communicate with a kernel module?
您的选项 3) 实际上是选项 1) 的子选项 -
ioctl()
是与设备文件交互的一种方式(read()
和write ()
是通常的方式)。另外两种值得考虑的方法是:
Your option 3) is really a sub-option of option 1) -
ioctl()
is one way of interacting with a device file (read()
andwrite()
being the usual ways).Two other ways worth considering are:
sysfs
filesystem;