|
Home > Archive > Unix Programming > May 2005 > flushing the output
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
flushing the output
|
|
| junky_fellow@yahoo.co.in 2005-05-17, 2: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 ?
| |
| Michael B Allen 2005-05-17, 2: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
| |
| Paul Pluzhnikov 2005-05-17, 2: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.
| |
| Stephane Zuckerman 2005-05-17, 7:48 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.
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)
|
|
|
|
|