Linux buffer output problems

Source: Internet
Author: User
I saw such a piece of code on the Internet about the Linux buffer output problem. The code itself is problematic. I looked at it and thought it was okay. The result is that I compiled and ran www.2cto.com, and an error is indeed returned, I checked the buffer information in linux, shared it with you, and finally discussed this section... I saw such a piece of code on the Internet about the Linux buffer output problem. The code itself is problematic. I looked at it and thought it was okay. The result is that I compiled and ran www.2cto.com, and an error is indeed returned, I checked the buffer information in linux, shared it with you, and finally discussed the problem with this code. when a program runs the output, is it necessary to immediately display the output to the user? For example, if a program is output to the terminal, ask the end user to answer the question, or give a prompt to the user, that is, let the user specify what content should be typed. at this time, the output of the program should be immediately displayed to the user. Another case of www.2cto.com is that the program outputs to a file, as long as all the final results are output to the file before the program ends. To sum up, there are two methods for program output: one is immediate processing, the other is cache first, and then a large number of writes, that is, the buffer method we call. Compare the advantages and disadvantages of the two methods. Immediate access often results in a high system burden, because the disk IO is generally slow; the buffer mode allows us to save the content to be written to a buffer before writing data to a file or disk, and then write the buffer content to a file after the buffer overflow, this reduces the number of disk I/O operations and improves the system efficiency. How big is the buffer set? The C language allows programmers to control the output data volume, that is, the size of the buffer can be customized, which is achieved through the setbuf () function. For example, the setbuf (stdout, buf) in the following code segment is to buffer the content to be written to the standard output to the buf. when the buf overflows, it is written to the stdout, and then the buf is cleared. The loop ends until the end. # Include Main () {int c; char buf [BUFSIZ]; setbuf (stdout, buf); while (c = getchar ())! = EOF) putchar (c);} where is the code disconnection problem? When the buffer overflow occurs, the buffer content is written to the file and then cleared. When does the last buffer clearance occur? The answer is that after the main () function returns, the main function does not return to the end of the program immediately, and the program must clear the c runtime library before returning control to the operating system, the buffer zone buf is cleared at this time. Because buf is a local variable defined in main, when buf is cleared, the main () function has already returned, and the buf cannot be found, so the program has an error. This is also an error-prone part of the C language. There are two solutions: 1. declare buf as static and Global: static char buf [BUFSIZ] # include Main () {int c; static char buf [BUFSIZ]; setbuf (stdout, buf); while (c = getchar ())! = EOF) putchar (c);} 2. use the malloc function to dynamically allocate buf memory space: setbuf (stdout, malloc (BUFSIZ). do not worry about memory allocation failure of the malloc function. if the malloc function fails, NULL is returned, and the buffer is disabled, there are three caching methods in linux: row buffering, full buffering, and no buffering # include # Include Main () {int c; char buf [BUFSIZ]; setbuf (stdout, malloc (BUFSIZ); while (c = getchar ())! = EOF) putchar (c );}

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.