Linux基礎知識 —— open&close

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.\n", __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.\n", __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.\n");
13     for (i=0; i<3; i++) {
14         fd[i] = open("/dev/misc_demo", O_RDONLY);
15         printf("open: %d\n", fd[i]);
16         fd2[i] = dup(fd[i]);
17         printf("dup: %d\n", fd2[i]);
18         sleep(1);
19     }
20 
21     sleep(5);
22 
23     printf("Begin close.\n");
24     for (i=0; i<3; i++) {
25         printf("close: %d\n", fd[i]);
26         close(fd[i]);
27         sleep(1);
28     }
29 
30     sleep(2);
31 
32     printf("Begin close dup.\n");
33     for (i=0; i<3; i++) {
34         printf("close dup: %d\n", 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與之對應。

 

完。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108826.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 嵌入式C语言的小细节,excel的几个函数[通俗易懂]

    嵌入式C语言的小细节,excel的几个函数[通俗易懂]记录一下笔记嵌入式C语言有时候无法使用Static关键字嵌入式C语言中如果定义了变量没有使用可能报错嵌入式C语言中如果定义了无符号类型的变量最好别去和0比较,可能会报错嵌入式C语言的技巧if,else的嵌套使用得好可以加快速度excel的使用=‘sheet’!A1跨表传递数据很有用excel的使用中,=count(obj1:obj2)是一个很厉害的函数,作用是统计obj1到obj2的数字的个数;…

    2022年10月21日
  • step by step学习LAMP配置

    step by step学习LAMP配置配置环境:debian6+apache+mysql+python步骤:1.在虚拟机上安装debian;2.配置虚拟机上网(采用host-only连接,单网卡,会虚拟出一个网卡,将主机的网络共享《 VMware下Linux网络配置攻略》,虚拟机和主机能够互相访问)3.配置debian,安装必要的软件包4.参考《http://library.linode.com/

    2022年6月6日
  • Nginx修改默认端口80

    Nginx修改默认端口80前言    安装流程请参考我的文章–Windows下安装Nginx。    博客地址:https://blog.csdn.net/zengwende/article/details/86610692修改步骤1、打开Nginx的配置文件nginx.conf2、修改默认端口的值即可(nginx默认的端口为80) …

    2022年9月8日
  • react 路由完整版「建议收藏」

    react 路由完整版「建议收藏」import{BrowserRouter}from’react-router-dom’1、用BrowserRouter管理整个应用 在index.js中,将<App/>用<BrowserRouter>包裹起来2、路由跳转 import{NavLink,Link}from’react-router-dom’ NavLink和Link都可以实现,且用法相同 <NavLinkto=’/about’>About</NavLink&g

    2022年4月28日
  • 如何快速记忆小鹤双拼键位图?

    如何快速记忆小鹤双拼键位图?记忆方法:韵母表+图形单韵母aoeiu、üAOEIV双拼练习网址:https://api.ihint.me/shuang/

    2022年6月29日
  • nat模式「建议收藏」

    nat模式「建议收藏」原文链接:https://www.linuxidc.com/Linux/2016-09/135521p2.htm(复制过来只是为了学习方便,如有不妥会立即删除)二、NAT(地址转换模式)刚刚我们说到,如果你的网络ip资源紧缺,但是你又希望你的虚拟机能够联网,这时候NAT模式是最好的选择。NAT模式借助虚拟NAT设备和虚拟DHCP服务器,使得虚拟机可以联网。其网络结构如下图所示:在NAT模式中,主机…

    2022年6月29日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号