You have a race condition due to your flock being cancelled when you 
close the file.

A open IN
A flock IN
A open OUT (truncates $tmp_db)
A while loop
B blocks
A close IN
B open IN
A close OUT
B open OUT (truncates $tmp_db)
A unlinks main_db
A tries to move $tmp_db to $main_bd, but $tmp_db is empty.

If you're married to using flock, you can flock a semaphore file for the 
whole operation:

sub Update_db {
   $main_db = $_[0];
   $tmp_db = $_[1];
   $update = $_[2];
   $lock_file = 'somefile';
   open LOCK, ">$lock_file";
   flock( LOCK, LOCK_EX ) or print "Unable to acquire lock: $!. 
Aborting";  #ps, this is a blocking call, it shouldnt fail
   open IN, "<$main_db" or print "Can't open $main_db: $!\n";
   open OUT, ">$tmp_db" or print "Can't open temporary file $tmp_db: 
$!\n";
   while ( <IN> ) {
     ($name, $team, $location)= split(/\|/, $_);
     next unless $name eq $update;
     $new_location = "palm_bay";
     $_ = join( "|", $name, $team, $new_location);
   }
   continue {
     print OUT $_ or print "Error writing $tmp_db: $!\n";
   }
   close IN;
   close OUT;
   unlink $main_db;
   rename $tmp_db, $main_db or print "Can't rename '$tmp_db' to 
'$main_db':$!\n";
   flock(LOCK, LOCK_UN);
   close(LOCK);
}

Otherwise you consider using a semaphore (perldoc IPC::Semaphore).


On Wednesday, July 17, 2002, at 08:31 PM, Jeff wrote:

> John,
>
> unix,
>
> sub Update_db {
>   $main_db = $_[0];
>   $tmp_db = $_[1];
>   $update = $_[2];
>   open IN, "<$main_db" or print "Can't open $main_db: $!\n";
>   flock( IN, LOCK_EX ) or print "Unable to acquire lock: $!. Aborting";
>   open OUT, ">$tmp_db" or print "Can't open temporary file $tmp_db: 
> $!\n";
>   flock( OUT, LOCK_EX ) or print "Unable to acquire lock: $!. Aborting";
>   while ( <IN> ) {
>     ($name, $team, $location)= split(/\|/, $_);
>     next unless $name eq $update;
>     $new_location = "palm_bay";
>     $_ = join( "|", $name, $team, $new_location);
>   }
>   continue {
>     print OUT $_ or print "Error writing $tmp_db: $!\n";
>   }
>   close IN;
>   close OUT;
>   unlink $main_db;
>   rename $tmp_db, $main_db or print "Can't rename '$tmp_db' to 
> '$main_db':
> $!\n";
> }
>
>
> -----Original Message-----
> From: John W. Krahn [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, July 17, 2002 8:11 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Flat File Db
>
>
> Jeff wrote:
>>
>> I'm use flat files to manage a list containing approx 25,000 records.  
>> For
>> updates, I write to a temp file then unlink main and rename temp file 
>> to
>> main.  I use flock for both temp and main files during update.  My main
> file
>> gets blown away on occasions.  What gives?  I can't figure out why this
>> happens.  Thanks for any help!
>
> OS?  code?
>
>
> John
> --
> use Perl;
> program
> fulfillment
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
// George Schlossnagle
// Principal Consultant
// OmniTI, Inc          http://www.omniti.com
// (c) 240.460.5234   (e) [EMAIL PROTECTED]
// 1024D/1100A5A0  1370 F70A 9365 96C9 2F5E 56C2 B2B9 262F 1100 A5A0


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to