in reply to Update in-place with temporary file and flocking

What about
rename "file1", "file2"; my $fh = new IO::File("file1","w") flock($fh,LOCK_EX) or die "can't get lock"; my $fh2 = new IO::File("file2","r"); #read fh2 write to $fh flock($fh,LOCK_UN); # perhaps delete file2
Boris

Replies are listed 'Best First'.
Re: Re: Update in-place with temporary file and flocking
by geohar (Acolyte) on Feb 18, 2004 at 16:52 UTC
    Sure, that's what I was going to try next. I was wondering though, If I could get away without the second read-write sequence. I wanted to just rename the 2nd file over the top...
      I can not see a second read-write sequence. The operations are the same for both examples.
      Boris
        I may have misread/understood your code, but in order to be able to open and lock file1 wouldn't it have to be:
        copy "file1", "file2"; # instead of move my $fh = new IO::File("file1","w") flock($fh,LOCK_EX) or die "can't get lock"; my $fh2 = new IO::File("file2","r"); #read fh2 write to $fh flock($fh,LOCK_UN); # perhaps delete file2
        With the rename, even if we could create a file1 to subsequently lock, wouldn't there'd be a small gap where a program could attempt to get read access to file1 and find it not there...