Linux 内核模块hello world

阅读更多
hello.c:
#include 
#include  
#include 


#define DRIVER_AUTHOR "LeslieChu"
#define DRIVER_DESC "test hello module"
#define VERSION "1.0.0"
#define ALIAS  "HeiMod"
#define LICENSE "GPL"

/*导出模块变量*/
int abc = 3;
int i = 0;
module_param(abc,int,S_IRUGO);

/*定义模块信息,通过modinfo查看*/
MODULE_LICENSE(LICENSE);
MODULE_AUTHOR(DRIVER_AUTHOR );        
MODULE_DESCRIPTION(DRIVER_DESC );    
MODULE_VERSION(VERSION);
MODULE_ALIAS(ALIAS);

/*加载模块*/
static int __init first_init (void)
{
    printk("the first mode insmod\n");
    for(i=0;i 
 

Makefile:

KERNELDIR=/usr/src/linux-headers-3.2.0-30-generic
#/usr/src/linux-headers-$(uname -r)
PWD:=$(shell pwd)
obj-m:=hello.o
default:
	make -C $(KERNELDIR) M=$(PWD) modules
clean:
	rm -rf *.o  .cmd *.ko *.mod.c .tmp_versions *.order  *.sym*

安装 模块:insmod hello.ko
卸载模块: rmmod hello
查看模块安装卸载的log:tail /var/log/kernel.log -n 10

你可能感兴趣的:(linux,c)