下面說一下在用戶空間調用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. Tcp/ip 报文解析

    在编写网络程序时,常使用TCP协议.那么一个tcp包到底由哪些东西构成的呢?其实一个TCP包,首先需要通过IP协议承载,而IP报文,又需要通过以太网传送.下面我们来看看几种协议头的构成 一 .Ethe ...

  2. ASP.NET Aries 入门开发教程1:框架下载与运行

    背景: 鉴于框架的使用者越来越多,文档太少,不少用户反映框架的入门门槛太高. 好吧,再辛苦下,抽时间写教程吧! 步骤1:下载框架源码 开源地址:https://github.com/cyq1162/A ...

  3. Xshell 连接CentOS服务器解密

    平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4822808.html Xshell生成密钥key(用于Linux 免密码登录)htt ...

  4. 几个比较”有意思“的JS脚本

    1.获取内网和公网真实IP地址(引用地址) <!DOCTYPE html> <html> <head> <meta http-equiv="Cont ...

  5. 这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)

    在前2篇文章这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧 和这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑)中,大伙热情高涨.再次拿出自己的私货,在.NET平台 ...

  6. [C#] 简单的 Helper 封装 -- RandomHelper

    using System; namespace Wen.Helpers { /// <summary> /// 随机数助手 /// </summary> public seal ...

  7. C#通过NPOI操作Excel

    参考页面: http://www.yuanjiaocheng.net/webapi/create-crud-api-1-post.html http://www.yuanjiaocheng.net/w ...

  8. java常用的设计模式

    设计模式:一个程序员对设计模式的理解:"不懂"为什么要把很简单的东西搞得那么复杂.后来随着软件开发经验的增加才开始明白我所看到的"复杂"恰恰就是设计模式的精髓所 ...

  9. AlloyTouch实战--60行代码搞定QQ看点资料卡

    原文链接:https://github.com/AlloyTeam/AlloyTouch/wiki/kandian 先验货 访问DEMO你也可以点击这里 源代码可以点击这里 如你体验所见,流程的滚动的 ...

  10. Android SDK 与API版本对应关系

    Android SDK版本号 与 API Level 对应关系如下表: Code name Version API level   (no code name) 1.0 API level 1   ( ...