LINUX--STRUCT file Structure

Source: Internet
Author: User

struct file (file struct):
The struct file struct definition is defined in include/linux/fs.h. The file structure represents an open file, and each open file in the system has an associated struct file in the kernel space.

It is created by the kernel when the file is opened and passed to any function that operates on the file. After all instances of the file have been closed, the kernel releases this data structure. In the kernel creation and drive source code,

A pointer to a struct file is usually named file or Filp. It has two very important fields: file descriptors and buffers.

file descriptor FD:

FD is a small integer that is generated when open. As an index, the process uses the file descriptor in the PCB to find the file pointer Filp that the FD points to.

The operation of the file descriptor (for example: Open) returns a file descriptor, which maintains a file descriptor table in each process space, and all open files are referenced by the file descriptor in this form;
While the stream (for example: fopen) Returns a file structure pointer, which is included with the document descriptor, the file structure function can be regarded as the encapsulation of the system call to the FD direct operation, which has the advantage of having I/O

Cache.

Each process maintains a file descriptor table in the PCB (Process Control block), which is an index to the list, and each table entry in the File description table has a

to already open pointer to the file, now let's be clear: The open file is represented in the kernel with the file struct, and the pointer in the descriptor table points to the file structure.

Buffer:

A ) buffer mechanism

Depending on how the application accesses the file, that is, if there is a buffer, access to the file can be divided into buffer-and non-buffer file operations:

A) with buffer file operation: Advanced standard file I/O operations will automatically open a memory buffer in the user space for the file being used.

b) Non-buffer file operations: Low-level file I/O operations, read and write files, will not open the buffer of file operations, directly through the system calls to the disk operation (read, write, etc.), of course, can be used in their own

in the program set a buffer for each file.

Explanation and comparison of the two file operations:

1, non-buffered file operation access, each time a read and write operation of the file, you need to use a read-write system call to handle this operation, that is, a system call needs to be executed once the system call will involve

and to the CPU The switching of the state, that is, from the user space to the kernel space, the process context of the switch, which will lose a certain amount of CPU time, frequent disk access to the execution efficiency of the program has a great impact.

2, ANSI standard C library function is built on the bottom of the system call, that is, the C function library file Access function is used in the implementation of low-level file I/O system calls, ANSI standard C library file processing function in order to reduce

less use of the system system calls, improve efficiency, the use of buffering mechanism, so that can be in the disk file operation, you can read a large amount of data from the file in the buffer, later access to this part does not need

to use the system again called, which requires a small amount of CPU state switching, which improves efficiency.

B ) Buffer type

Standard I/O provides 3 types of buffers.

1, full buffer: This buffering method requires filling the entire buffer before the I/O system call operation. Operations for disk files are typically accessed in a fully buffered manner. The first time I/O operation is performed, the ANSI label

quasi-file tube function to obtain the required buffer by calling the malloc function with a default size of 8192.

2. Row buffers: In the case of row buffering, the standard I/O library function performs system invoke operations when a newline character is encountered in input and output. When the flow that is being manipulated involves a terminal (for example, standard input and standard

output), using The line buffering method. Because the buffer length for each row of the standard I/O library is fixed, the I/O system call operation is performed, even if no line break has been encountered, as long as the buffers are filled in, and the default row buffers

The size is 1024.

3. No buffer:

No buffer means that the standard I/O library does not cache characters and calls system calls directly. The standard error stream stderr is usually non-buffered, which allows the error message to be displayed as quickly as possible.

Note:

1. Standard input and standard output devices: the standard input stream and the standard output stream are fully buffered when and only if the interaction device is not involved.

2, standard error output device: Standard errors will never be fully buffered.

3. For any given stream, you can call the Setbuf () and setvbuf () functions to change their buffer type.

Below we will learn more about buffers using the following procedure:

When printing to the screen (standard output):

When writing to a file:

So why output to the screen only 5 output commands and output to a file has 7 output command?

According to the output we can see that printf and fwrite repeat two times, no duplicate print is write.

Both printf and fwrite are library functions: in combination with existing knowledge, we understand that when using the Library function command, the print message is not written directly to the output location, but rather first writes the data to the output buffer,

Out of position.

1, when the output target location for output to the display, the refresh mode is row refresh;

2, when the output target location for output to a file, the refresh mode from the row buffer to full buffer, full buffer refers to when the buffer is full before refreshing. (or force refresh)

In the code, printf and fwrite first print, before the fork operation, the second time after the fork operation, because the output commands of printf and fwrite write the data into the buffer before the fork operation, at which point

only carried out this two commands, because it is a fully buffered refresh mode, so these two commands are not enough to fill the cache, so the data is temporarily present in the buffer, and then fork to create the child process, because the fork created by the parent-child

Process code sharing, and data is not shared, each selfish one, the data in the buffer belongs to the data, because the parent process's residual data is still in the buffer, so after the fork is finished, the parent-child process will save the data in the cache

a copy has the parent process residue data data, so when the parent-child process is refreshed individually, the parent-child process executes the output commands of printf and fwrite once, so the command changes from two to four.

Other important members of the Structfile are:.
1.mode_t F_mode;
The file mode determines whether the file is readable or writable (or both), via Bits fmode_read and fmode_write. You may want to check the read and write of this member in your open or IOCTL function.

License, but It is not necessary to check the read and write permission because the kernel checks before calling your method. Attempts to read or write when the file has not been opened for that kind of access are rejected, and the driver is not even aware of the situation.

2.loff_t F_pos;
The current read and write location. loff_t is 64 bits on all platforms (long long in GCC terminology). The driver can read this value if it needs to know the current position in the file, but should not change it normally; Read

and write should use them as pointers to the last parameter to update a position instead of directly acting on Filp->f_pos. An exception to this rule is in the Llseek method, which is intended to change the file location.

3.unsigned int f_flags;
These are file flags, such as O_rdonly, O_nonblock, and O_sync. The driver should check the O_NONBLOCK flag to see if it is a request non-blocking operation; Other flags are seldom used. In particular,

should check read/write permission, use F_mode instead of f_flags. All the flags are defined in the header file <linux/fcntl.h>.

4.struct file_operations *f_op;
The operation associated with the file. The kernel arranges the pointer as part of its open implementation, and then reads it when it needs to dispatch any operations. The value in Filp->f_op is never saved by the kernel as a subsequent reference; This means

you can change. The file operation associated with your file, the new method will work after you return the caller. For example, the open code associated to main Number 1 (/dev/null,/dev/zero, etc.) is replaced by the number of

Filp->f_op in the generation of This practice allows for several behaviors to be implemented under the same master number without introducing overhead in each system call. The ability to replace file operations is a kernel pair of object-oriented programming "method overloads"

Equal body.

5.void *private_data;
The open system call sets this pointer to NULL before the open method is called for the drive. You are free to use this member or ignore it; You can use this member to point to the assigned data, but then you must

remember in the kernel Release the memory in the release method before destroying the file structure. Private_data is a useful resource for preserving state information between system calls, which is used by most of our example modules.

6.struct Dentry *f_dentry;
The directory entry (DENTRY) structure associated to the file. Device driver writers normally do not need to care about the dentry structure, except as a filp->f_dentry->d_inode access inode structure.

LINUX--STRUCT file Structure

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.