带参数的可加载模块

宏MODULE_PARAM(var,type,right) 用于向模块传递命令行参数。参数类型可以是整数、长整型、字符串等类型。

例1.2  带参数的内核模块实例

代码见光盘\src\1drivermodel\1-2module。本实例演示了如何向模块传递整型、长整型、字符串型等参数。核心代码如下所示:

  1. static int itype=0;  
  2. module_param(itype, int, 0);  
  3. static int btype = 0;  
  4. module_param(btype, bool, 0);  
  5. static unsigned char ctype=0;  
  6. module_param(ctype, byte, 0);  
  7. static char *stype=0;  
  8. module_param(stype, charp, 0);  
  9. //模块初始化  
  10. static int __init demo_module_init(void)  
  11. {  
  12.     printk("simple module init\n");  
  13.     printk("itype=%d\n",itype);  
  14.     printk("btype=%d\n",btype);  
  15.     printk("ctype=%d\n",ctype);  
  16.     printk("stype='%s'\n",stype);  
  17.     return 0;  
  18. }  
  19. //模块卸载  
  20. static void __exit demo_module_exit(void)  
  21. {  
  22.     printk("simple module exit\n");  
  23. }  
  24. module_init(demo_module_init);  
  25. module_exit(demo_module_exit);  

接下来编写一个makefile文件,同例1.1。执行make后生成smodule.ko,运行结果如下:
  1. [root@urbetter /home]# insmod  smodule.ko  itype=2 btype=1 ctype=0xAC stype='a' 
  2. simple module init  
  3. itype=2 
  4. btype=1 
  5. ctype=172 
  6. stype='a' 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值