Home Operation and Maintenance Linux Operation and Maintenance Detailed explanation of examples of IO buffer management

Detailed explanation of examples of IO buffer management

Jun 20, 2017 pm 01:19 PM
Backstage manage buffer

The write prototype in Linux system IO is ssize_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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Discuz background login problem solution revealed Discuz background login problem solution revealed Mar 03, 2024 am 08:57 AM

The solution to the Discuz background login problem is revealed. Specific code examples are needed. With the rapid development of the Internet, website construction has become more and more common, and Discuz, as a commonly used forum website building system, has been favored by many webmasters. However, precisely because of its powerful functions, sometimes we encounter some problems when using Discuz, such as background login problems. Today, we will reveal the solution to the Discuz background login problem and provide specific code examples. We hope to help those in need.

How to use Redis to implement distributed transaction management How to use Redis to implement distributed transaction management Nov 07, 2023 pm 12:07 PM

How to use Redis to implement distributed transaction management Introduction: With the rapid development of the Internet, the use of distributed systems is becoming more and more widespread. In distributed systems, transaction management is an important challenge. Traditional transaction management methods are difficult to implement in distributed systems and are inefficient. Using the characteristics of Redis, we can easily implement distributed transaction management and improve the performance and reliability of the system. 1. Introduction to Redis Redis is a memory-based data storage system with efficient read and write performance and rich data

Are you worried about WordPress backend garbled code? Try these solutions Are you worried about WordPress backend garbled code? Try these solutions Mar 05, 2024 pm 09:27 PM

Are you worried about WordPress backend garbled code? Try these solutions, specific code examples are required. With the widespread application of WordPress in website construction, many users may encounter the problem of garbled code in the WordPress backend. This kind of problem will cause the background management interface to display garbled characters, causing great trouble to users. This article will introduce some common solutions to help users solve the trouble of garbled characters in the WordPress backend. Modify the wp-config.php file and open wp-config.

How to implement student performance management function in Java? How to implement student performance management function in Java? Nov 04, 2023 pm 12:00 PM

How to implement student performance management function in Java? In the modern education system, student performance management is a very important task. By managing student performance, schools can better monitor students' learning progress, understand their weaknesses and strengths, and make more targeted teaching plans based on this information. In this article, we will discuss how to use Java programming language to implement student performance management functions. First, we need to determine the data structure of student grades. Typically, student grades can be represented as a

Laravel extension package management: easily integrate third-party code and functions Laravel extension package management: easily integrate third-party code and functions Aug 25, 2023 pm 04:07 PM

Laravel extension package management: Easily integrate third-party code and functions Introduction: In Laravel development, we often use third-party code and functions to improve the efficiency and stability of the project. The Laravel extension package management system allows us to easily integrate these third-party codes and functions, making our development work more convenient and efficient. This article will introduce the basic concepts and usage of Laravel extension package management, and use some practical code examples to help readers better understand and apply it. What is Lara

What to do if the right-click menu management cannot be opened in Windows 10 What to do if the right-click menu management cannot be opened in Windows 10 Jan 04, 2024 pm 07:07 PM

When we use the win10 system, when we use the mouse to right-click the desktop or the right-click menu, we find that the menu cannot be opened and we cannot use the computer normally. At this time, we need to restore the system to solve the problem. Win10 right-click menu management cannot be opened: 1. First open our control panel, and then click. 2. Then click under Security and Maintenance. 3. Click on the right to restore the system. 4. If it still cannot be used, check whether there is something wrong with the mouse itself. 5. If you are sure there is no problem with the mouse, press + and enter. 6. After the execution is completed, restart the computer.

Six pictures explain Linux zero-copy technology clearly Six pictures explain Linux zero-copy technology clearly Feb 22, 2024 pm 06:40 PM

Hello everyone, today let us talk about Linux zero-copy technology. We will use the sendfile system call as an entry point to deeply explore the basic principles of zero-copy technology. The core idea of ​​zero-copy technology is to minimize the copying of data between memories and improve the efficiency and performance of data transmission by optimizing the data transmission path. 1. Introduction to zero-copy technology Linux zero-copy technology is a technology used to optimize data transmission. It improves the efficiency of data transmission by reducing the number of data copies between kernel mode and user mode. During the process of data transmission, it is usually necessary to copy the data from the kernel buffer to the application buffer, and then from the application buffer to the buffer of the network device before the transmission can be completed. Advantages of zero-copy technology

Discuz background login failed? Teach you how to solve it easily! Discuz background login failed? Teach you how to solve it easily! Mar 02, 2024 pm 06:03 PM

Discuz background login failed? Teach you how to solve it easily! As Discuz, as a popular forum platform, is widely used in website construction and management, sometimes you will encounter backend login failures, which is troubling. Today we will discuss the issues that may cause Discuz backend login failure, provide some solutions, and attach specific code examples. I hope this article can help webmasters and developers who encounter similar problems. 1. Troubleshooting is to solve the problem of Discuz background login failure.

See all articles