*BSD News Article 70605


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!news.uwa.edu.au!disco.iinet.net.au!news.uoregon.edu!newsfeed.internetmci.com!chi-news.cic.net!news.cais.net!mr.net!news.mr.net!dawn.mmm.com!news
From: damercer@mmm.com (Dan Mercer)
Newsgroups: comp.unix.programmer,comp.os.linux.development.apps,comp.unix.solaris,comp.unix.bsd
Subject: Re: How to flush write buffer ?
Followup-To: comp.unix.programmer,comp.os.linux.development.apps,comp.unix.solaris,comp.unix.bsd
Date: 10 Jun 1996 16:00:57 GMT
Organization: ClearSystems, Inc
Lines: 75
Distribution: inet
Message-ID: <4phgrp$3hd@dawn.mmm.com>
References: <4pg906$8su@sjx-ixn3.ix.netcom.com>
X-Newsreader: TIN [version 1.1 PL9]
Xref: euryale.cc.adfa.oz.au comp.unix.programmer:38299 comp.os.linux.development.apps:17415 comp.unix.solaris:71413 comp.unix.bsd:16800

Hugh McCurdy (hmccurdy@ix.netcom.com) wrote:
: Mike Humski <mhk@rahul.net> wrote:


: >Could some one let me know how to flush the write buffer of kernel?

: >I know you can flush the buffer at each call of "write" by selecting
: >O_SYNC as the second argumnet of "open" system call.

: >But what I would like to do is to flush the buffer when I want to.

: >Only possibility that I know of is "fcntl" with O_SYNC argumnet. But it
: >seems to be effective only from the next call of "write" and not to the 
: >currently pending buffer contents.

: >By the way I understand that fflush function of the standard lib does just
: >flushing from the lib's buffer to kernel, not from kernel's buffer.
: >Am I right ?

: >I wish to use this technique with Solaris2.x (SunOS5.x), SunOS4.x, BSDs, and
: >LINUX.

: (Let's hope I get this right).

: First, I'm not certain exactly what you mean.

: If you are trying to force the "unix" kernel to write its buffers to
: disk, you may use the sync() system call.

: sync() is process independent (everyone's buffers get flushed).
: Normally the process "update" performs a sync every 30 seconds.
: I'm not sure is sync() blocks or not...meaning I'm not sure if the
: buffers are flushed when sync returns.  sync() might just tell the
: kernel to do the flush.

: You might also want to look at fsync().  I'm not sure if this is
: available in all the platforms you mentioned.  fsync() appears to be a
: file specific sync(), which may be just what you are looking for.  I
: have never used fsync() in a program myself.  I think I'll try it
: soon.


: As for write(), write is a system call().  That means that when
: write() returns, the data is logically written to disk.  This means
: that the data is now available for another process to read the data.

No,  write returns when the data is written to the system buffers.  If
you want to make sure you don't get control back until the data is
written to disk,  you should use the synchronous write (swrite)
command.  This will,  of course,  greatly slow down yor app.

: The O_SYNC (or in some systems O_SYNCW) argument (hopefully) forces
: the write() call to block (not return) until the data has been
: physically written to the disk.  I think you knew this part.


: I think you understand fflush() by the way you phrased your question.
: fflush is part of the standard library.  But write() is the actual
: system call.  (Check the man pages, libraries in section 3 and system
: calls in section 2).


: Feel free to E-mail me if you have any questions about anything that
: I've said here.





: Hugh McCurdy


--
Dan Mercer
Reply To:  damercer@mmm.com

Opinions expressed herein are my own and may not represent those of my employer.