下图反映了应用程序、linux内核、驱动程序、硬件的关系。

Folder:

  • /dev: 设备

模块 module:

  • 编译出的内核本身并不含有所有功能,而在这些功能需要被使用的时候,其对应的代码可以被动态的加载到内核中

    • advantage: improve compilation efficiency
  • 模块本身并不被编译入内核,从而控制了内核的大小。

  • 模块一旦被加载,他就和内核中的其他部分完全一样。

  • module

    1)以下是一个最简单的模块例子
    static int  __init  hello_init(void)      
    /*模块加载函数,通过insmod命令加载模块时,被自动执行*/  
    /* Linux内核模块一般以__init标示声明*/
    {  
    printk(KERN_INFO " Hello World enter\n");  
    return 0;  
    }  
    static void  __exit  hello_exit(void)    /*模块卸载函数,当通过rmmod命令卸载时,会被自动执行*/  
    {  
    printk(KERN_INFO " Hello World exit\n ");  
    }  
    /*模块加载函数的名字可以随便取,但必须以“module_init(函数名)”的形式被指定;*/
    module_init(hello_init);  
    /*模块卸载函数在模块卸载的时候执行,不返回任何值,需用”module_exit(函数名)”的形式被指定。*/
    module_exit(hello_exit);  
    MODULE_AUTHOR("dengwei");           
    /*模块作者,可选*/  
    MODULE_LICENSE("Dual BSD/GPL");     
    /*模块许可证明,描述内核模块的许可权限,必须*/  
    MODULE_DESCRIPTION("A simple Hello World Module"); 
    /*模块说明,可选*/  
    MODULE_ALIAS("a simplest module");                 
    /*模块说明,可选*/
    <span style="font-family:SimSun;font-size:18px;color:#FF0000;"><strong>
    
  • 常用的几种模块操作:

    • insmod XXX.ko 加载指定模块
    • lsmod 列举当前系统中的所有模块
    • rmmod XXX 卸载指定模块(注意没有.ko后缀)
    • dmesg 当打印等级低于默认输出等级时,采用此命令查看系统日志
  • module_param(参数名、参数类型、参数读写属性) 为模块定义一个参数

    • module_param (1000,int,S_IRUGO);
    • Example

      static char *string_test="default paramater";  
      static int num_test=1000;  
      static int __init hello_init(void)  
      {  
        PRINTK("\nthe  string_test is : %s\n",string_test);  
        PRINTK("the  num_test is : %d\n",num_test);  
        return 0;  
      }  
      static void __exit hello_exit(void)  
      {  
        PRINTK(" input paramater module exit\n ");  
      }  
      module_init(hello_init);  
      module_exit(hello_exit);  
      module_param(num_test,int,S_IRUGO);  
      module_param(string_test,charp,S_IRUGO);
      

      当执行insmod hello_param.ko num_test=2000 string_test=“edit by dengwei”,执行dmesg查看内核输出信息:

        the test string is: edit by dengwei  
        the test num is :2000
      

results matching ""

    No results matching ""