File caching in linux

Source: Internet
Author: User
File caching in linux is also mentioned in advanced file programming in linux: the so-called file write buffering means that when a file stream performs an output operation, it does not immediately write data into the file, instead, the data is first accumulated to the buffer, and then batch output to the file in block units. Similarly, the file read buffer refers... information

File caching in linux
In advanced file programming in linux, we also talk about:
The so-called file write buffer means that when a file stream executes an output operation, it does not immediately write data into the file, but first accumulates data into the buffer zone, the data is then exported to files in batches in blocks. Similarly, file read buffering refers to reading file content in blocks when a file stream performs an input operation, the excess data is stored in the memory. If the content of the next read operation is exactly the same, the results can be directly returned to avoid one input operation. The buffer technology can reduce the number of calls to low-level I/O functions read and write functions, thus greatly improving the efficiency of software execution.
1) buffer mode
The standard FILE programming library uses the FILE type to describe the FILE stream. compared with low-level I/O functions, the biggest feature is the application and the added buffer function (low-level I/O functions only use the buffer function provided by the file system). the input and output of files are completed in batches in the unit of "buffer block, three buffer modes are provided based on the size of the buffer block.
(1) Full Buffer (_ IOFBF): generally, the full buffer mode is used to read and write common disk files.
(2) row Buffering (_ IOLBF): for example, if the fgets function is called to input characters from stdin of the standard input stream, the function returns only when the customer enters the carriage return and changes the line.
(3) no buffer (_ IONBF): for example, stderr adopts the no buffer mode;
2) buffer functions
# Include
Void setbuf (FILE * stream, char * buf );
Int setvbuf (FILE * stream, char * buf, int type, size_t size );
Int fflush (FILE * stream );
Setbuf sets the buffer of the file stream. the parameter buf points to a memory block of the BUFSIZ size. after the call is successful, the file stream uses the memory block as the new buffer. If the buf is a NULL pointer, the buffer of the file stream will be completely disabled. The buffer memory block is generally defined:
Char buf [BUFSIZ]; --- where BUFSIZ is a constant in stdio. h, which represents the buffer size, usually an integer multiple of 256.
Setvbuf sets the buffer and buffer mode of the file stream. the buffer mode is determined by the parameter type.
_ IOFBF (full buffer): When the buffer is empty, data is read from the stream. Or when the buffer is full, write data to the stream.
_ IOLBF (row buffering): each time a row of data is read from the stream or a row of data is written to the stream. _ IONBF (no buffer): reads data directly from the stream or writes data directly to the stream without a buffer.
You can use fflush to refresh the buffer at any time and forcibly output the buffer content to the file. the parameter stream specifies the updated file stream. when its value is NULL, the system refreshes the buffer of all file streams.
Instance:
# Include
Void main ()
{
Printf ("1---1 ");
/* Fflush (stdout );*/
Fprintf (stderr, "2---2 ");
Printf ("3---3 \ n ");
Fprintf (stderr, "4--4 \ n ");
}
Compile and run:
Root @ qingcheng-virtual-machine :~ # Gcc-o buf1 buf1.c
Root @ qingcheng-virtual-machine :~ #./Buf1
2---2 1---1 3---3
4--4
If the fflush comment is removed, the result is as follows:
Root @ qingcheng-virtual-machine :~ # Gcc-o buf1 buf1.c
Root @ qingcheng-virtual-machine :~ #./Buf1
1---1 2---2 3---3
4--4
This is because stdout is a row buffer and stderr is not a buffer.
Author "pstary"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.