writing Linux kernel modules to create a new device file and an entry in the proc file
Question:
writing Linux kernel modules to create a new device file and an entry in the proc file system.
The objectives of this project is to learn:
1. How to write a helloworld Linux kernel module.
Create a Helloworld kernel module
The following code is a complete helloworld module. #include
#include
#include
int init_new_module ( void)
{
p ri n t k (KERN_INFO " Hell o , world ! n" ) ;
return 0 ;
}
void exit_new_module ( void)
{
p ri n t k (KERN_INFO "Goodbye , world ! n" ) ;
}
module_init ( init_new_module ) ;
module_exit ( exit_new_module ) ;
***The module defines two functions. init_module is invoked when the module is loaded into the kernel and cleanup_module is called when the module is removed from the kernel. module_init and module_exit are special kernel macros to indicate the role of these two functions. Use the following makefile to compile the module.
ob j−m += new_module . o
a l l :
sudo make −C / l i b /modules /$ ( s h e l l uname −r ) / b uil d M=$ (PWD) modules
cl e a n :
sudo make −C / l i b /modules /$ ( s h e l l uname −r ) / b uil d M=$ (PWD) cl e a n
***To insert the module into the Linux kernel:
# insmod new_module.ko
Use the following command to verify the module has been loaded:
# lsmod
To remove the module from the kernel:
Systems analysis and design
ISBN: 978-0136089162
8th Edition
Authors: kenneth e. kendall, julie e. kendall