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