这个项目就是要编写一个最基本的模块,加载模块,观察结果。从而掌握模块的基本要素及加载、卸载、查看工具的使用。
主机:Ubuntu 10.10。
编写程序:
/*
* hello.c
*
* Simple hello world 2.6 driver module
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE ("GPL");
int __init init_module (void)
{
printk (KERN_INFO "Hello worldn");
return 0;
}
void __exit cleanup_module (void)
{
printk (KERN_INFO "Goodbye worldn");
}
然后:
1、将文件夹ex1-hello-world复制到linux环境中,如:/home/linux/test
$su root
$cp ex1-hello-world /home/linux/test –a
2、$cd /home/linux/test/ex1-hello-world
3、$make
4、通过insmod命令将模块加入内核
$insmod insmod hello.ko
5、通过lsmod查看内核模块
$ lsmod | grep hello
6、通过rmmod删除内核中的模块
$ rmmod hello
7、查看源码,适当修改,掌握实验内容