下面說一下在用戶空間調用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的更多相关文章

  1. JavaScript基礎知識

    JavaScript基礎知識 1.標籤組使用 <script charset='utf-8' //設置字元集 defet //使腳本延遲到文檔解析完成,Browser已忽略 language=' ...

  2. BootStrap基礎知識

    BootStrap基礎知識 1. .lead //突出 .text-left //文字居左 .text-right //文字居右 .text-center //文字居中 .text-justify / ...

  3. CSS1-3基礎知識

    CSS1-3基礎知識 1.css排版 css在html內排版: <style type='text/css'> 標記名{} .類型名{} #ID名{} 標記名,.類型名,#ID名{} &l ...

  4. jQuery基礎知識

    jQuery基礎知識 $(function(){}) //jQuery先執行一遍再執行其他函數 $(document).ready(fn) //文檔加載完後觸發 1. 刪除$:jQuery.noCon ...

  5. Python开发 基礎知識 (未完代補)

    一.Python基本知識 1.Python屬高階語言,所編築的是字節碼 2.一般狀態statement 終止於換行,如需使用多數行編寫,可在行末加上 \,以表延續 但在 parentheses ( ) ...

  6. 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 ...

  7. Python开发 基礎知識 3.類別&方法 (bool & str) (未完待續)

    類別 可使用type()查看 內建 [ 布爾:bool (Boolen) 字串:str (String) 數字:int (Integer) 小數:float 列表:list 元祖:tuple 字典:d ...

  8. Python开发 基礎知識 2.變量 ( *arg, **kwargs )

    變量 *args 和 **kwargs ( *和**為本體,名稱為通俗的名稱約定 ) *args 用於函式定義. 可將不定數量的參數傳遞給一個函數,傳入函式的引數,會先以Tuple物件收集,再設定給參 ...

  9. Python 基礎 - 認識模塊

    什麼是模塊?簡單說就是別人寫好了一堆功能,封裝在一起. 模塊有分二種,一個是之前有提到的 標準庫,就是不需要透過額外的安裝就有的模塊 ,另一個叫 第三方庫,需要另外安裝才能使用的模塊 #!/usr/b ...

随机推荐

  1. 试试SQLSERVER2014的内存优化表

    试试SQLSERVER2014的内存优化表 SQL Server 2014中的内存引擎(代号为Hekaton)将OLTP提升到了新的高度. 现在,存储引擎已整合进当前的数据库管理系统,而使用先进内存技 ...

  2. Fis3的前端工程化之路[三大特性篇之声明依赖]

    Fis3版本:v3.4.22 Fis3的三大特性 资源定位:获取任何开发中所使用资源的线上路径 内容嵌入:把一个文件的内容(文本)或者base64编码(图片)嵌入到另一个文件中 依赖声明:在一个文本文 ...

  3. 如何一步一步用DDD设计一个电商网站(二)—— 项目架构

    阅读目录 前言 六边形架构 终于开始建项目了 DDD中的3个臭皮匠 CQRS(Command Query Responsibility Segregation) 结语 一.前言 上一篇我们讲了DDD的 ...

  4. Windows平台分布式架构实践 - 负载均衡

    概述 最近.NET的世界开始闹腾了,微软官方终于加入到了对.NET跨平台的支持,并且在不久的将来,我们在VS里面写的代码可能就可以通过Mono直接在Linux和Mac上运行.那么大家(开发者和企业)为 ...

  5. 如何解决流程开发中SheetRadioButtonList页面取值问题

    分享一个常见的取值问题. 应用场景: SheetRadioButtonList控件,点击其中一项执行事件操作.如果是页面加载的情况下,值就无法取到. 具体原因如下: 我给SheetRadioButto ...

  6. mac好用的markdown编辑器

    在刚开始接触markdown的时候,就被吸引了.此后一直在找贴心的好用的markdown编辑器.印象笔记和马克飞象配合着用也是挺好的,唯一的缺点就是比较封闭,发个笔记的链接给同学,还得注册才能看,导致 ...

  7. 为什么你SQL Server的数据库文件的Date modified没有变化呢?

    在SQL Server数据库中,数据文件与事务日志文件的修改日期(Date Modified)是会变化的,但是有时候你会发现你的数据文件或日志文件的修改日期(Date Modified)几个月甚至是半 ...

  8. OpenGL shader 中关于顶点坐标值的思考

    今天工作中需要做一个事情: 在shader内部做一些空间距离上的计算,而且需要对所有的点进行计算,符合条件的显示,不符合条件的点不显示. 思路很简单,在vertex shader内知道顶点坐标,进行计 ...

  9. POJ1149 PIGS [最大流 建图]

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20662   Accepted: 9435 Description ...

  10. linux压缩和解压缩命令大全

    .tar 解包:tar zxvf FileName.tar 打包:tar czvf FileName.tar DirName ------------------------------------- ...