码迷,mamicode.com
首页 > 其他好文 > 详细

HelloWorld 模块

时间:2014-10-22 12:33:04      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   sp   div   on   log   

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 代码

 1 obj-m := helloworld.o
 2 
 3 CURRENT_DIR :=$(shell pwd)
 4 
 5 KERNEL_DIR := /usr/src/linux-headers-$(shell uname -r)
 6 
 7 all:
 8         $(MAKE) -C $(KERNEL_DIR) M=$(CURRENT_DIR) modules
 9 
10 clean:
11         rm -rf %.o

执行make

1 ryan@Ryan-pc:/data1/Ryan/demo/helloworld$ make 
2 make -C /usr/src/linux-headers-3.13.0-32-generic  M=/data1/Ryan/demo/helloworld modules
3 make[1]: Entering directory `/usr/src/linux-headers-3.13.0-32-generic4   Building modules, stage 2.
5   MODPOST 1 modules
6 make[1]: Leaving directory `/usr/src/linux-headers-3.13.0-32-generic

加载模块

1 ryan@Ryan-pc:/data1/Ryan/demo/helloworld$ sudo insmod helloworld.ko 
2 ryan@Ryan-pc:/data1/Ryan/demo/helloworld$ 

dmesg查看

1 ryan@Ryan-pc:/data1/Ryan/demo/helloworld$ demsg
 1 [178401.813566] sr 0:0:0:0: [sr0] Device not ready
 2 [178401.813570] sr 0:0:0:0: [sr0]  
 3 [178401.813571] Result: hostbyte=DID_OK driverbyte=DRIVER_SENSE
 4 [178401.813572] sr 0:0:0:0: [sr0]  
 5 [178401.813573] Sense Key : Not Ready [current] 
 6 [178401.813575] sr 0:0:0:0: [sr0]  
 7 [178401.813577] Add. Sense: Medium not present - tray closed
 8 [178401.813579] sr 0:0:0:0: [sr0] CDB: 
 9 [178401.813580] Read(10): 28 00 00 00 02 2f 00 00 01 00
10 [178401.813584] end_request: I/O error, dev sr0, sector 2236
11 [179614.794066] helloworld: module verification failed: signature and/or  required key missing - tainting kernel
12 [179614.794552] Hello world
13 [180458.268623] goodbye,cruel world
14 [180514.941519] Hello world
15 [180549.136795] goodbye,cruel world
16 [181244.193514] Hello world

卸载模块

1 ryan@Ryan-pc:/data1/Ryan/demo/helloworld$ sudo rmmod helloworld 
2 ryan@Ryan-pc:/data1/Ryan/demo/helloworld$ 

 

HelloWorld 模块

标签:style   blog   color   io   os   sp   div   on   log   

原文地址:http://www.cnblogs.com/main-xlg/p/4042667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!