|Pathologically Eclectic Rubbish Lister|
Re: Proper use of flockby Aristotle (Chancellor)
|on Oct 30, 2002 at 00:38 UTC||Need Help??|
Start out with
use Fcntl qw(:flock);
Right after you open a file for reading, do flock FH, LOCK_SH or die "Failed acquiring shared lock on $filename: $!";
If you want to write to a file, do flock FH, LOCK_EX or die "Failed acquiring ex. lock on $filename: $!";
DO NOT unlock the file, ever. Just close it. If you want to read from a file and then also write to it, then open it for reading and writing once, and get a shared lock. Once you want to write to it, upgrade to an exclusive lock by calling flock on the file handle again - do not close and reopen it. Don't downgrade that lock to a shared one either.
See perldoc -f flock for help on the function.
Makeshifts last the longest.