内核和用户应用程序之间通信的方法

发布于 2024-07-13 03:52:58 字数 585 浏览 6 评论 0原文

Linux 编程新手。

我试图通过共享内存与内核模块进行通信,但无法调用用户应用程序中使用的函数,例如 shmget(); 我已经安装了 kernel-headers 和 kernel-devel,并包括, 到内核​​模块源代码,但标头不包含任何函数。

内核中是否有使用共享内存的标准方法?

此外,有关如何在相同情况下使用套接字的任何提示都会有很大帮助。

使用 发行版:CentOS 5.2 内核:2.6.18-92.1.22.e15

提前致谢。

//添加// 为了澄清这一点,我正在尝试制作一个具有典型嵌入式的共同特征的演示 Linux项目。 最终目标是移植到LynxOS上,看看会出现什么样的麻烦 在较大的项目中这样做时会发生。

目前该演示的主要功能是 多进程、管道、消息队列、共享内存、套接字、多线程。

它有一个用户应用程序和一个可加载的内核模块,可以相互通信。 用户应用程序内两个进程之间的管道和消息,以及共享内存和 用户应用程序和内核模块之间的套接字。

如果我做了一些明显偏离常规的事情,请告诉我。

New to Linux programming in general.

I am trying to communicate with a kernel module via shared memory, but cannot call the functions used in user apps such as shmget();
I have installed kernel-headers and kernel-devel, and included ,
to the kernel module source, but the headers do not contain any functions.

Is there a standard way of using shared memory in the kernel?

Also, any tips on how to use sockets in the same situation will be of great help.

Using
Distro: CentOS 5.2
kernel: 2.6.18-92.1.22.e15

Thanks in advance.

//Added//
To clarify, I am trying to make a demo that has common features of a typical embedded
linux project. The final goal being porting it to LynxOS to see what kind of troubles may
occur when doing so on larger projects.

Currently the main features of the demo is
multi-process, pipes, message ques, shared-memory, sockets, multi-threads.

It has an user app and a loadable kernel module communicating with each other.
pipes and messages between two processes within the user app, and shared-memory and
sockets between the user app and kernel module.

If I am doing something plain off-course, please tell me.

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

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

发布评论

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

评论(3

回忆凄美了谁 2024-07-20 03:52:58

我觉得你的目的不太清楚。 共享内存不是用户空间和内核空间之间通信的方式。 如果您想访问某些内核分配的内存,一种方法是实现 mmap 系统调用。

如果您确实需要编写一些内核代码,建议阅读:
Linux 设备驱动程序第三版,第 15 章就是您正在寻找的内容。

一旦您拥有共享内存,您可能需要一种方法来进行一些同步,即在内核中处理一个部分,同时在用户空间中使用另一部分。 用户空间/内核共享内存的一个示例是 V4L2 API。 更友好的解释可以在LWN系列中找到。

但是,如果您更具体地了解自己想要做什么,那么您将获得有关执行此操作的更具体的帮助,如果您确实需要内核模块,您可以查看哪些内核代码等等。

对于套接字的事情,我不太了解,但你应该谷歌搜索linux + kernel + netlink。 它用于通过“标准”套接字调用从用户空间与内核进行通信,但我真的不知道如何在模块中使用它。

Your purpose does not look very clear to me. Shared memory is not a way to communicate between user space and kernel space. If you want to have access to some kernel allocated memory, one way to go is to implement the mmap system call.

If you really need to write some kernel code, a recommended reading is :
linux device driver third edition, and chapter 15 is the one you are looking for.

Once you have shared memory, you probably will need a way to do some synchronisation, ie working on one part within the kernel, while using another part in user space. An example of userspace / kernel sharing of memory is the V4L2 API. An more friendly explanation can be found in the LWN series about it.

However, if you were more specific in what you want to do, you would receive more specific help on the way to do it, if you really need a kernel module, which kernel code you could have a look at and so on.

For the socket thing, I don't know enough, but you should google for linux + kernel + netlink. It is used to communicate with the kernel from user space via "standard" socket call, but I have really no idea on how to use it in a module.

桜花祭 2024-07-20 03:52:58

内核可以自动访问用户空间内存,内核中已经有很多这样的例子(提示:open()需要访问用户空间来读取要打开的文件名)

不清楚您的用例是什么,请更具体。

用户空间和内核之间通信的正常方式是通过系统调用; 您可以添加自己的(不一定推荐)或扩展一些现有的(字符设备上的 ioctl 是典型的)。

许多内核模块实现了设备特殊文件,这就是(大多数)设备驱动程序所做的(网络接口除外,它们在 Linux 下不是设备特殊文件)

The kernel can access userspace memory automatically, there are plenty of examples of this in the kernel already (Hint: open() needs to access userspace to read the filename to open)

It's not clear what your use case is, please be more specific.

The normal way of communicating between userspace and the kernel is via sys calls; you can add your own (not necessarily recommended) or extend some existing ones (ioctl on a character device is typical).

A lot of kernel modules implement a device-special-file, this is what (most) device drivers do (except network interfaces, which aren't device special files under Linux)

衣神在巴黎 2024-07-20 03:52:58

以下是你可以与内核对话的方法
映射

设备文件(为您编写简单的设备驱动程序,或者您也使用fifo文件,Netlink套接字(网络堆栈),套接字(网络),或者为您编写系统调用......如果您详细告诉我您到底想要实现什么,我可以帮助你更多。

following are ways by which u can talk to kernel
mmap

device files ( write you simple device driver , or you use fifo files as well , Netlink sockets (networking stack ), sockets ( networking) , or write you system call ... if you tell me in detail what exactly you want to acheive i can help you more.

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