zoukankan      html  css  js  c++  java
  • Linux基礎知識 —— open&close

    下面說一下在用戶空間調用open/close/dup跟驅動中的open和release的對應。

    下面是測試驅動:

     1 #include <linux/module.h>
     2 #include <linux/miscdevice.h>
     3 #include <linux/fs.h>
     4 
     5 
     6 static int misc_demo_open(struct inode *nodp, struct file *filp)
     7 {
     8     printk("%s enter, nodp: %p, filp: %p.
    ", __func__, nodp, filp);
     9 
    10     return 0;
    11 }
    12 
    13 static int misc_demo_release(struct inode *nodp, struct file *filp)
    14 {
    15     printk("%s enter, nodp: %p, filp: %p.
    ", __func__, nodp, filp);
    16 
    17     return 0;
    18 }
    19 
    20 static struct file_operations misc_demo_fops = {
    21     .owner = THIS_MODULE,
    22     .open = misc_demo_open,
    23     .release = misc_demo_release,
    24 };
    25 
    26 static struct miscdevice misc_demo_dev = {
    27     .minor = MISC_DYNAMIC_MINOR,
    28     .name = "misc_demo",
    29     .fops = &misc_demo_fops
    30 };
    31 
    32 static __init int misc_demo_init(void)
    33 {
    34     int ret; 
    35 
    36     ret = misc_register(&misc_demo_dev);
    37 
    38     return ret;
    39 }
    40 
    41 static __exit void misc_demo_exit(void)
    42 {
    43     misc_deregister(&misc_demo_dev);
    44 
    45     return;
    46 }
    47 
    48 module_init(misc_demo_init);
    49 module_exit(misc_demo_exit);
    50 MODULE_LICENSE("GPL");


    下面是用戶空間測試代碼:

     1 #include <stdio.h>
     2 #include <sys/types.h>
     3 #include <sys/stat.h>
     4 #include <fcntl.h>
     5 #include <unistd.h>
     6 
     7 
     8 int main(int argc, const char *argv[])
     9 {
    10     int fd[3], fd2[3], i;
    11 
    12     printf("Begin open.
    ");
    13     for (i=0; i<3; i++) {
    14         fd[i] = open("/dev/misc_demo", O_RDONLY);
    15         printf("open: %d
    ", fd[i]);
    16         fd2[i] = dup(fd[i]);
    17         printf("dup: %d
    ", fd2[i]);
    18         sleep(1);
    19     }
    20 
    21     sleep(5);
    22 
    23     printf("Begin close.
    ");
    24     for (i=0; i<3; i++) {
    25         printf("close: %d
    ", fd[i]);
    26         close(fd[i]);
    27         sleep(1);
    28     }
    29 
    30     sleep(2);
    31 
    32     printf("Begin close dup.
    ");
    33     for (i=0; i<3; i++) {
    34         printf("close dup: %d
    ", fd2[i]);
    35         close(fd2[i]);
    36         sleep(1);
    37     }
    38 
    39     return 0;
    40 }

    下面是輸出的log:

    Begin open.
    [ 4628.805135] misc_demo_open enter, nodp: c3b88a18, filp: c3859060.
    open: 3
    dup: 4
    [ 4629.809860] misc_demo_open enter, nodp: c3b88a18, filp: c3859c40.
    open: 5
    dup: 6
    [ 4630.814891] misc_demo_open enter, nodp: c3b88a18, filp: c3859ec0.
    open: 7
    dup: 8
    
    
    Begin close.
    close: 3
    close: 5
    close: 7

    Begin close dup. close dup: 4 [ 4641.845172] misc_demo_release enter, nodp: c3b88a18, filp: c3859060. close dup: 6 [ 4642.850183] misc_demo_release enter, nodp: c3b88a18, filp: c3859c40. close dup: 8 [ 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與之對應。

    完。

  • 相关阅读:
    Unity3D中AssetBundle应用
    C++智能指针 auto_ptr、shared_ptr、weak_ptr和unique_ptr
    C++枚举类型Enum及C++11强枚举类型用法
    C++强制类型转换
    Git使用(二、分支的创建和上传)
    Git使用(一、TortoiseGit和Gitlab在Windows下的项目库创建和上传)
    UE4行为树
    软件光栅器实现(四、OBJ文件加载)
    软件光栅器实现(三、裁剪)
    软件光栅器实现(二、VS和PS的运作,法线贴图,切空间的计算)
  • 原文地址:https://www.cnblogs.com/pengdonglin137/p/5990558.html
Copyright © 2011-2022 走看看