下面說一下在用戶空間調用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. 加深一下BlockingQueue的认识

    认识BlockingQueue BlockingQueue是一种可以阻塞线程的队列,java中对这种队列提供了方法抽象,BlockingQueue则是抽象的接口. add:添加元素到队列里,添加成功返 ...

  2. 使用 Nodejs 搭建简单的Web服务器

    使用Nodejs搭建Web服务器是学习Node.js比较全面的入门教程,因为要完成一个简单的Web服务器,你需要学习Nodejs中几个比较重要的模块,比如:http协议模块.文件系统.url解析模块. ...

  3. web前端基础知识

    #HTML    什么是HTML,和他ML...    网页可以比作一个装修好了的,可以娶媳妇的房子.    房子分为:毛坯房,精装修    毛坯房的修建: 砖,瓦,水泥,石头,石子....    精 ...

  4. 了不起的 nodejs-TwitterWeb 案例 bug 解决

    了不起的nodejs算是一本不错的入门书,不过书中个别案例存在bug,按照书中源码无法做出和书中相同效果,原本兴奋的心情掺杂着些许失落. 现在我们看一下第七章HTTP,一个Twitter Web客户端 ...

  5. 流程开发Activiti 与SpringMVC整合实例

    流程(Activiti) 流程是完成一系列有序动作的概述.每一个节点动作的结果将对后面的具体操作步骤产生影响.信息化系统中流程的功能完全等同于纸上办公的层级审批,尤其在oa系统中各类电子流提现较为明显 ...

  6. Android Studio —— 重装 HAXM

    Android Studio -- 重装 HAXM 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. Android SDK 自带模拟器一直以慢.卡 ...

  7. await and async

    Most people have already heard about the new “async” and “await” functionality coming in Visual Stud ...

  8. 漫谈TCP

    不得不承认,tcp是一个非常复杂的协议.它包含了RFC793及之后的一些协议.能把tcp的所有方面面面具到地说清楚,本身就是个很复杂的事情.如果再讲得枯燥,那么就会更让人昏昏欲睡了.本文希望能尽量用稍 ...

  9. Boost信号/槽signals2

    信号槽是Qt框架中一个重要的部分,主要用来解耦一组互相协作的类,使用起来非常方便.项目中有同事引入了第三方的信号槽机制,其实Boost本身就有信号/槽,而且Boost的模块相对来说更稳定. signa ...

  10. Android Studio开发RecyclerView遇到的各种问题以及解决(一)

    以前一直在用ListView,,,最近才看RecyclerView发现好强大.RecyclerView前提是Android版本在5.0以上,本人以前用的是eclipse只支持到4.4.索性就安装一个A ...