简体   繁体   English

/ proc的内核模块

[英]Kernel module for /proc

How to write a kernel module that creates a directory in /proc named mymod and a file in it name is mymodfile. 如何编写在/ proc中创建名为mymod的目录并在其中命名为mymodfile的内核模块。 This file should accept a number ranged from 1 to 3 when written into it and return the following messages when read based on the number already written into it: 写入文件时,该文件应接受一个介于1到3之间的数字,并根据已写入的数字读取该文件时返回以下消息:

• 1: Current system time (in microseconds precision) •1:当前系统时间(以微秒为单位)
• 2: System uptime •2:系统正常运行时间
• 3: Number of processes currently in the system •3:系统中当前的进程数

“The Output” “输出”

root@Paradise# echo 1 > /proc/mymod/mymodfile
root@Paradise# cat /proc/mymod/mymodfile
08:30:24 342us
root@Paradise# echo 2 > /proc/mymod/mymodfile
root@Paradise# cat /proc/mymod/mymodfile
up 1 day, 8 min 
root@Paradise# echo 3 > /proc/mymod/mymodfile
root@Paradise# cat /proc/mymod/mymodfile
process count: 48 

please give me some hint how to write a kernel modules and how to compile and install it . 请给我一些提示,说明如何编写内核模块以及如何编译和安装内核模块。

您正在寻找的是《 Linux内核模块编程指南》 ,特别是/ proc文件系统上的这一 ,其中提供了详细记录如何添加新条目的示例。

There is an article about this in a french magazine called "Gnu/Linux magazine" this month. 本月在法国杂志“ Gnu / Linux magazine”上有一篇有关此的文章。

I don't have my bookmarks here, but theses links look ok: 我这里没有书签,但这些链接看起来不错:

http://www.linuxhq.com/lkprogram.html http://www.linuxhq.com/lkprogram.html

http://tldp.org/HOWTO/Module-HOWTO/x839.html http://tldp.org/HOWTO/Module-HOWTO/x839.html

http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html

This might be easier to do using sysfs. 使用sysfs可能更容易做到。 Sysfs was designed with these sorts of operations in mind, and has simple functions for creating directories and virtual files and callbacks for read and write operations to those files. Sysfs在设计时就考虑到了这类操作,并且具有用于创建目录和虚拟文件的简单功能以及用于对这些文件进行读写操作的回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM