Linux内核/proc FIFO/管道 [英] Linux kernel /proc FIFO/pipe

查看:137
本文介绍了Linux内核/proc FIFO/管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试创建一个内核模块,该模块将根据内核事件生成数据并将其推送到文件中.在读完这是不好的(我同意)之后,我决定将数据存储在/proc文件中,在必要时用户程序可以从中提取该数据更有意义.但是,这种想法导致了各种各样的问题,尤其是何时以及如何清除此文件.所以我想...为什么不在/proc中创建一个命名管道并从中读取?"

I'm currently trying to create a kernel module that will produce data based on kernel events and push them to a file. After reading that this is bad (and I agree), I decided it would make more sense to have the data in a /proc file that a user program could pull from when necessary. However, this idea led to all sorts of problems, particularly when and how to clear out this file. So I thought... "why don't I make a named pipe in /proc and read from that?"

我已经为proc文件设置了读取功能和写入功能,但是我仍然在概念上遇到麻烦.即,我将如何编写一个函数以获取任意数据并将其从内核写入这样的管道?有谁知道如何将数据从内核空间推送到命名管道?最后,它没有必须作为/proc文件(特别是如果这样做对我来说是不对的),但这就是我得出的结论.然后,我必须弄清楚如何从用户空间程序附加到它,但是我觉得那是一个单独的问题.

I've got the general gist of setting a read function and a write function for a proc file, but I'm still having conceptual trouble with how I'd go about this. Namely, how would I write such a function to take arbitrary data and write it to such a pipe from the kernel? Does anyone have any idea how you'd push data to a named pipe from kernel-space? In the end, it doesn't have to be a /proc file (particularly if it's wrong of me to do so), but that was the conclusion I came to. Then I'll have to figure out how to attach to it from a userspace program, but I feel that's a separate issue.

推荐答案

您要做的不是创建命名管道,而是创建一个字符设备".如果要进行简单的交互或将数据从内核传输到用户空间,然后再返回,这是通常的方法.我建议在Linux内核中查找类似的设备,然后看看它们的作用.

Instead of making a named pipe, what you want to do is create a "Character device". If you want simple interaction or streaming data from kernel to userspace and back, this is the usual method. I'd recommend looking up similar devices in the Linux kernel and seeing what they do.

这篇关于Linux内核/proc FIFO/管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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