?
Solved

IO Buffering...

Posted on 2011-10-09
3
Medium Priority
?
272 Views
Last Modified: 2012-05-12
I have a question on I/O buffering, can somebody help me out:

int main() {
        int c;
       
        printf("Hello World from PID=%d.",getpid());        
        sleep(10);
        printf("Hello Again, World! from PID=%d.\n",getpid());
        return 0;
}

the above program sleeps for 10 sec and then print out's the printf's.

Whereas, the one below print's out the statement before new line and sleeps for
10 seconds. I want to understand why is that ?

#include <stdio.h>

int main() {
        int c;
        printf("Hello World from PID=%d.\n",getpid());
        sleep(10);
        printf("Hello Again, World! from PID=%d.\n",getpid());
        return 0;
}

Thanks
0
Comment
Question by:tpat
  • 2
3 Comments
 
LVL 4

Expert Comment

by:stachenov
ID: 36940483
Console output (to a screen) is usually line-buffered, meaning that a line is output when you finish it, meaning that the "\n" is the key here. Note that this is usually not true when redirecting output into a file - you should fflush(stdout) if you want to see data appearing in that file immediately, regardless of newlines.
0
 
LVL 4

Expert Comment

by:stachenov
ID: 36940487
By the way, it's all platform-dependent too. I just tested it in Windows and it prints the first line right away regardless of flushing. On Linux, it prints it only if there is a newline or if I fflush(stdout) after the first printf().
0
 
LVL 35

Accepted Solution

by:
Duncan Roe earned 2000 total points
ID: 37005517
The default behaviour is:
- when output is to a tty, it is line bufferred
- when output is to a file (including to a pipe) it is file system block bufferred.
I know of 2 ways to override the default behaviour:

1. Hard Way

insert a call to fflush() after every call to printf()

2. Easy Way

Insert a call to setvbuf() at the front of your program, e.g. for unbuffered stdout
setvbuf(stdout,NULL,_IONBF,0)

Open in new window

Enter man setvbuf in a command window to see all possibilities
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question