Linux file descriptor tables Open File Table Inode Vnode

Source: Internet
Author: User

 

In Linux, a process accesses a file through a file descriptor (a filename descriptors, or FD) rather than a file name, which is actually an integer. Linux specifies that each process can use a maximum of Nr_open file descriptors at the same time, which is defined in fs.h and is defined as 1024*1024 (only 256 in version 2.0).

Each file has a 32-bit number that represents the next read and write byte position, which is called the file location. Each time you open a file, unless explicitly requested, the file location is set to 0, that is, the beginning of the file, and thereafter the read or write operation will be performed from the beginning of the file, but you can modify the file location by executing the system call Lseek (random storage). Linux uses a data structure file to hold open file location, which is called Open file description. The setup of this data structure is painstaking, because it is closely related to the process, which can be said to be a more difficult data structure in the VFS.


The file structure mainly holds the location of the files, in addition to the pointer to the file index node is also placed in it. The file structure forms a doubly linked list, called the System Open File table, whose maximum length is nr_file, defined in Fs.h as 8192.

The file structure is defined in Include\linux\fs.h as follows:

[CPP]View Plaincopy
  1. struct file
  2. {
  3. struct List_head f_list; / * All open files form a linked list * /
  4. struct Dentry *f_dentry; / * Pointer to related catalog item * /
  5. struct Vfsmount *f_vfsmnt; / * Pointer to VFS mount point * /
  6. struct file_operations *f_op; / * Pointer to the File action table * /
  7. mode_t F_mode; / * Open mode for file * /
  8. loff_t F_pos; / * Current Location of file * /
  9. unsigned short f_flags; / * Flag specified when opening the file * /
  10. unsigned short f_count; / * Number of processes using the structure * /
  11. Unsigned long F_reada, F_ramax, F_raend, F_ralen, F_rawin;
  12. / * read-ahead flag, the maximum number of pages to read, the last read-ahead file pointer, the number of read-ahead bytes, and the number of pre-read pages * /
  13. int F_owner; /* Transfer of asynchronous I/O data via signal */
  14. unsigned int f_uid, f_gid; /* UID and gid*/of user
  15. int f_error; / * error code for network write operation * /
  16. unsigned long f_version; / * Version number * /
  17. void *private_data; / * TTY driver required * /
  18. };


The kernel, which corresponds to each process, has a file descriptor that represents all the files opened by the process. Each item in the File description table is a pointer to a data block that describes the open file ——— the file object, which describes important information such as open mode, read and write location, and the kernel creates a new file object when the process opens. It is important to note that the file object is not dedicated to a process, and the pointers in the files descriptor tables of the different processes can point to the same file object, thereby sharing the open files. The file object has a reference count, which records the number of filename descriptors that reference the object, and the kernel destroys the file object only if the reference count is 0 o'clock, so a process closes it without affecting the process that shares the same file object with it.

The file object contains a pointer to the Dentry object. The Dentry object represents a separate file path, and if a file path is opened more than once, multiple file objects are created, but they all point to the same Dentry object. The Dentry object also contains a pointer to the Inode object. The Inode object represents a separate file. Because there are hard links and symbolic links, different dentry objects can point to the same Inode object. The Inode object contains all the information that is required to eventually manipulate the file, such as the file system type, how the file is manipulated, the permissions of the file, the date of access, and so on. After opening the file, the process obtains the file descriptor is essentially the file descriptor of the subscript, the kernel according to the subscript value to access the corresponding file object, so as to achieve the operation of the file.

Note that the kernel creates multiple file objects when the same file is opened multiple times by the same process.   When a process uses a fork system call to create a child process, the child process inherits the file descriptor of the parent process, so files opened in the parent process can be accessed in the child process with the same descriptor. Links: http://oss.org.cn/kernel-book/ch08/8.2.4.htmhttp://blog.chinaunix.net/uid-25940216-id-3118066.htmlhttp:// Canbeatle.iteye.com/blog/891319http://blog.21ic.com/user1/4132/archives/2010/75445.html

Linux file descriptor table open file tables Inode Vnode

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.