*BSD News Article 70566


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!news.rmit.EDU.AU!news.unimelb.EDU.AU!munnari.OZ.AU!spool.mu.edu!howland.reston.ans.net!swrinde!tank.news.pipex.net!pipex!usenet2.news.uk.psi.net!uknet!dispatch.news.demon.net!demon!netcom.net.uk!ix.netcom.com!news
From: hmccurdy@ix.netcom.com (Hugh McCurdy)
Newsgroups: comp.unix.programmer,comp.os.linux.development.apps,comp.unix.solaris,comp.unix.bsd
Subject: Re: How to flush write buffer ?
Date: Mon, 10 Jun 1996 04:39:53 GMT
Organization: Netcom
Lines: 66
Distribution: inet
Message-ID: <4pg906$8su@sjx-ixn3.ix.netcom.com>
References: <4pg43b$hru@samba.rahul.net>
NNTP-Posting-Host: syr-ny3-41.ix.netcom.com
X-NETCOM-Date: Sun Jun 09  9:40:38 PM PDT 1996
X-Newsreader: Forte Free Agent 1.0.82
Xref: euryale.cc.adfa.oz.au comp.unix.programmer:38274 comp.os.linux.development.apps:17374 comp.unix.solaris:71345 comp.unix.bsd:16796

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.


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