重温Linux Driver基础之Hello World

2014-11-24 08:20:36 · 作者: · 浏览: 2

Linux驱动手动加载 insmod 手动卸载 rmmod


Linux设备驱动第三版:


参考:《linux设备驱动程序》第三版,下载在Linux公社的1号FTP服务器里,下载地址:


密码:www.muu.cc


在 2011年LinuxIDC.com\3月\《linux设备驱动程序》第三版


下载方法见 http://www.linuxidc.net/thread-1187-1-1.html


/*********************************************/


hello.c


#include
#include


MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);


#/*********************************************/


makefile:


obj-m := hello.o


KERNELDIR = /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)


all:
$(MAKE) -C $(KERNELDIR) M=$(PWD)


保存文件,使用make命令编译


#make


#insmod hello.ko


#rmmod hello.ko


在我的测试环境中,并没有直接从终端打出信息,而是在/var/log/messages中出现以下信息。至此编译运行成功。


Mar 27 17:01:30 localhost kernel: Hello,world
Mar 27 17:01:36 localhost kernel: Goodbye,cruel world