Linux 驱动开发庖丁解牛之二
——模块编程
dreamice
e-mail:dreamice.jiang@gmail.com
本文是建立在前面的开发环境已经成功建立的基础之上的。如果没有建立好,请参照
《Linux 驱动开发庖丁解牛之一——开发环境的建立》。
已经有很多文档讲述模块编程,个人觉得《The Linux kernel module programming guide》
是最详尽的。本文不再立足于从理论上去阐述模块编程的相关知识,而着重从实践的基础上
去掌握模块编程,领悟模块编程的实质。当然,具备足够的理论知识才能从实践出发,所以,
本文档尽量配合《Linux Device Driver》第三版的第二章,以及讲述模块编程最完善的文档
《The Linux kernel module programming guide》。下面,我们从实践开始出发吧。
1. 人之初(hello world)
/*
* hello.c - The first kernel module programming
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_ALERT */
#include <linux/init.h>
MODULE_LICENCE(“Dual BSD/GPL”);
static int hello_init(void)
{
printk(KERN_INFO "Hello world");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
static void hello_exit(void)
{
printk(KERN_INFO "Goodbye world");
}
module_init(hello_init);