However, if you naively use flock, you run the risk of getting exclusive access to the now-dead file, rather than the live file, so you need to verify that you really have the live file exclusively flocked. Here's a template to do that.
The checks for the device/inode number verify that the filehandle FILE is really the same as the file $file.
{ open FILE, $file or die; my ($dev,$ino) = stat(FILE); flock FILE, 2 or die; my ($dev1, $ino1) = stat($file); redo unless "$dev $ino" eq "$dev1 $ino1"; } open FILE_OUT, ">$file.tmp" or die; ### ... process FILE into FILE_OUT close FILE_OUT; rename "$file.tmp", "$file" or die; close FILE;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
$$, perhaps?
by gryng (Hermit) on Aug 22, 2000 at 21:35 UTC | |
by merlyn (Sage) on Aug 22, 2000 at 22:05 UTC | |
|
Re: Exclusively updating a file that continues to be repeatedly read
by halley (Prior) on Feb 18, 2004 at 18:05 UTC |