Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What types of buffers are there?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what buffer types are there". In daily operation, I believe many people have doubts about what buffer types they have. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "what buffer types are there?" Next, please follow the editor to study!

Buffer

To reduce the number of read and write calls, the standard IO library provides buffering, and one might ask, why reduce the number of calls to them? It is clear that read and write are system calls, and they will take more time, which is not described in this article. So what are the three types of buffers?

Full buffering

In the case of full buffering, the actual Iripple O operation is not performed until the standard Iripple O buffer is filled. Writing disk files is usually fully buffered. For example:

# include # include int main (void) {/ * Open * / FILE * fp = fopen (". / test.txt", "w +") in a readable and writable manner; if (NULL = = fp) {perror ("open file failed"); return-1;} / * writes * / char buf [] = "wechat:shouwangxiansheng\ n"; fwrite (buf,sizeof (char), sizeof (buf), fp) / / fflush (fp); / * sleep for a period of time to observe * / sleep (20); fclose (fp); return 0;}

Open a file and write a string to it. We compile and run:

$gcc-o buff buff.c $cat test.txt wechat:shouwangxiansheng $. / buff

Observe the test.txt at this time:

$cat test.txt

Found that its content is empty! Why is there nothing when it has already been written? The reason is that it is fully buffered by default, so after the content is written to the file, it is not directly stored in the file. When the program closes the file or the program finishes running and exits, check again:

It is found that the file already has its contents. In addition to waiting for the program to run, you can also use the fflush function, which writes the contents of the buffer to disk (the terminal driver represents the data of the discarded buffer). So after removing the comment from the next line of fwrite, you can see that after writing, you can see the content directly in the file. So when you are writing a file, but looking at the file without writing anything, don't always doubt your code.

Row buffering

Line buffering means that when a newline character is encountered, or when the buffer is full (usually 1024 bytes), the standard I _ prime O library performs the I _ big O operation. Again, for example:

# include # include int main (void) {printf ("wechat:shouwangxiansheng"); sleep (10); return 0;}

Compile and run the above program:

$gcc-o lineBuff lineBuff.c $. / lineBuff

You will find that after the execution of the printf, the content is not immediately output to the terminal, but only after the program has been run. Of course, you know that if you want to print directly to the terminal, you only need to change it to the following:

Printf ("wechat:shouwangxiansheng\ n")

Without buffer

Its meaning can be understood literally. Again, for example:

NoBuff.c*/ # include # include int main (void) {fprintf (stderr, "wechat:shouwangxiansheng"); sleep (10); return 0;}

When you compile and run, you will find that after running the fprintf statement, the content is output directly to the terminal without waiting for a line break. Generally speaking, standard errors are unbuffered.

Summary

Through some of the above examples, we have also found some rules:

Usually the files on disk are fully buffered

Standard input and standard input are usually line buffered

A stream pointing to an end device is usually a line buffer, while when pointing to a file, it is fully buffered

In order to display error messages as much as possible, standard errors are unbuffered

At this point, the study of "what types of buffers are there" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report