开发者

Kernel module for /proc

开发者 https://www.devze.com 2022-12-29 10:58 出处:网络
How to write a kernel module that creates a directory in /proc named mymod and a file in it name is mymodfile. This file should accept a number ranged from 1 to 3 when written into it and return the f

How to write a kernel module that creates a directory in /proc named mymod and a file in it name is 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: Current system time (in microseconds precision)

• 2: System uptime

• 3: Number of processes currently in the system

“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 .


What you're looking for is the Linux Kernel Module Programming Guide, specifically the section on the /proc filesystem, which has well documented examples of how to add new entries.


There is an article about this in a french magazine called "Gnu/Linux magazine" this month.

I don't have my bookmarks here, but theses links look ok:

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

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

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


This might be easier to do using 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消