Re: Correct file locking techniques
From: Bob Walton (invalid-email_at_rochester.rr.com)
Date: 06/27/04
- Next message: Purl Gurl: "Re: Bokma"
- Previous message: Shalini Joshi: "Re: repeated calculations everyday"
- In reply to: Robert TV: "Correct file locking techniques"
- Next in thread: Ben Morrow: "Re: Correct file locking techniques"
- Reply: Ben Morrow: "Re: Correct file locking techniques"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 27 Jun 2004 02:01:17 GMT
Robert TV wrote:
> Hi, I am asking foir advice on which form of code i've written below is the
> correct and safe way to lock a file. I've done some reading on" use Fcntl
I have found that the most portable and least hassle way to lock files
is to use a lock file. This is a file which is used exclusively for the
purpose of establishing a lock -- it can be empty, or have any contents
you want, but the data in the lock file would be unused and unimportant.
This technique is particularly useful when doing stuff which is a bit
out of the ordinary, such as locking DBM-type files to which you wish to
tie a hash, for example.
If you want to write a file, open the lock file for write (that will, at
least on some systems, wipe out the contents of the lock file), then
establish an exclusive lock on it. Then go do whatever you wanted the
exclusive lock for (write another file, tie a hash to a DBM-type file,
or any other operation requiring exclusive access to a resource). Then,
when you are all done with your lock, close the lock file.
If you want to read a file, open the lock file for reading, and
establish a shared lock on it. Go read your data file or do whatever
you wanted the shared lock for. When done, close the lock file.
To the best of my knowledge, that works flawlessly on at least local
files on every OS that supports locking (that, by the way, rules out
Windoze 95/98/98SE). However, you should carefully and thoroughly read
everything your OS has to say about file locking, particularly if you
are considering locking files accessed via a network.
You will want to note that locking a file does not prevent other
processes from read/writing/deleting/whatevering a file. *All* it does
is establish a lock -- the other processes have to cooperate with the
lock in order for it to work. An exclusive lock means other processes
seeking a lock will block until the exclusive lock is released; a shared
lock means other processes seeking an exclusive lock will block until
all the shared locks are released.
I find it convenient to write a couple of subs to do the locking, like
[untested]:
sub lockex{
open LOCK,">lockfile.txt"
or die "Oops, couldn't open lockfile.txt for write, $!";
flock(LOCK,LOCK_EX)
or die "flock bombed out in lockex, $!";
}
sub locksh{
open LOCK,"lockfile.txt"
or die "Oops, couldn't open lockfile.txt for read, $!";
flock(LOCK,LOCK_SH)
or die "flock bombed out in locksh, $!";
}
sub unlock{
close LOCK or die "Oops, couldn't close lockfile.txt, $!";
}
HTH.
...
> Robert
-- Bob Walton Email: http://bwalton.com/cgi-bin/emailbob.pl
- Next message: Purl Gurl: "Re: Bokma"
- Previous message: Shalini Joshi: "Re: repeated calculations everyday"
- In reply to: Robert TV: "Correct file locking techniques"
- Next in thread: Ben Morrow: "Re: Correct file locking techniques"
- Reply: Ben Morrow: "Re: Correct file locking techniques"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|