Home>Article>Operation and Maintenance> Detailed explanation of examples of IO buffer management

Detailed explanation of examples of IO buffer management

PHP中文网
PHP中文网 Original
2017-06-20 13:19:50 2419browse

The write prototype in Linux system IO isssize_t write(int filedes, const void * buff, size_t nbytes);

When calling write When reading data, write returns directly after the call is completed, but the disk is a slow device. The operating system will save the data in the buffer in the kernel and be responsible for writing the data to the disk asynchronously. Of course, if the system goes down at this time, data will be lost. Write is a system call, and each call will trap the kernel, so choosing an appropriate block length buffsize and minimizing its calls can optimize efficiency. In the standard IO of ANSI C, when we call printf/fprintf/fputs, etc., they will be processed in a stream. We only need to write to the stream instead of selecting a buffsize like write, because the standard IO library handles many details for us. , such as buffer allocation, performing IO with optimized length, etc. This will reduce the number of write/read system calls and improve efficiency. But at the same time, another problem will be introduced: data copying. For example, when using the functions fgets and fputs, it usually needs to go through two buffers: one is the standard IO buffer, and the other is the kernel buffer that calls read and write. But in general, using standard IO has a simpler interface than system IO and is equally efficient.

Standard IO provides three types of buffers: full cache, row cache and no cache. Full cache will only actively flush when the buffer is full. It is usually used for A disk file IO. The line cache will flush when it encounters a newline character in the buffer. In another case, the buffer will be flushed when input data needs to be obtained from the standard input and output. The line cache is generally used in interactive terminals. Without caching, it is equivalent to directly writing the system call output. The standard error stream stderr is usually not cached, which allows the error message to be displayed as quickly as possible. In addition to the default flush conditions, the buffer will also be flushed when the fflush function is explicitly called and the program terminates normally. We can use setbuf/setvbuf to change the default buffer length, see APUE Section 5.4.

In a program that uses standard IO, when we redirect a standard output to a file, the line cache will become a full cache, which may cause Some unexpected errors, such as when calling printf("*****\n"), will be output normally when the program is run in interactive mode. But when the standard output is redirected to a file, the buffer area becomes fully cached, printf will not output normally, and the line of data is still in the buffer. If you fork a child process at this time, when the data space is copied to the child process, the buffer data will also be copied to the child process. Then, if output is performed in the child process, the previous content in the buffer will be refreshed, resulting in some unexpected output.

In network programming, system IO should be used directly. Standard IO introduces a buffering mechanism to improve performance, which increases the complexity of network applications. Moreover, in a sense, the standard IO stream is full-duplex and can perform input and output at the same time. However, the restrictions on the stream and the restrictions on the socket sometimes conflict with each other. (See CSAPP P611)

Some advanced network libraries (such as the muduo library) will create their own buffers based on the use of system IO to help users shield system IO Some inconveniences, such as when calling write to send a large amount of data, the application layer needs to wait when the sending buffer is full, and when read receives data, packets are sticky and data is received slowly. When the application layer buffer is added, the network library handles these implementation details to simplify user operations.

Linux also provides zero-copy technology to reduce memory copies and thereby improve efficiency. We know that using read/write to send data from the disk to the network card will go through four copy operations: when an application needs to access a certain piece of data At this time, the operating system kernel will first check whether the data has been stored in the buffer of the operating system kernel address space due to a previous access to the same file. If the data is not found in the kernel buffer, Linux The operating system kernel will first read this data from the disk and put it in the buffer of the operating system kernel. If this data reading operation is completed by DMA, then during the process of data reading by DMA, the CPU only needs to perform buffer management, and create and process DMA. In addition, the CPU does not need to do any other changes. Many things, after DMA performs the data reading operation, it will notify the operating system for further processing. The Linux operating system will store this piece of data in the address space of the application that requested this piece of data based on the address of the application address space specified by the read system call. After the user completes the operation on the data, the operating system needs to restore the data. A copy is made from the buffer in the user application address space to the kernel buffer related to the network stack. This process also requires CPU usage. After the data copy operation is completed, the data will be packaged and then sent to the network interface card. As can be seen from the above description, during this traditional data transfer process, the data is copied at least four times. Even if DMA is used to communicate with the hardware, the CPU still needs to access the data twice.

(ps: I remember reading an interview question before that said the printf output process passes through several buffers. Now everyone understands it!)

Using zero-copy technology can avoid data copying in the buffer of the system kernel address space and the buffer of the user application address space. Sometimes, the application does not need to access the data during the data transmission process. The transmitted data does not need to be copied to the user application area, but can be sent directly to the network card through the kernel. This can improve performance, and zero copy is required at this time. technology. Under Linux, you can use mmap, sendfile, and splice to achieve zero copy.

The above is the detailed content of Detailed explanation of examples of IO buffer management. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn