Detailed Explanation of bufio Package in Golang (1): Buffered and Unbuffered IO Operations

The previous articles have introduced the io package in detail, and the io package provides a basic operation interface for IO primitives (I/O primitives). The bufio package to be explained next provides buffered io operations compared to the io package. First, the difference and connection between buffered and unbuffered is explained.

How to understand unbuffered I/O?

The unbuffered I/O operation means that the application directly calls the system call function of the operating system to complete, instead of putting data into a buffer first, and then reading and writing data from the buffer. The unbuffered I/O operation provided by the language itself means that the process does not provide buffering function (but the kernel still provides buffering. The system kernel will provide a block buffer for reading and writing to the disk. When writing data, first write the data to The block buffer is queued, and when the block buffer reaches a certain amount, the data is written to the disk).

The io package in the Golang standard library provides unbuffered read and write operations, and a system call is made for each operation.

How to understand buffered I/O?

Buffered I/O means that the process provides a buffer when performing I/O operations, and the data will be read into a buffer first. When certain conditions are met, such as the stream buffer is full or the buffer is refreshed , and then read or write from the buffer, a system call will be generated.

The bufio package in the Golang standard library provides buffered read and write operation functions. When reading and writing files or network data, you can use the bufio package to improve efficiency. For example, when reading data from a large file, you can use bufio.Reader to read some data into the buffer each time, and then read data from the buffer to avoid system calls for each read and improve efficiency . Similarly, when a large amount of data needs to be written to a file or network, you can use bufio.Writer to write the data into the buffer each time, and then write the data in the buffer to the file at one time.

Advantages of buffered I/O over unbuffered I/O

Buffered I/O can reduce the number of system calls and improve execution efficiency. The data exchange between the kernel and peripheral devices, and the data exchange between the kernel and user space are relatively time-consuming, and the use of buffers greatly optimizes these time-consuming operations.

summary

This article introduces buffered and unbuffered IO operations. It can be seen that buffered I/O provides a buffer, and the data will be read into a buffer first. When certain conditions are met, the data will be read from the buffer. A system call is generated for reading or writing. Compared with unbuffered I/O operations, it reduces the number of system calls and improves execution efficiency. The next few articles will explain in detail the knowledge related to the bufio package.

Guess you like

Origin blog.csdn.net/luduoyuan/article/details/131316301