Skip to content

Commit 739d206

Browse files
author
chenzufei
committed
添加内核模块编程示例
1 parent 93e580c commit 739d206

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
obj-m += test.o
2+
all:
3+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
4+
clean:
5+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
环境:
2+
Ubuntu 20.04 server
3+
4+
编译命令:
5+
make
6+
7+
加载模块命令:
8+
insmod test.ko
9+
10+
卸载模块命令:
11+
rmmond test.ko
12+
13+
查询模块打印日志:
14+
dmesg
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <linux/init.h>
2+
#include <linux/module.h>
3+
#include <linux/kernel.h>
4+
5+
MODULE_LICENSE("GPL");
6+
MODULE_AUTHOR("test");
7+
MODULE_DESCRIPTION("A simple example Linux module.");
8+
MODULE_VERSION("0.1");
9+
10+
static int __init test_init(void) {
11+
printk(KERN_INFO "Hello, World!\n");
12+
return 0;
13+
}
14+
static void __exit test_exit(void) {
15+
printk(KERN_INFO "Goodbye, World!\n");
16+
}
17+
18+
module_init(test_init);
19+
module_exit(test_exit);

0 commit comments

Comments
 (0)