内核hello模块加载和卸载例子

Khello.c

#include<linux/module.h>

#include<linux/init.h>

#include<linux/kernel.h>

static int __init hello_init()

{

printk("hello world!\n");

return 0;

}

static void __exit hello_exit()

{

printk("see you next time\n");

}

module_init(hello_init);

module_exit(hello_exit);

Makefile:

 

ifneq ($(KERNELRELEASE),)

obj-m := khello.o

else

KDIR := /lib/modules/$(shell uname -r)/build

PWD := $(shell pwd)

default:

$(MAKE) -C $(KDIR) M=$(PWD) modules

endif

编译出来的东西

模块只能在超级用户模式下加载:

加载命令insmod 或者modprobe

卸载命令rmmod

通过dmesg查看加载和卸载内核模块的打印信息

static int __init hello_init()

{

printk("hello world!\n");

return 0;

}

static void __exit hello_exit()

{

printk("see you next time\n");

}

module_init(hello_init);

module_exit(hello_exit);

多次加载和卸载khello.ko模块产生的打印信息。

你可能感兴趣的:(内核模块)