Linux file descriptor (file descriptor) and file pointer

File descriptor (file descriptor):

      In linux system, the device is in the form of documents exist to operate the equipment must first open the file, open the file will get this file descriptor, it is a small positive integer that is an index value , pointing to the kernel for each process maintained, the process opens the file table record .

      The advantages of file descriptors: standard POSIX compatible, many Linux and UNIX system calls are dependent on it. Wenjianmiaoshufu disadvantages: not migrate to systems other than UNIX up, nor intuitive.

File pointer:

     C language is used in the file pointer instead of a file descriptor as the I / O handle. "File pointer (file pointer)" a process point to the user area is called FILE data structure structure ( not the Document Object File , the file descriptor that is pointed) . FILE structure comprising a buffer and a file descriptor value. And the file descriptor value is the file descriptor table an index . In a sense the file pointer is a handle to handle .

 

 

      File descriptor operations (such as: open) returns a file descriptor, the kernel in each process space maintained in a file descriptor table , all open files are referenced by this table of file descriptors; 
     flows (eg: fopen) returns a pointer to FILE structure, FILE structure that contains the file descriptor is , FILE structure function can be seen as direct encapsulation of fd operating system call, it has the advantage with the I / O cache  .

Linux supports a variety of file system formats, such as ext2, ext3, reiserfs, FAT, NTFS, iso9660 , etc., different disk partition, CD-ROM or other storage device has a different file system format , however, these file systems can mount to a directory , so we see a unified directory tree , directories and files on the various file systems we use the ls command to look the same, read and write operations with them are also the same, this is how do it? Linux kernel on a variety of different file system format made a layer of abstraction , making the concept of files, directories, and so become read-write access concept abstraction layer, so all kinds of file systems appear to use up all the same, this abstraction layer called a virtual file system (VFS , virtual filesystem). The following file system representation in the kernel of us running.

Kernel data structures

Linux VFS kernel subsystem may be illustrated as follows:

In the file structure maintenance File Status Flag (members of the file structure f_flags) and write current location (members of the file structure f_pos). In the figure, processes 1 and 2 are open the same file , but corresponding to different file structure , and therefore can have different File Status Flag and read-write position . file structure of the more important members include f_count , represents the reference count (the Count Reference), dup , fork system calls result in multiple file descriptors point to the same file structure , for example, fd1 and fd2 refer to the same file structure, then its reference count is 2, when the close time (FD1) and does not release the file structure , but only the reference count reduced to one, if we close (FD2), the reference count will be reduced to 0 simultaneously released file structure, this really closed the file. Each process in the PCB (Process Control Block) i.e., the process control block are stored with a file descriptor table, The file descriptor is an index of the table, file descriptor table Each entry has a point to open the file pointer , and now we are clear: open the file in the kernel with file structure representation, file descriptor table pointer to file structure .

Each file structure points to a file_operations structure, members of this structure are a function pointer , pointing to achieve a variety of file operations kernel functions . For example, a file descriptor read in the user program, read through system calls into the kernel, and then locate the file structure of the file descriptor pointed to find file_operations structure file structure pointed, calling its members pointed to read kernel functions to complete the user request. Called in the user program lseek , Read , Write , the ioctl , Open and other functions, eventually by kernel calls file_operations kernel function pointed to by the members of the user request is completed . file_operations structure of the release member for the user to complete the program of close request, was called release is not close to call because it does not necessarily really close the file , but to reduce the reference count, only the reference count reaches 0 before closing the file. For conventional open files on the same file systems, the Read , the WriteThe method steps and other file operations should be the same, function call should be the same , so that the file structure of FIG three open file file_operations point to the same structure. If you open a character device file , then it's the Read , the Write operation recognition and regular files are not the same , not read and write data blocks of disk read and write but hardware devices, so the file structure should point to a different file_operations structure , in which each kinds of file manipulation functions implemented by the device driver . 

Each file has a structure pointing dentry pointer structure , "dentry" is a directory entry ( directory entry ) Abbreviation. We pass function parameters open, stat, etc. is a path, such as / home / akaedu / a, you need to find the inode file according to the path . In order to reduce the number of read the disk, the kernel cache directory tree structure , called dentry Cache , where each node is a dentry structure, if a search can dentry portions along the path from the root directory / find home directory, then find akaedu directory, and then locate the file a. dentry cache only stores recently accessed directory entry , if you are looking in the directory entry is not in the cache, it is necessary to read from disk into memory .

Each dentry structure has a pointer to the inode structure . inode structure holds the information read from the disk inode up . In the example above, there are two dentry , respectively / home / akaedu / a and / home / akaedu / b, they all point to the same the inode , file illustrate two mutually hard links . inode structure stored in the information read from the inode up disk partitions, such as owner, file size, file type and permission bits and so on. Each inode structure has a pointer to inode_operations pointer structure, which is a pointer to the function set of some of the kernel function completing the file directory operations. And file_operations different, inode_operations points to not carry out a function for a certain file operations, and is a function of the impact of layout files and directories , for example, such as adding delete files and directories, follow symbolic links , and so on, each inode structure belonging to the same file system inode_operations body can point to the same structure.

inode structure has a pointer pointing super_block structure. super_block structure maintains information read from the disk partition superblock up, for example, a file system type, block size, and the like. super_block structure s_root member is a pointer dentry pointer indicating the file system of the root directory is mount where , in the example of the figure is to mount this partition / home directory.

File , dentry , inode , super_block these structures form the core of the VFS concept. For ext2 file system, stored on disk layout also has the concept of inode and superblock, so it is easy and VFS concept of establishing a corresponding relationship. While others file system format from non-UNIX systems (such as Windows, FAT32, NTFS), may not have inode or superblock such a concept , but in order to be able to mount a Linux system, the only thing in the driver hard to scrape together what , under Linux look FAT32 and NTFS partitions will find permission bit is wrong, all the files are rwxrwxrwx , because they had no concept of inode and permission bits, which is hard to scrape together out.


 

 

Guess you like

Origin blog.csdn.net/qq_38971487/article/details/91492832