码迷,mamicode.com
首页 > 系统相关 > 详细

Linux基礎知識 —— open&close

时间:2016-10-23 21:08:12      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:linu   license   this   include   nod   void   return   dynamic   release   

下面說一下在用戶空間調用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纔會被執行。

 

完。

Linux基礎知識 —— open&close

标签:linu   license   this   include   nod   void   return   dynamic   release   

原文地址:http://www.cnblogs.com/pengdonglin137/p/5990558.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!