在这里小编简单介绍一下Linux内核模块的设计
首先,我们要知道什么是内核模块
内核模块具有如下特点:
1.模块本身并不被编译内核文件(zImage或者bzImage)
2.可以根据需求,在内核运行期间动态的安装或卸载
安装与卸载
安装 insmod
例:insmod /home/dnw_usb
卸载 rmmod
例:rmmod dnw_usb
查看 lsmod
例: lsmod
Linux内核模块设计
三要素:
1.头文件-linux/init.h linux/module.h
2.加载函数-module_init
3.卸载函数-module_exit
简单编写一个内核模块,来证明所需要的三要素
在Linux终端上创建 # vi helloworld.c
#include<linux/init.h>
#include<linux/exit.h>
static int hello_init()
{
printk(KERN_WARING"hello world!\n");
return 0;
}
static int hello_exit()
{
printk(KERN_WARING""hello world!\n);
}
module_init(hello_init);
module_exit(hello_eixt);
编写Makefile
obj-m := hellowold.o
KDIR := /home/boss/linux-2.6.28.6 #这个是开发板内核代码的路径
all:
make -C $(KDIR) modules CROSS_COMPILE=arm-linux- ARCH=arm
clean:
rm -f *.o *.ko *.order *.symvers