A simple Hello Wrold Module
Source Code:
- #include <linux/init.h>
- #include <linux/module.h>
- MODULE_LICENSE("GPL"); /*这行用于告诉内核该模块拥有free license,在2.6中这是必须的*/
- /*执行真正的初始化工作*/
- static int hello_init(void)
- {
- printk(KERN_ALERT "Hello, world\n");
- return 0;
- }
- /*执行真正的析构工作*/
- static void hello_exit(void) {
- printk(KERN_ALERT "Goodbye, cruel world\n");
- }
- /*该函数注册模块的构造函数*/
- module_init(hello_init);
- /*该函数注册模块的析构函数*/
- module_exit(hello_exit);
Makefile:
- obj-m += hello.o
- KERNEL_VERSION = /lib/modules/$(shell uname -r)/build/
- #PWD := $(shell pwd)
- all:
- make -C $(KERNEL_VERSION) M=$(PWD) modules
- clean:
- make -C $(KERNEL_VERSION) M=$(PWD) clean
Directory
/"your_dir"/helloworld/hello.c
/"your_dir"/helloworld/Makefile
Command
install module:
insmod hello.ko
cat /var/log/message
rmmod hello.ko
cat /var/log/message
finished.
阅读(962) | 评论(1) | 转发(0) |