Linux最简单的驱动模型:hello world!


1.文件hello.c内容如下:
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif

#include /*printk*/
#include
#include /*module_init module_exit*/

static int __init hello_init(void)
{
printk("hello world,my god!\n");
return 0;
}

static void __exit hello_exit(void)
{
printk("bye bye,my god!\n");
}

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

2.编译驱动
2.1硬件环境介绍:
2.1.1>vmware虚拟机中RedHat9[linux-2.4.20-8]
2.1.2>putty.exe远程ssh登陆vmware上的linux
2.1.3>windows xp下使用winscp.exe与vmware中的linux远程小文件拷贝
2.1.4>vmware->文件->设置->选项->共享进行大文件共享[/mnt/hgfs/][gliethttp]
2.2准备:
因为我所使用linux测试系统为linux-2.4.20-8,所以先确保/usr/src/linux-2.4.20-8的存在
如果不存在,下载linux-2.4.20-8内核源码,放到任何一个目录,姑且把下载的linux-2.4.20-8内核源码,放在
/usr/src目录下,准备工作已经完成.
2.3编译
[root@gliethttp]# gcc -c hello.c -I /usr/src/linux-2.4.20-8/include
成功之后在hello.c同目录下会生成hello.o
2.4执行
[root@gliethttp]# insmod hello.o
hello world,my //屏幕显式
[root@gliethttp]# lsmod //查看hello驱动单元
[root@gliethttp]# rmmod hello //从系统移除
bye bye,my //屏幕显式
注意:如果在putty中执行,putty中不会显式结果,vmware的tty中才显式[gliethttp]

相关内容