#include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> static int __init hello_init(void) { printk(KERN_INFO "Hello world\n"); return 0; } static void __exit hello_exit(void) { printk(KERN_INFO "Goodbye world\n"); } module_init(hello_init); module_exit(hello_exit);
•
static int __init hello_init(void)
•
static void __exit hello_exit(void)
–
Static
声明
,因为这种函数在特定文件之外没有其它意义
–
__
init
标记
,
该函数只在初始化期间使用。模块装载后,将该函数占
用的内存空间释放
–
__exit
标记
该代码仅用于模块卸载。
•
Init/exit
–
宏:
module_init/module_exit
–
声明模块初始化及清除函数所在的位置
–
装载和卸载模块时,内核可以自动找到相应的函数
module_init(hello_init);
module_exit(hello_exit);
•
Makefile
文件
obj-m := hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
•
Module includes more files
obj-m:=hello.o
hello-objs := a.o b.o
•
相关命令
–
lsmod
–
insmod hello.ko
–
rmmod hello.ko