


Detailed 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!

The five core components of the Linux operating system are: 1. Kernel, 2. System libraries, 3. System tools, 4. System services, 5. File system. These components work together to ensure the stable and efficient operation of the system, and together form a powerful and flexible operating system.

The five core elements of Linux are: 1. Kernel, 2. Command line interface, 3. File system, 4. Package management, 5. Community and open source. Together, these elements define the nature and functionality of Linux.

Linux user management and security can be achieved through the following steps: 1. Create users and groups, using commands such as sudouseradd-m-gdevelopers-s/bin/bashjohn. 2. Bulkly create users and set password policies, using the for loop and chpasswd commands. 3. Check and fix common errors, home directory and shell settings. 4. Implement best practices such as strong cryptographic policies, regular audits and the principle of minimum authority. 5. Optimize performance, use sudo and adjust PAM module configuration. Through these methods, users can be effectively managed and system security can be improved.

The core operations of Linux file system and process management include file system management and process control. 1) File system operations include creating, deleting, copying and moving files or directories, using commands such as mkdir, rmdir, cp and mv. 2) Process management involves starting, monitoring and killing processes, using commands such as ./my_script.sh&, top and kill.

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor
