Chinaunix首页 | 论坛 | 博客
  • 博客访问: 112221
  • 博文数量: 17
  • 博客积分: 351
  • 博客等级: 一等列兵
  • 技术积分: 200
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-27 11:00
文章分类

全部博文(17)

文章存档

2012年(4)

2011年(13)

我的朋友

分类: LINUX

2011-12-08 16:32:54

在用户态下编程可以通过main()的来传递命令行参数,而编写一个内核模块则通过module_param()

module_param宏是Linux 2.6内核中新增的,该宏被定义在include/linux/moduleparam.h文件中,具体定义如下:

#define module_param(name, type, perm)                
    module_param_named(name, name, type, perm)

其中使用了 3 个参数:要传递的参数变量名, 变量的数据类型, 以及访问参数的权限。


module_param_array(name,type,num,perm);
这里 name 是你的数组的名子(也是参数名), type 是数组元素的类型, num 是一个整型变量, perm 是通常的权限值. 如果数组参数在加载时设置, num 被设置成提供的数的个数. 模块加载者拒绝比数组能放下的多的值.


测试模块,源程序hello.c内容如下:
#include #include
#include         

MODULE_LICENSE("Dual BSD/GPL");    

static char *who= "world";            
static int times = 1;       
module_param(times,int,S_IRUSR);    
module_param(who,charp,S_IRUSR);  

static int hello_init(void)      
{
    int i;
    for(i=0;i       printk(KERN_ALERT "(%d) hello, %s!\n",i,who);
     return 0;
}

static void hello_exit(void)
{
    printk(KERN_ALERT"Goodbye, %s!\n",who);
}

module_init(hello_init);
module_exit(hello_exit);

编译生成可执行文件hello

插入:

 # insmod hello who="world" times=5
出现5次"hello,world!":
 #(1)hello,world!
#(2)hello,world!

#(3)hello,world!

#(4)hello,world!
 #(5)hello,world!
卸载: # rmmod hello
出现:
#Goodbye,world!
原文地址:http://hi.baidu.com/operationsystem/blog/item/c150423c1d7e7ce83c6d97ff.html
阅读(2909) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~