带参数的可加载模块

来源:互联网 发布:自学数学软件 编辑:程序博客网 时间:2024/04/29 22:40

宏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' 
0 0
原创粉丝点击