您的位置:首页 > 运维架构 > Linux

linux kernel 学习 ----构造和运行模块

2014-05-19 00:13 141 查看
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                       

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: