Frage

This question seems to be simple, but I want to send an event to notify my user space program that the module buffer is ready to be read.

For example, I have a buffer in my kernel module and its data will be consumed by the user space program. If all the data was consumed, the kernel module has to notify my program when a new data arrived.

It's a typically problem of producer/consumer. The producer is a kernel module and the consumer is a user space program.

Today, I send a signal to my program (event) and access the data buffer using the ioctl function. But I don't know if this approach is good enough to solve this kind of problem. I'm afraid to use netlink or memory mapping unnecessarily to solve this.

War es hilfreich?

Lösung

Read some other modules that do what you want.

There are lots of options for how to do this in the Linux kernel, including:

  • virtual filesystems, e.g. /proc, /sys, configfs, relayfs (really look at relayfs)
  • netlink
  • blocking syscalls
  • poll() / epoll() & related

/proc is probably the easiest to start with since it has been around forever and there is a ton of documentation on how to use it. Create a virtual file that maps to your buffer, then have your userspace app open an fd and use select. Simple and ubiquitous. There are more modern and "better" ways - they will inevitably be described in terms of /proc + select() so learning those first will teach you something useful.

Andere Tipps

For sharing status information of your kernel module, I suggest using any one of the virtual filesystems available (eg: /proc, /sys or if you're debugging debugfs).

However, if you want to transfer large amounts of data to and fro the kernel, then you can look at using netlink sockets or use mmap which is probably the fastest mechanism for transferring data although you will have to implement your own notification mechanism to inform the userspace program when data is ready and vice-versa (for consumption).

See this link for all possible options available under Linux.

There are couple of well known methods to communicate from user space to kernel space.

Above listed all methods use common method to call remote procedure call (RPC) from user space to kernal space.

If you can understand this http://articles.manugarg.com/systemcallinlinux2_6.html then you can develope your own framework in linux kernel for user space to kernel space communication.

You can define a variable in user space and map that to kernel. When the data is ready, kernel will set that variable. In user space, the application can poll the variable. When set, an ioctl can be done to read the data from the kernel. The drawback is that you will have to poll the variable in user space in every defined time but the upside is that you can avoid the locking mechanisms.

You can achieve this with Poll or select system call in the userspace and poll fops in the kernel space. It will be blocked with the poll system call from the userspace. When the data is ready send notification/event to userspace from kernel space. On receiving this event, process can read the data.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top