开发者

Strange Behaviour because of sleep()

I was just getting familiar with sleep(), i found that

#include<stdio.h>
int main()
{
int i=0;
printf("*********Testing Sleep***********\n");
for(i=0;i<10;i++)
{
    printf("%d",i);
    sleep(1);
}
return 0;

}

this does not print number per iter开发者_如何学JAVAation rather dumps all numbers when gets out of loop.... but when i modify printf...

#include<stdio.h>
int main()
{
int i=0;
printf("*********Testing Sleep***********\n");
for(i=0;i<10;i++)
{
    printf("%d\n",i);
    sleep(1);
}
return 0;

}

and now as i've added '\n' new line it works as expected... why it is behaving strangely in former one...


This is because the output buffer isn't being flushed (in other words, actually committed to the terminal). When you write a newline, the output buffer is more likely to be (but still not always, in some cases) flushed. Many terminal implementations do this to improve performance. To force the behaviour you want, you need to call fflush(stdout); after each printf call, like this:

#include<stdio.h>
int main()
{
int i=0;
printf("*********Testing Sleep***********\n");
for(i=0;i<10;i++)
{
    printf("%d",i);
    fflush(stdout);
    sleep(1);
}
return 0;
}


What you are looking at is line buffered output. Actually writing to output is an expensive operation, so I/O streams are usually buffered. Actually writing the buffer is deferred until a specific event is encountered. In standard C, you have three types of buffering:

  • fully buffered - the buffer is written when full.
  • line buffered - the buffer is written when a newline is encountered (your case).
  • unbuffered - the buffer is written whenever an I/O function is executed. (Good for error logging.)

Writing the buffer is called flushing. That's why there is a stdio function called fflush(). You might also want to check out setvbuf() and its parameter constants, _IOFBF, _IOLBF and _IONBF. I am sure you can figure out what they mean without looking them up. ;-)

Edit: This program delivers as you originally expected:

#include <stdio.h>

// This is the header where sleep() is declared. Don't go without it.
#include <unistd.h>

int main()
{
    int i=0;

    // setvbuf() can be called on a stream only BEFORE
    // you do any I/O on it!
    setvbuf( stdout, NULL, _IONBF, 0 );

    printf( "*********Testing Sleep***********\n" );
    for ( i = 0; i < 10; ++i )
    {
        printf( "%d", i );
        sleep( 1 );
    }
    return 0;
}


standard output for terminals is line buffered, output is not written unless there is a newline or you manually flush it.


Output is buffered, so that that the OS has an opportunity to optimize output speed. To make sure they are flushed immediately, do fflush (stdout);, but usually, you don't.


This is because printf() uses buffered output for better performance. Buffer is flushed to the console once \n is printed.


Printf is buffered.

You can force printf to 'flush' its buffer using the fflush call.

Or

Simply push the buffer to stdout using \n as in your case .

More detailed discussion is here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜