pjf at perltraining.com.au
Sun Jan 20 18:47:34 CST 2002
On Mon, Jan 21, 2002 at 11:01:23AM +1100, Andrew Gray wrote:
> I now have another more generic question.
> What is the 'best' way to implment file locking to a multiple access file,
> from perl.
If you're doing the access over NFS, then despair. NFS makes
file locking exceedingly difficult.
If you're using a regular filesystem, then the usual method
of file-locking is using Perl's inbuilt "flock" function
("perldoc -f flock" for details):
use Fcntl ':flock'; # For LOCK_* constants
flock(FILEHANDLE,LOCK_EX); # Exclusive lock
flock(FILEHANDLE,LOCK_SH); # Shared lock
flock(FILEHANDLE,LOCK_UN); # Unlock
By default, flock will block until the requested lock is obtained.
If you need non-blocking lock requests, then you can bitwise-or
your request with LOCK_NB, like this:
Of course, with non-blocking locks you have to check the return
status to see if your lock was granted. (You should be
doing this regardless.)
Be aware that flock provides advisory locks. Other processes
can choose to ignore your locks if they don't want to play
by the rules.
Paul Fenwick <pjf at perltraining.com.au> | http://perltraining.com.au/
Director of Training | Ph: +61 3 9354 6001
Perl Training Australia | Fax: +61 3 9354 2681
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 232 bytes
Desc: not available
Url : http://mail.pm.org/archives/melbourne-pm/attachments/20020121/1c304d64/attachment.bin
More information about the Melbourne-pm