1第一个模块----hello world!
#include <linux/init.h>
#include <linux/module.h>
static int hello_init(void)
{
printk(KERNEL_ALERT“hello,world\n”);
return 0;
}
static int hello_exit(void)
{
printk(KERNEL_ALERT"BYE BYE!");
return 0;
}
module_init(hello_init); //模块装载到内核时被调用
module_exit(hello_exit); //模块从内核卸载时被调用
2Makefile实例
ifneq ($(KERNELRELEASE),)
obj-m := hello.o //说明有一个模块需要从hello.中构造,而从该目标文件中构造的模块名称为hello.ko
else
KERNELDIR ?=/lib/modules/$(shell uname -r)/build //对应内核的源代码树
PWD :=$(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
@echo “success!”
endif
3、装载和卸载模块
insmod hello.ko
rm hello or rm hello.ko
显示内核模块
lsmod
ls /proc/module
本文介绍了如何在Linux环境中创建一个简单的内核模块,即"Hello World"模块。通过示例代码展示了`module_init`和`module_exit`宏的使用,以及如何编写Makefile来构建模块。同时,还讲解了如何装载(`insmod`)和卸载(`rmmod`)模块,并使用`lsmod`和`ls /proc/modules`命令查看内核模块的状态。

被折叠的 条评论
为什么被折叠?



