Introduction to Linux Standard I/O Library cache policies

Source: Internet
Author: User

Standard Io library operations are performed around the stream. When we open a file through the fopen standard Io library function, we associate a file with an IO stream. Here we equate the IO stream with the file pointer file *, because all operations on the IO stream are implemented through the file * pointer.

We know that the purpose of introducing the standard Io library is to improve Io efficiency and avoid frequent read/write system calls. system calls consume a lot of resources. Therefore, the standard Io library introduces the IO cache. After accumulating a certain amount of Io data, it is then concentrated into the actual file to reduce system calls, thus improving Io efficiency. The standard I/O Library automatically manages internal caches without the intervention of programmers. However, it is precisely because we do not see the cache of the standard Io library that sometimes brings us some confusion. This section describes the Cache Policy of the standard I/O library.

I. The standard I/O cache-the standard output is used as an example: (this indicates the default value)

1) When stdout is connected to the terminal device, it will be cached, that is, when the standard Io library does not see a newline/N, the cache is refreshed (that is, the actual output operation is executed ). This feature can be verified by the following test code:

Int main ()
{
Printf ("this line shoshould be cached ...");
Sleep (3); // No output is visible on the terminal at this time
Printf ("/nthis line shocould be cached again"); // you can see the output of the first printf at this time, because the line break is refreshed.
Sleep (3); // at this time, only one line of output can be seen, but not the output of the second printf
Printf ("this line shocould not be cached again/N"); // you can see the output of the second and third printf at this time, because it is refreshed by the ending/n.
Sleep (3 );
Getchar ();
}

2) When stdout is redirected to a specific file, the standard output is fully cached, that is to say, the actual write operation will be performed only when the output cache is filled or fflush or fclose is called. The specific example is not provided here, you can use freopen to redirect stdout to a specific file for testing.

II. Stderr: To see the error information as soon as possible, the standard error does not contain any Cache

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/whrail/archive/2010/09/01/5856083.aspx

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.