Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

RE: File Locking

by KM (Priest)
on May 22, 2000 at 07:15 UTC ( [id://14137]=note: print w/replies, xml ) Need Help??

in reply to File Locking

Your examples on file locking to avoid race conditions all have race conditions themselves. Let's go over some of what you wrote:

First, some people do say 'eff-lock' :) Most things looked ok until the All about flock section. The concepts of LOCK_* is somewhat explained, but these values are constants which should be retrieved from the system header file which defines them. You should not define them in the script or create subroutines to do this. On an aside, you showed:

sub LOCK_SH { 1 } ## shared lock
sub LOCK_EX { 2 } ## exclusive lock
sub LOCK_NB { 4 } ## non-blocking
sub LOCK_UN { 8 } ## unlock
If you want to define a constant, you should include the prototype () like:

sub LOCK_SH () { 1 } ## shared lock

That's one way to make a constant, but.. back to the show..

One of the best ways to get these values is with the Fcntl module like:

use Fcntl qw(:flock);

This will import your systems constants for those values. The next example given contains a race condition. Actually, the way you are explaining to get around a race condition, is really also a race condition. Not to worry, many people make this mistake and think that a simple flock() will make everything watertight. One of the problems is that the system itself may not finish writing to the file when the actual lock is released (remember, locks are only advisory, a file isn't actually frozen in some way), and another process reads the file before the write is comeplete. This may be a rare occasion, however. Another problem can occur when another process updates the file between the time the file is opened, and the lock is obtained. This is ano

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://14137]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-06-17 12:41 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.