search
HomeOperation and MaintenanceLinux Operation and MaintenanceDetailed explanation of examples of IO buffer management

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
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
日志记录器缓冲区大小日志有什么用日志记录器缓冲区大小日志有什么用Mar 13, 2023 pm 04:27 PM

作用是:给工程师们反馈使用信息与记录便于分析问题(开发时使用的);由于用户本身不是经常产生上传日志,所以对用户无用。日志记录缓冲区是小型的、用于短期存储将写入到磁盘上的重做日志的变更向量的临时区域。日志缓冲区对磁盘的一次写入是来自多个事务的一批变更向量。即使如此,日志缓冲区中的变更向量也是接近实时地写入磁盘,当会话发出COMMIT语句时,会实时执行日志缓冲区写操作。

Laravel扩展包管理:轻松集成第三方代码和功能Laravel扩展包管理:轻松集成第三方代码和功能Aug 25, 2023 pm 04:07 PM

Laravel扩展包管理:轻松集成第三方代码和功能引言:在Laravel开发中,我们经常使用第三方代码和功能来提高项目的效率和稳定性。而Laravel扩展包管理系统允许我们轻松地集成这些第三方代码和功能,使得我们的开发工作更加便捷和高效。本文将介绍Laravel扩展包管理的基本概念和使用方法,并通过一些实际的代码示例来帮助读者更好地理解和应用。什么是Lara

如何在麒麟操作系统上进行网络服务器的设置和管理?如何在麒麟操作系统上进行网络服务器的设置和管理?Aug 04, 2023 pm 09:25 PM

如何在麒麟操作系统上进行网络服务器的设置和管理?麒麟操作系统是中国自主开发的一种基于Linux的操作系统。它具有开源、安全、稳定等特点,在国内得到了广泛的应用。本文将介绍如何在麒麟操作系统上进行网络服务器的设置和管理,帮助读者更好地搭建和管理自己的网络服务器。一、安装相关软件在开始设置和管理网络服务器之前,我们需要先安装一些必要的软件。在麒麟操作系统上,可以

如何在麒麟操作系统上进行硬盘空间的管理和清理?如何在麒麟操作系统上进行硬盘空间的管理和清理?Aug 04, 2023 am 09:49 AM

如何在麒麟操作系统上进行硬盘空间的管理和清理?麒麟操作系统是一个基于Linux的操作系统,相比其他操作系统,麒麟提供了更多的自由度和可定制性。在长期的使用过程中,我们经常会遇到硬盘空间不足的问题,这时候就需要进行硬盘空间的管理和清理。本文将介绍如何在麒麟操作系统上进行硬盘空间的管理和清理,包括查看硬盘空间使用情况、删除不必要的文件以及使用磁盘清理工具。首先,

ThinkPHP6中如何进行审核流程管理?ThinkPHP6中如何进行审核流程管理?Jun 12, 2023 am 09:31 AM

随着互联网的发展,越来越多的企业开始使用网络进行业务处理,这就要求企业必须有一套完善的审核流程管理系统来确保业务的安全和规范。在PHP开发中,ThinkPHP6框架提供了便捷的审核流程管理功能,本文将介绍如何在ThinkPHP6中实现审核流程管理。一、ThinkPHP6审核流程管理基本思路ThinkPHP6的审核流程管理基本思路是通过数据库记录来实现,一般需

六张图讲清楚Linux零拷贝技术六张图讲清楚Linux零拷贝技术Feb 22, 2024 pm 06:40 PM

大家好,今天让我们聊一聊Linux零拷贝技术。我们将以sendfile系统调用作为切入点,深入探讨零拷贝技术的基本原理。零拷贝技术的核心思想是尽量减少数据在内存之间的复制,通过优化数据传输路径,提高数据传输的效率和性能。1.零拷贝技术简介Linux零拷贝技术是一项用于优化数据传输的技术,通过减少数据在内核态和用户态之间的复制次数,从而提高数据传输的效率。在数据传输的过程中,通常需要将数据从内核缓冲区复制到应用程序的缓冲区,再从应用程序缓冲区复制到网络设备的缓冲区,最终才能完成发送。零拷贝技术的优

如何在Linux中进行集群管理如何在Linux中进行集群管理Jun 19, 2023 am 08:21 AM

在高可用性(HA)的系统中,集群是不可或缺的一部分。当一个单一节点不能提供足够的可用性或性能时,集群是一种实用的解决方案。Linux是非常流行的集群环境,它通过多种途径来提供集群的实现和支持。在本文中,我们将学习如何在Linux中进行集群管理。集群管理软件Linux使用许多集群管理软件来帮助管理员轻松地管理多台服务器的集群实例。有许多工具可供选择,其

麒麟操作系统如何提供多屏幕工作环境的扩展和管理?麒麟操作系统如何提供多屏幕工作环境的扩展和管理?Aug 04, 2023 am 10:15 AM

麒麟操作系统如何提供多屏幕工作环境的扩展和管理?随着计算机技术的不断发展,多屏幕显示已经成为现代工作环境中的一个常见需求。为了满足用户对于多任务处理和工作效率的要求,麒麟操作系统提供了一套强大的多屏幕扩展和管理功能。本文将介绍麒麟操作系统如何实现多屏幕工作环境的扩展和管理,并附上相应的代码示例。多屏幕工作环境的扩展麒麟操作系统通过提供多屏幕工作环境的扩展功能

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools