About row buffering ~

Source: Internet
Author: User
Tags sleep function

Today, we can see a friend of the C version of Cu code and the output result.
# Include <stdio. h>

Main ()
{
Printf ("1 .");
Sleep (2 );
Printf ("2 .");
Sleep (2 );
Printf ("3 .");
Sleep (2 );
Printf ("4 .");
Printf ("OK/N ");
}
% GCC test. c
%./A. Out
1.2.3.4. OK

Sleep didn't play a role, but these 1.2.3.4 will be output together after a while.
I tried it. This is indeed the case. It was compiled by GCC.
Let's look at the arguments of the high people in the downstairs. The system will output the data of a row together, instead of printing a little bit. Haha
The standard output has row buffering. There are several solutions:
I added a line feed to each backend, so the problem would be gone.
[Ktktkt @ Jintao common] $ cat 1.c
# Include <stdio. h>

Int main ()
{
Printf ("1/N ");
Sleep (2 );
Printf ("2/N ");
Sleep (2 );
Printf ("3/N ");
Sleep (2 );
Printf ("4/N ");
Sleep (2 );
Printf ("5/N ");
Sleep (2 );

Return 0;
}
In this way, with a line feed, the output will not be output together :)
[Ktktkt @ Jintao common] $./1
1
2
3
4
5

Another solution is to force Refresh after each line when no/N is changed.
[Ktktkt @ Jintao common] $ cat 1.c
# Include <stdio. h>

Int main ()
{
Printf ("1 ");
Fflush (stdout );
Sleep (2 );
Printf ("2 ");
Fflush (stdout );
Sleep (2 );
Printf ("3 ");
Fflush (stdout );
Sleep (2 );
Printf ("4 ");
Fflush (stdout );
Sleep (2 );
Printf ("5 ");
Fflush (stdout );
Sleep (2 );

Return 0;
}

The result is as follows:
[Ktktkt @ Jintao common] $./1
1 2 3 4 5
It is indeed two seconds of output, pretty cool ~
This small tip is quite enjoyable ~

However, the problem is that when you specify-wall during compilation, the following prompt will still be displayed:
No sleep function?
[Ktktkt @ Jintao common] $ GCC 1.C-O 1-wall
1. C: In function 'main ':
1. C: 7: Warning: Implicit declaration of function 'sleep'

Man.
# Man 3 sleep
It is found that it is in unistd. H, so add this line and compile again, no problem :), the Code becomes as follows:

# Include <stdio. h>
# Include <unistd. h>

Int main ()
{
Printf ("1 ");
Fflush (stdout );
Sleep (2 );
Printf ("2 ");
Fflush (stdout );
Sleep (2 );
Printf ("3 ");
Fflush (stdout );
Sleep (2 );
Printf ("4 ");
Fflush (stdout );
Sleep (2 );
Printf ("5 ");
Fflush (stdout );
Sleep (2 );

Return 0;
}
In this way, the compilation is successful...

Explanation of FLW boss:
There are several solutions:
1. Use printf ("1/N") instead. However, this method is sometimes unwilling by the author.
2. Use fprintf (stderr, "1") instead. Because stderr is not a row buffer, you can do this. I often use this method, but this method requires special processing when outputting redirection. In other words, it does not conform to certain rules for shell interaction. The developed program does not conform to the shell filter habits.
3. Use printf ("1/N") + fflush (stdout) instead. This is the only method that does not affect the author's original intention, but can produce the expected results. Strictly speaking, this is the only correct method, because this method will not affect anyone or anything.
4. Modify the attributes of your stdout directly by some means to make it non-row buffering, such as calling IOCTL or using commands provided by the operating system ......

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.