LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: 15
About buffers?


#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>

int main()
{

int nfd;

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

if( fork() == 0 )
{

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

}

else
{

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
Bye,
Eshwar.
 
Old 04-30-2004, 04:31 AM   #2
the_dayi
LQ Newbie
 
Registered: Oct 2002
Location: istanbul
Distribution: redhat
Posts: 16

Rep: Reputation: 0
well,
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, 05:13 AM   #3
shishir
Member
 
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 buffered..ie, 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....
 
  


Reply



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 04:41 AM
[C++] buffers streams Ossar Programming 1 06-14-2005 12:27 PM
saving vi buffers frostillicus Linux - Newbie 5 04-19-2005 09:03 PM
mplayerplugin buffers to 99% then nothing happens vavoem Linux - Software 9 10-13-2004 01:03 AM
eth1: no RX buffers subhasis_ray Linux - Networking 1 11-11-2002 06:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:13 PM.

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