when to fflush ?

Stephen Clamage steve at taumet.com
Sat Jun 15 00:01:34 AEST 1991


siva at bally.Bally.COM (Siva Chelliah) writes:


>   I have a question.  Does seeking to the end of a file causes a flushing
>of the buffer ? In my opinion it should not.

It is an implementation issue, but I don't see why you would expect the
buffer NOT to be flushed in general upon a seek.  Consider the alternative:

When you write to a stdio FILE, the data you write must go to the current
file position.  If you then seek and do another write, that data must
go to the new file position.  If you seek and read, the read might
involve all or part of the buffered data.  This leaves the implementation
with two main choices:

1.  Always flush just before seeking.  This is always correct (satisfies
    all ANSI C requirements) and takes no extra bookkeeping.

2.  Keep track of where each buffered piece of data goes, and when you
    can't stand it any more, write out all the pieces you have saved.
    When you finally do a flush, you have to be careful to seek to each
    saved position and write the piece of buffer in the order in which the
    buffered write calls occurred from user code.  Each read must also be
    inspected to see whether it corresponds to any of the pieces of data
    saved up.  A read could involve several of the pieces, and these pieces
    might have overwritten one another.  When satisfying the read request,
    all this must be taken into account.  In other words, you must duplicate
    the functionality of the file system in your buffering scheme.

Which would YOU rather implement and verify for correctness?
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.unix.aix mailing list