LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[Webdev] revisiting flock() for an example

[Webdev] revisiting flock() for an example

Lee Hosty hostyle at oscail.com
Tue May 1 11:03:29 IST 2001


At 01:05 01/05/01 +0100, Gabe Petrie wrote:

>[4] situations when using flock() is not necessary ?


 From c.l.p.m.

FWIW, here is an excerpt from open(2) on my RedHat 7.0 system:

        O_APPEND
               The  file  is  opened  in  append mode. Before each
               write, the file pointer is positioned at the end of
               the  file,  as if with lseek.  O_APPEND may lead to
               corrupted files on NFS file systems  if  more  than
               one  process  appends data to a file at once.  This
               is because NFS does  not  support  appending  to  a
               file,  so  the  client  kernel  has to simulate it,
               which can't be done without a race condition.

The key point is that the append mode isn't just "open the file and
start writing at the end".  Instead it is, "open the file and start
*every* write at the end".  This difference is what makes it safe to
append from multiple processes at the same time.

In addition to the NFS problem mentioned in the man page, there are at
least two additional problems:

1. If the write is larger than the buffer size, then it will possibly
be broken into multiple writes that could be interleaved with other
writes.

Now, this isn't a problem if you're writing single lines to a log
file, for example.  You can feel confident that each such write will
occur atomically and no corruption will occur.

2. It is OS-specific.  It may be the case that some OSes do not
provide this semantic for O_APPEND.  I'm honestly not sure how
prolific this functionality is.

Of course, flock isn't portable either, so using flock because you
can't trust the semantics of append mode is pretty silly.


If you want to use flock, use flock.  But don't think it is bringing
much additional robustness to the situation.

-- 
Ren Maddox
ren at tivoli.com


--------------------------------------------------------

ie. for appending (open FILEHANDLE, ">>$filename") small buffers on most 
OSes it doesnt cause corruption and therefore flock() is not necessary ...


Lee Hosty -x- hostyle at oscail.com -x- +353 (0)86 8768780
-- 
welcome to the Geophysical Inversion Facility ...





More information about the Webdev mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell