学习内核模块编程,第一个小程序当然是hello,kernel!了.
1.首先编写hello.c文件
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
static int hello_init(void)
{
printk(KERN_ALERT "hello,I am fine.\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "goodbye,kernel\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("mcu");
MODULE

这篇博客介绍了如何编写和测试Linux内核驱动模块,以hello.ko为例。首先,讲解了hello.c文件的编写,包括模块加载和卸载函数,以及module_init()的使用。接着,详细阐述了Makefile的编写,确保模块能正确编译,并讨论了其移植性和可读性。通过make命令生成hello.ko后,使用insmod加载,rmmod卸载,并通过dmesg查看模块加载信息。整个过程在Ubuntu系统上进行了验证。
最低0.47元/天 解锁文章
270

被折叠的 条评论
为什么被折叠?



