Linux内核API module_put

module_put函数功能描述:该函数的功能是将一个特定模块module的引用计数减一,这样当一个模块的引用计数因为不为0而不能从内核中卸载时,可以调用此函数一次或多次,实现对模块计数的清零,从而实现模块卸载。

module_put文件包含

#include <linux/module.h>

module_put函数定义

在内核源码中的位置:linux-3.19.3/kernel/module.c

函数定义格式:

void module_put(struct module *module)

module_put输入参数说明

module:指向模块结构体的指针,结构体中包含模块的名称、状态、所属的模块链表等。关于结构体struct module的定义,请参见find_module()函数的分析。

module_put返回参数说明

该函数没有返回值。

module_put实例解析

编写测试文件:module_put.c

头文件及全局变量声明如下:

#include <linux/module.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
static int __init module_put_init(void);
static void __exit module_put_exit(void);

模块初始化函数:

int __init module_put_init(void)
{
    const char *name = "test_module";   //定义待查找的模块名为“test_module”
    struct module * fmodule = find_module( name );         //调用查找模块函数

    if( fmodule ! = NULL )
    {
        /*调用module_put( )函数之前,输出模块test_moudule的引用计数*/
        printk("before calling module_put, \n");
        printk("refs of %s is: %d\n", name, module_refcount(fmodule));
        module_put(fmodule);                                //调用module_put( )函数

        /*调用module_put( )函数之后,输出模块test_moudule的引用计数*/
        printk("after calling module_put, \n");
        printk("refs of %s is: %d\n", name, module_refcount(fmodule));
    }
    else
    {
        printk("find %s failed! ", name);
    }
    return 0;
}

C

模块退出函数:

void __exit module_put_exit(void)
{
    printk("module exit ok! \n");
}

模块初始化及退出函数调用:

module_init(module_put_init);
module_exit(module_put_exit);

实例运行结果及分析:

首先执行命令lsmod | head -4,然后编译模块,执行命令insmod module_put.ko插入模块,再执行命令dmesg -c,会出现如图所示的结果。
Linux内核API module_put

结果分析:

在该测试程序中,首先通过“lsmod | head -4”命令获取一些模块的信息,这里主要关注模块“test_module”,该模块是笔者动态插入的模块。上图中可以看到模块“test_module”的引用计数为4。

然后测试module_put()函数的功能。首先调用find_module()内核函数查找名为“test_module”的模块,查找模块返回不为空后,再调用module_put()函数实现对模块“test_module”的引用计数减一。上图中为运行结果,从中可以看到,在调用module_put()之前,引用计数为4,调用module_put()之后,模块“test_module”的引用计数因减一而变为3。

最后,再通过“lsmod | head -4”命令获取模块“test_module”的信息,从上图中的显示结果可知,模块“test_module”的引用计数确实变为3了。

实例程序中调用了函数find_module()和函数module_refcount(),函数find_module()是根据模块名查找模块并返回查找到的模块,函数module_refcount()则是用来获得模块被引用的次数。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程