现在的位置: 首页 > 综合 > 正文

【linux】——helloworld模块编写与测试

2013年08月07日 ⁄ 综合 ⁄ 共 869字 ⁄ 字号 评论关闭

将RedHat Linux 9.0的内核升级到2.6.18后,就开始学习驱动的编写了。

 在ldd3中,有一个helloworld模块,C程序代码如下:

#include <linux/init.h>
#include <linux/module.h>
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文件内容如下:

# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)

obj-m := hello.o
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else

KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

endif

然后make

执行insmod ./hello.ko或者insmod hello.ko

就将helloworld模块加载,但是在终端上并不会打印出消息(不知道?!?!)

然后用lsmod查看hello模块是否加载好

再执行

rmmod hello.ko

卸载helloworld模块,再用lsmod查看是否卸载hello模块

再执行dmesg |tail -8查看printk的打印消息

抱歉!评论已关闭.