Linux基礎知識 —— open&close
下面說一下在用戶空間調用open/close/dup跟驅動中的open和release的對應。
下面是測試驅動:
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/fs.h> static int misc_demo_open(struct inode *nodp, struct file *filp)
{
printk("%s enter, nodp: %p, filp: %p.\n", __func__, nodp, filp); return ;
} static int misc_demo_release(struct inode *nodp, struct file *filp)
{
printk("%s enter, nodp: %p, filp: %p.\n", __func__, nodp, filp); return ;
} static struct file_operations misc_demo_fops = {
.owner = THIS_MODULE,
.open = misc_demo_open,
.release = misc_demo_release,
}; static struct miscdevice misc_demo_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "misc_demo",
.fops = &misc_demo_fops
}; static __init int misc_demo_init(void)
{
int ret; ret = misc_register(&misc_demo_dev); return ret;
} static __exit void misc_demo_exit(void)
{
misc_deregister(&misc_demo_dev); return;
} module_init(misc_demo_init);
module_exit(misc_demo_exit);
MODULE_LICENSE("GPL");
下面是用戶空間測試代碼:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> int main(int argc, const char *argv[])
{
int fd[], fd2[], i; printf("Begin open.\n");
for (i=; i<; i++) {
fd[i] = open("/dev/misc_demo", O_RDONLY);
printf("open: %d\n", fd[i]);
fd2[i] = dup(fd[i]);
printf("dup: %d\n", fd2[i]);
sleep();
} sleep(); printf("Begin close.\n");
for (i=; i<; i++) {
printf("close: %d\n", fd[i]);
close(fd[i]);
sleep();
} sleep(); printf("Begin close dup.\n");
for (i=; i<; i++) {
printf("close dup: %d\n", fd2[i]);
close(fd2[i]);
sleep();
} return ;
}
下面是輸出的log:
Begin open.
[ 4628.805135] misc_demo_open enter, nodp: c3b88a18, filp: c3859060.
open:
dup:
[ 4629.809860] misc_demo_open enter, nodp: c3b88a18, filp: c3859c40.
open:
dup:
[ 4630.814891] misc_demo_open enter, nodp: c3b88a18, filp: c3859ec0.
open:
dup: Begin close.
close:
close:
close: 7 Begin close dup.
close dup:
[ 4641.845172] misc_demo_release enter, nodp: c3b88a18, filp: c3859060.
close dup:
[ 4642.850183] misc_demo_release enter, nodp: c3b88a18, filp: c3859c40.
close dup:
[ 4643.855123] misc_demo_release enter, nodp: c3b88a18, filp: c3859ec0.
通過分析log,我們得出結論, 用戶空間每調用一次open,驅動中的open都會被執行一次,而在調用dup的時候,只是將struct file的引用計數加1,而沒有產生新的struct file,所以返回的新的fd跟老的fd對應的是同一個struct file,同時也沒用調用open。在close的時候,只有struct file對應的所有fd都被關閉或者說struct file的引用計數爲0的時候,驅動中的release纔會被執行。
此外,如果將同時執行多個test程序,會發現,inode的地址都相同,說明每個文件只有一個inode與之對應。
完。
Linux基礎知識 —— open&close的更多相关文章
- JavaScript基礎知識
JavaScript基礎知識 1.標籤組使用 <script charset='utf-8' //設置字元集 defet //使腳本延遲到文檔解析完成,Browser已忽略 language=' ...
- BootStrap基礎知識
BootStrap基礎知識 1. .lead //突出 .text-left //文字居左 .text-right //文字居右 .text-center //文字居中 .text-justify / ...
- CSS1-3基礎知識
CSS1-3基礎知識 1.css排版 css在html內排版: <style type='text/css'> 標記名{} .類型名{} #ID名{} 標記名,.類型名,#ID名{} &l ...
- jQuery基礎知識
jQuery基礎知識 $(function(){}) //jQuery先執行一遍再執行其他函數 $(document).ready(fn) //文檔加載完後觸發 1. 刪除$:jQuery.noCon ...
- Python开发 基礎知識 (未完代補)
一.Python基本知識 1.Python屬高階語言,所編築的是字節碼 2.一般狀態statement 終止於換行,如需使用多數行編寫,可在行末加上 \,以表延續 但在 parentheses ( ) ...
- HTML 4.01+5基礎知識
HTML 4.01+5 1.Html結構:html>head+body 2.Html快捷鍵:!加Tab(在sublime中) 3.雙標籤: ①常用標籤 h1.h2.h3.h4.h5.h6 p.c ...
- Python开发 基礎知識 3.類別&方法 (bool & str) (未完待續)
類別 可使用type()查看 內建 [ 布爾:bool (Boolen) 字串:str (String) 數字:int (Integer) 小數:float 列表:list 元祖:tuple 字典:d ...
- Python开发 基礎知識 2.變量 ( *arg, **kwargs )
變量 *args 和 **kwargs ( *和**為本體,名稱為通俗的名稱約定 ) *args 用於函式定義. 可將不定數量的參數傳遞給一個函數,傳入函式的引數,會先以Tuple物件收集,再設定給參 ...
- Python 基礎 - 認識模塊
什麼是模塊?簡單說就是別人寫好了一堆功能,封裝在一起. 模塊有分二種,一個是之前有提到的 標準庫,就是不需要透過額外的安裝就有的模塊 ,另一個叫 第三方庫,需要另外安裝才能使用的模塊 #!/usr/b ...
随机推荐
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
- 我是如何在SQLServer中处理每天四亿三千万记录的
首先声明,我只是个程序员,不是专业的DBA,以下这篇文章是从一个问题的解决过程去写的,而不是一开始就给大家一个正确的结果,如果文中有不对的地方,请各位数据库大牛给予指正,以便我能够更好的处理此次业务. ...
- Ubuntu 16.10 开启PHP错误提示
两个步骤: 修改php.ini配置文件中的error_reporting 和 display_errors两地方内容: sudo vim /etc/php/7.0/apache2/php.ini er ...
- 构建通用的 React 和 Node 应用
这是一篇非常优秀的 React 教程,这篇文章对 React 组件.React Router 以及 Node 做了很好的梳理.我是 9 月份读的该文章,当时跟着教程做了一遍,收获很大.但是由于时间原因 ...
- 如何安全的将VMware vCenter Server使用的SQL Server Express数据库平滑升级到完整版
背景: 由于建设初期使用的vSphere vCenter for Windows版,其中安装自动化过程中会使用SQL Server Express的免费版数据库进行基础环境构建.而此时随着业务量的增加 ...
- Oracle 数据库知识汇总篇
Oracle 数据库知识汇总篇(更新中..) 1.安装部署篇 2.管理维护篇 3.数据迁移篇 4.故障处理篇 5.性能调优篇 6.SQL PL/SQL篇 7.考试认证篇 8.原理体系篇 9.架构设计篇 ...
- 【转】java通用URL接口地址调用方式GET和POST方式
java通用URL接口地址调用方式GET和POST方式,包括建立请求和设置请求头部信息等等......... import java.io.ByteArrayOutputStream; import ...
- SqlServer简单数据分页
手边开发的后端项目一直以来都用的.NET MVC框架,访问数据库使用其自带的EF CodeFirst模式,写存储过程的能力都快退化了 闲来无事,自己写了条分页存储过程,网上类似的文章多的是,这里只列了 ...
- ASP.NET Aries JSAPI 文档说明:AR.DataGrid
AR.DataGrid 文档 用法: <body> <table id="dg"></table> </body> </htm ...
- xamarin绑定原生库的一些坑
最近一个项目涉及到较多的第三方库的绑定技术,中间遇到了几个坑,记录下来与大家分享 绑定Jar库 monoandroid对原生库的调用都通过Android.Runtime.JNIEnv进行调入(http ...