Detailed explanation of standard I/O Buffer

Source: Internet
Author: User

The standard I/O Library provides buffer to minimize the number of Read and Write calls. It also automatically performs buffer management for each I/O Stream, thus avoiding the trouble that the application needs to consider this. Unfortunately, the most confusing thing about the standard I/O Library is its buffer.
Standard I/O provides three types of buffering:
1. Full buffer. In this case, the actual I/O operation is performed only after the standard I/O buffer is filled. Files that reside on the disk are usually fully buffered by the standard I/O library. When a stream performs the first I/O operation, the related standard I/O functions usually call malloc to obtain the buffer to be used.
The write operation on the I/O buffer zone is described in terms of flushing. The buffer can be automatically rinsed by the standard I/O routine, or the fflush function can be called to fl a stream. It is worth noting that flush has two meanings in the Unix environment. In terms of the standard I/O library, flush thinks that this writes the content in the buffer to the disk. Flush indicates that data already stored in the buffer zone is discarded in the terminal driver.
2. Row buffering. In this case, when a line break is encountered in the input and output, the standard I/O Library performs the I/O operation. This allows us to output one character at a time, but the actual I/O operation is performed only after a line is written. When a stream involves a terminal, it usually uses row buffering.
There are two restrictions on Row buffering. First, because the buffer length used by the standard I/O library to collect each row is fixed, as long as the buffer is filled up, I/O operations are performed even if no line break is written. Second, any time as long as the standard I/O Library requests the input data from the stream buffered by a bag or the stream buffered by B rows, then, the output stream is buffered for all rows. The reason that a description in parentheses is included in B is that the required data may already be in the buffer zone and does not need to be read from the kernel when data is needed. Obviously, data is obtained from the kernel from input requests in a stream without buffering.
3. No buffer. The standard I/O Library string is buffered for storage. For example, if the iaozhun I/O function fputs is used to write 15 characters to a stream without buffering, this function is likely to use the Write System to call the function to immediately write these characters to the associated open file.
Standard Error slide stderr is usually not buffered, so that the error information can be displayed as soon as possible, regardless of whether they contain a line break.


Iso c requires the following buffer features:
When and only when the standard input and standard output do not involve interactive devices, they are fully buffered.
The standard will not enable full buffering.
However, this does not tell us if the standard input and standard output involve interactive devices, whether they are buffered or line-buffered; and the row buffer is not included in the standard error. By default, the system uses the following types of buffering:
Standard errors are not buffered.
If it involves other streams of terminal devices, they are buffered; otherwise, they are fully buffered.
For any given stream, if we do not like the default situation of these systems, we can call one of the following functions to change the buffer type:
Void setbuf (File * restrict FP, char * restrict BUF)
Int setvbuf (File * restrict FP, char * restrict Buf, int mode, size_t size)

Let's look at a small example.
Source program:

[root@happy src]# cat simplefork.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int globa = 4;

int main (void )
{
        pid_t pid;
        int vari = 5;

        printf ("before fork\n" );

        if ((pid = fork()) < 0){
                printf ("fork error\n");
                exit (0);
        }

        else if (pid == 0){
                globa++ ;
                vari--;
                printf("Child changed\n");
        }
        else
                printf("Parent did not changde\n");

        printf("globa = %d vari = %d\n",globa,vari);
        exit(0);
}

Execution result:
Output to standard output
[Root @ happy bin] #./simplefork
Before fork
Child changed
Globa = 5 vari = 4
Parent did not Changde
Globa = 4 vari = 5

When redirecting to a file, before fork outputs both sides
[Root @ happy bin] #./simplefork> temp
[Root @ happy bin] # Cat temp
Before fork
Child changed
Globa = 5 vari = 4
Before fork
Parent did not Changde
Globa = 4 vari = 5

When the analysis runs the program directly, the standard output is a row buffer, and the new row is quickly washed away. After redirection, the standard output is fully buffered. When fork is called, The before fork line is still saved in the buffer and copied to the sub-process buffer along with the data segment. In this way, this line enters the output buffer of the Parent and Child processes respectively, and the remaining output is followed by this line.

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.