There's something I never quite understood about file locking with flock:
Isn't there theoretically a tiny window of opportunity between when you open the file and when you lock it, to where another process might want to open the file and read/write to it?
I've always just used lockfiles, such as:
So does flock have a slight possibility of defeating its own purpose, or is the nanosecond of time between opening and locking too insignificant and statistically unlikely to be intercepted by another process to even worry about it?
-------------
Cuvou.com | The NEW Kirsle.net
Code:
use Fcntl qw(:flock);
open (FH, "file.ext"); # open a file here
flock (FH, LOCK_EX); # lock it here
Isn't there theoretically a tiny window of opportunity between when you open the file and when you lock it, to where another process might want to open the file and read/write to it?
I've always just used lockfiles, such as:
Code:
my $i = 0;
while (-f "lockfile.lck") {
sleep 1;
$i++;
die "Can't gain access to the lock" if $i > 30;
}
open (LCK, ">lockfile.lck");
print LCK "it's mine now, suckas!";
close (LCK);
open (FILE, "the_file.txt");
my @data = <FILE>;
close (FILE);
# manipulate
open (FILE, ">the_file.txt");
print FILE join ("\n",@data);
close (FILE);
# unlock
unlink "lockfile.lck";
So does flock have a slight possibility of defeating its own purpose, or is the nanosecond of time between opening and locking too insignificant and statistically unlikely to be intercepted by another process to even worry about it?
-------------
Cuvou.com | The NEW Kirsle.net