Next | File Locking | 8 |
Consider this useful utility:
#!perl use Fcntl ':flock';
my $file = shift or die ...; open F, "< $file" or die ...; flock F, LOCK_EX or die ...; system($ENV{EDITOR}, $file);
But if the file is already locked, it just hangs silently
We'd like it to print a message instead
Solution:
unless (flock F, LOCK_EX | LOCK_NB) { warn "File $file already locked; waiting...\n"; flock F, LOCK_EX or die ... }
Or:
unless (flock F, LOCK_EX | LOCK_NB) { warn "File $file already locked; waiting...\n"; alarm 60; flock F, LOCK_EX or die ... }
Next | Copyright © 2003 M. J. Dominus |