Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-30-2004, 04:38 AM   #1
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
About buffers?


int main()

int nfd;

printf( "Hello world%d\n", getpid() );
// fflush( stdout );

if( fork() == 0 )

printf(" I am child \n" );
exit( 0 );



wait( );
printf(" I am parent \n" );



I f i execute the above code as below

$ ./a.out

hello world 2180
I am child
I am parent.

When i redirect the output to file its output is

$ ./a.out > temp

vi temp
hello world 2180
I am child.
hello world 2180
I am parent
If i remove the commants before fflush i am getting proper output

Why its behaving like this?
Please explain me.
Thanks in advance
Old 04-30-2004, 05:31 AM   #2
LQ Newbie
Registered: Oct 2002
Location: istanbul
Distribution: redhat
Posts: 16

Rep: Reputation: 0
standard formatted output is normally buffered and given that fork copies (almost) everything from parent to child it's natural to have that output...
..but under normal circumstances, as far as i know, printf is supposed to flush the output buffer with every newline character...and you are indeed using \n which means this is probably a bug in printf... (???)

try writing to stderr with fprintf..stderr is always flushed.
Old 04-30-2004, 06:13 AM   #3
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
the_dayi...there is no bug in printf...stdout is line, a \n will flush the buffer...

stderr is unbuffered..and files are fully buffered...
rest what you say is fine...
check out the Advanced programming in the Unix Env. by Stevens..

this thing is clearly mentioned in it....


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
UDP Buffers awksquawk Linux - Networking 1 11-18-2005 05:41 AM
[C++] buffers streams Ossar Programming 1 06-14-2005 01:27 PM
saving vi buffers frostillicus Linux - Newbie 5 04-19-2005 10:03 PM
mplayerplugin buffers to 99% then nothing happens vavoem Linux - Software 9 10-13-2004 02:03 AM
eth1: no RX buffers subhasis_ray Linux - Networking 1 11-11-2002 07:45 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:41 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration