flushing the output
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > flushing the output




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    flushing the output  
junky_fellow@yahoo.co.in


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-17-05 07:49 AM

I am using fprintf to write the output to "stdout".
It works fine.
However when I redirect the output to some file at the
command prompt, the output in the file appears after few
seconds. However, if put fflush after the fprintf it works
fine. Why the output appears instantly on the stdout
but delayed by few seconds when I redirect the output to
some file ?






[ Post a follow-up to this message ]



    Re: flushing the output  
Michael B Allen


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-17-05 07:49 AM

On Mon, 16 May 2005 23:12:37 -0700, junky_fellow wrote:

> I am using fprintf to write the output to "stdout".
> It works fine.
> However when I redirect the output to some file at the
> command prompt, the output in the file appears after few
> seconds. However, if put fflush after the fprintf it works
> fine. Why the output appears instantly on the stdout
> but delayed by few seconds when I redirect the output to
> some file ?

How do you know it's not in the file?

IOW what are you using to look at the output file?

Mike






[ Post a follow-up to this message ]



    Re: flushing the output  
Paul Pluzhnikov


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-17-05 07:49 AM

junky_fellow@yahoo.co.in writes:

> Why the output appears instantly on the stdout
> but delayed by few seconds when I redirect the output to
> some file ?

Because by default ouput to a terminal via stdio routines is
line-buffered, but output to a file is block-buffered.

Try "man setvbuf" and/or read the APUE.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.





[ Post a follow-up to this message ]



    Re: flushing the output  
Stephane Zuckerman


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-17-05 12:48 PM

> I am using fprintf to write the output to "stdout".
> It works fine.
> However when I redirect the output to some file at the
> command prompt, the output in the file appears after few
> seconds. However, if put fflush after the fprintf it works
> fine.

That is a normal behaviour. The fprintf() function uses a buffer to let
the system choose when to write data. When calling fflush(), you're asking
the system to force all the data waiting to be written to ... be written,
"now" (of course, the system decides whether "now" is really right now,
or if the task must wait a little bit). :-)

There are various buffers within the system. If I understand correctly how
things work, you have a buffer for your data waiting to be written, but
you also have at least one more buffer for the disk writings. So when
you're outputing to the console, everything's fine, because the time you
have to wait is quite short (but if you were using multi-{task,thread}
programming, this probably wouldn't be so), whereas with the disk, you
have a "physical writing" to be performed in addition to the "normal" one.

--
"Je deteste les ordinateurs : ils font toujours ce que je dis, jamais ce
que je veux !"
"The obvious mathematical breakthrough would be development of an easy
way to factor large prime numbers." (Bill Gates, The Road Ahead)





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 05:41 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register