linux内核简单例程


原文入口-):》》》》》 (O=O=O)
1
/*
2 * hello.c – 本例取自《Linux Kernel Development 3rd》,稍作修改
3 */
4 //#include <linux/init.h> //早期的Linux内核源码存在这个头文件,2.6已经不存在了
5 #include <linux/module.h>
6 #include <linux/kernel.h>
7 static int hello_init(void)
8 {
9 printk(KERN_ALERT “I bear a charmed life.\n”);
10 return 0;
11 }
12 /*
13 * hello_exit – the exit function, called when the module is removed.
14 */
15 static void hello_exit(void)
16 {
17 printk(KERN_ALERT “Out, out, brief candle!\n”);
18 }
19 module_init(hello_init);
20 module_exit(hello_exit);
21 MODULE_LICENSE(“GPL”);
22 MODULE_AUTHOR(“Your name here”);
23 MODULE_DESCRIPTION(“A Hello, World Module”);


相应的Makefile

obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean



在终端载入编译好的模块:

insmod hello.ko

卸载模块:

rmmod hello.ko

代码中调用了printk,但是它并不会输出至终端的屏幕,而是会输出到/var/log/kern.log当中。


posted @ 2011-10-05 22:19  Matrix_  阅读(614)  评论(0编辑  收藏  举报