Kernel module for / proc

How do I write a kernel module that creates a directory in / proc called mymod and the file in it is mymodfile. This file should accept a number between 1 and 3 when it is written and return the following messages when read based on the number already written to it:

• 1: current system time (microsecond precision)
• 2: System uptime
• 3: number of processes currently on the system.

"Exit"

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 let me understand how to write kernel modules and how to compile and install them.

+2


a source to share


3 answers


What you are looking for is the Linux Kernel Modules Programming Guide , specifically on the / proc filesystem , which has well-documented examples of adding new entries.



+6


a source


This month there is an article about it in a French magazine called "The Gnu / Linux Magazine".

I don't have my bookmarks, but the link theses 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

+1


a source


This may be easier to do with sysfs. Sysfs was designed with these kinds of operations in mind and has simple functions for creating directories and virtual files and callbacks for reading and writing these files.

0


a source







All Articles