Linux-based Knowledge--open&close

Source: Internet
Author: User

The following is a response to the open and release in the user's space between the open/close/dup and the drivers.

Here are the test drivers:

1#include <linux/module.h>2#include <linux/miscdevice.h>3#include <linux/fs.h>4 5 6 Static intMisc_demo_open (structInode *NODP,structFile *Filp)7 {8Printk"%s Enter, NODP:%p, Filp:%p.\n", __func__, NODP, FILP);9 Ten     return 0; One } A  - Static intMisc_demo_release (structInode *NODP,structFile *Filp) - { thePrintk"%s Enter, NODP:%p, Filp:%p.\n", __func__, NODP, FILP); -  -     return 0; - } +  - Static structFile_operations Misc_demo_fops = { +. Owner =This_module, A. Open =Misc_demo_open, at. Release =Misc_demo_release, - }; -  - Static structMiscdevice Misc_demo_dev = { -. minor =Misc_dynamic_minor, -. Name ="Misc_demo", in. FoPs = &Misc_demo_fops - }; to  + Static__initintMisc_demo_init (void) - { the     intret; *  $ret = Misc_register (&Misc_demo_dev);Panax Notoginseng  -     returnret; the } +  A Static__exitvoidMisc_demo_exit (void) the { +Misc_deregister (&Misc_demo_dev); -  $     return; $ } -  - Module_init (misc_demo_init); the Module_exit (misc_demo_exit); -Module_license ("GPL");


Here is the user space test code:

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 intMainintargcConst Char*argv[])9 {Ten     intfd[3], fd2[3], I; One  Aprintf"Begin open.\n"); -      for(i=0; i<3; i++) { -Fd[i] = open ("/dev/misc_demo", o_rdonly); theprintf"Open:%d\n", Fd[i]); -Fd2[i] =DUP (Fd[i]); -printf"DUP:%d\n", Fd2[i]); -Sleep1); +     } -  +Sleep5); A  atprintf"Begin close.\n"); -      for(i=0; i<3; i++) { -printf"Close:%d\n", Fd[i]); - Close (Fd[i]); -Sleep1); -     } in  -Sleep2); to  +printf"Begin Close dup.\n"); -      for(i=0; i<3; i++) { theprintf"Close dup:%d\n", Fd2[i]); * Close (Fd2[i]); $Sleep1);Panax Notoginseng     } -  the     return 0; +}

The following is the output log:

Begin Open. [ 4628.805135] Misc_demo_open Enter, Nodp:c3b88a18, Filp:c3859060.open:3DUP:4[ 4629.809860] Misc_demo_open Enter, Nodp:c3b88a18, Filp:c3859c40.open:5DUP:6[ 4630.814891] Misc_demo_open Enter, Nodp:c3b88a18, Filp:c3859ec0.open:7DUP:8Begin close.close:3Close:5Close: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.


Through the analysis log, we conclude that the open in the user's space will be executed once for each open, and when the DUP is adjusted, it only adds 1 to the reference count of the struct file, without producing a new struct file. So the new FD returned is the same struct file as the old FD, and does not use open at the same time. When close, only the struct file response to all FD is closed or if the struct file's reference count is 0, the release just in the drivers will be executed.

Finish.

Linux-based Knowledge--open&close

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.