函数:module_put ( )【转】

简介: 转自:http://book.2cto.com/201307/27049.html 文件包含: #include 函数定义: 函数在内核源码中的位置:linux-2.6.30/kernel/module.c 函数定义格式:void module_put(struct module *module) 函数功能描述: 该函数的功能是将一个特定模块module的引用计数减1 ,这样当一个模块的引用计数因为不为0而不能从内核中卸载时,可以调用此函数一次或多次,实现对模块计数的清零,从而实现模块卸载。

转自:http://book.2cto.com/201307/27049.html

文件包含:

#include <linux/module.h>

函数定义:

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

函数定义格式:void module_put(struct module *module)

函数功能描述:

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

输入参数说明:

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

返回参数说明:

该函数没有返回值。

实例解析:

编写测试文件: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("<0>before calling module_put,\n");
printk("<0>refs of %s is: %d\n",name,module_refcount(fmodule));
module_put(fmodule);   // 调用module_put( )函数
/*调用module_put( )函数之前,输出模块test_moudule的引用计数*/
printk("<0>after calling module_put,\n");
printk("<0>refs of %s is: %d\n",name,module_refcount(fmodule));
}
else
{
printk("<0>find %s failed!",name);
}
return 0;
}

模块退出函数:
void __exit module_put_exit(void)
{
printk("<0>module exit ok!\n");
}

模块加载、退出函数调用:
module_init(module_put_init);
module_exit(module_put_exit);

实例运行结果及分析:

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

 

结果分析:

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

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

最后,再通过“lsmod | head -4 ”命令获取模块“test_module ”的信息,从图2-17 中下方白色部分的显示结果可知,模块“test_module”的引用计数确实变为了0 。

实例程序中调用了函数find_module( ) 和函数module_refcount( )。函数find_module( ) 是根据模块名查找模块并返回查找到的模块,函数module_refcount( )则是用来获得模块被引用的次数。关于这两个函数的详细说明见本章中关于它们的分析。

【作者】 张昺华
【新浪微博】 张昺华--sky
【twitter】 @sky2030_
【facebook】 张昺华 zhangbinghua
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
目录
相关文章
|
5月前
|
JavaScript 前端开发 开发者
ts详解以及相关例子(一篇带你详细了解ts)
ts详解以及相关例子(一篇带你详细了解ts)
42 1
|
10月前
|
存储 JavaScript
【TS】object类型
【TS】object类型
51 0
|
10月前
|
Java 关系型数据库 MySQL
8. 成功解决:Error: Module not specified
使用 IDEA 时,调用 `main` 方法,提示 `Error: Module not specified` 错误,意思是“module 未指定”。
1188 1
|
Linux
函数__module_address()
函数__module_address()
91 0
|
存储 NoSQL MongoDB
es6 map与set
es6 map与set
|
机器人
DefaultRobotHWSim::initSim函数详解
DefaultRobotHWSim::initSim函数详解
DefaultRobotHWSim::initSim函数详解
如何使用代码获得一个function module的Where Used List
如果要获得一个function module的Where Used List,我们通常用的办法是使用ABAP workbench里提供的功能。
110 0
如何使用代码获得一个function module的Where Used List
Caused by: 元素类型为 "package" 的内容必须匹配 "(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,default-class-ref?,global-results?,globa
在Struts-2.3的配置文件struts.xml中,Caused by: 元素类型为 "package" 的内容必须匹配 "(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,default-class-ref?,global-results?,global-exception-mappings?,action*)"。
1282 0