内核允许对驱动程序指定参数,而这些参数可在装载驱动程序模块时改变。
以下是我的实验程序:
- #include<linux/init.h>
-
#include<linux/module.h>
-
#include<linux/moduleparam.h>
-
-
static char *whom = "guixiao198881";
-
static int howmany = 1;
-
static int guixiao[] = {1,2,3,4};
-
static int guixiao_n = 4;
-
module_param(howmany,int,S_IRUGO);
-
module_param(whom,charp,S_IRUGO);
-
module_param_array(guixiao,int,&guixiao_n,S_IRUGO);
-
-
static int __init hello_init(void)
-
{
-
int i;
-
for (i = 0; i < howmany; i++)
-
printk(KERN_ALERT "(%d) Hello, %s !\n", i, whom);
-
for (i = 0; i < 4; i++)
-
printk(KERN_ALERT "guixiao[%d] : %d \n", i, guixiao[i]);
-
return 0;
-
}
-
-
static void __exit hello_exit(void)
-
{
-
printk(KERN_ALERT "Goodbye, guixiao198881 !\n");
-
}
-
-
-
MODULE_LICENSE("Dual BSD/GPL");
-
module_init(hello_init);
-
module_exit(hello_exit);
编译后,在安装时候,加参数和不加参数的区别。
如果不加参数:
insmod hello.ko
- [10231.944033] (0) Hello, guixiao198881 !
-
[10231.944044] guixiao[0] : 1
-
[10231.944051] guixiao[1] : 2
-
[10231.944056] guixiao[2] : 3
-
[10231.944062] guixiao[3] : 4
加入参数以后:
insmod hello.ko whom="ljj" howmany=3 guixiao=4,3,2,1
- [10292.789489] (0) Hello, ljj !
-
[10292.789497] (1) Hello, ljj !
-
[10292.789502] (2) Hello, ljj !
-
[10292.789507] guixiao[0] : 4
-
[10292.789511] guixiao[1] : 3
-
[10292.789515] guixiao[2] : 2
-
[10292.789519] guixiao[3] : 1
阅读(2173) | 评论(0) | 转发(0) |