There is a race condition with > and >> , but it can be circumvented with +< .
# > open(my $fh, '+<', $qfn) or die $!; flock($fh, LOCK_EX) or die $!; truncate($fh, 0) or die $!; ...
There is also a race condition in the scenario you are describing.
Writer Reader ========================= ========================= - opens file - opens file - locks file - obtains lock on file - locks file [blocks] - reads the file [empty] - closes and unlocks file - obtains lock on file - writes to file - writes to file - closes and unlocks file
A common strategy to avoid this problem is to have a writer
- create a file in a temporary directory and then
rename file to a directory that the reader monitors when the file is completed.
rename is an atomic action, so the file will be completely formed in the directory that the reader is viewing. This requires the cooperation of a writer, but better solutions will be.
source share