Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old August 20th, 2000, 03:07 PM
focus focus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Location: Luxembourg
Posts: 87 focus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 55 sec
Reputation Power: 8
Hi

I use flock DB,2 while running an archiving routine, but other routines - also using flock DB,2 - elsewhere in the application are writing to the file before the archiving is completed so I get lost / corrupted data.

Do I have to include use:flock or something to make flock work or is it automatically available to my perl scripts?

Or what else could be happening?

Some details:-

"reservations.txt" file has some 2500 records. The script extracts "expired" records and writes them to history.txt, while extracting and writing "current" records to "resback.txt" file. It then renames "resback.txt" to "reservations.txt".

Script
------
I inserted some "sleeps" to permit actions time to complete. The $newname.txt is a backup file as I need to recover lost data so often.

open(DB, "data/reservations.txt") or die "Error opening reservations file: $!n";
flock DB,2;

open(ARCHIVE,">>data/history.txt") or die "Error opening archive file: $!n";
flock ARCHIVE,2;

open(TEMP,">data/resback.txt") or die "Error opening temp file: $!n";
flock TEMP,2;

$newname = time();
open(BACKUP,">data/$newname.txt") or die "Error opening backup file: $!n";
flock BACKUP,2;

while(<DB> ){
($delkey,$gamedate,$court,$start,$player,$partner,$res,$slotgame)= split(/ /, $_);
print BACKUP $_ ;

if(($gamedate < $thisdate)| |(($gamedate < $tomoro)&&($start < $thour))){
print ARCHIVE $_ ;
}else{
print TEMP $_ ;
}
}
sleep 10;

flock ARCHIVE,8;
close(ARCHIVE);

if(-e "data/resback.txt"){
unlink("data/reservations.txt") | | die "Could not write from backup! $!";
sleep 10;
rename "data/resback.txt", "data/reservations.txt")| | die "Could not rename temp file! $!";
sleep 10;
}else{
$message = "Error ! resback does not exist";
require "error_notify.cgi";
}
flock DB,8;
close(DB);

flock BACKUP,8;
close(BACKUP);

Reply With Quote
  #2  
Old August 20th, 2000, 11:41 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Your script doesn't need to use "flock" at all. While running your script, no other scripts will write to history.txt, resback.txt and $newname.txt.

Reply With Quote
  #3  
Old August 21st, 2000, 02:19 AM
focus focus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Location: Luxembourg
Posts: 87 focus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 55 sec
Reputation Power: 8
I didn't express myself clearly.
It's the reservations.txt file which is being written to by other routines

Reply With Quote
  #4  
Old August 21st, 2000, 03:08 AM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
>>which is being written to by other routines

which is?

Also,
rename "data/resback.txt", "data/reservations.txt")| | die "Could not rename temp file! $!";

why you placed this before the flock DB,8;?
"8" is "unlock" and you were trying to overwrite the existing reservations.txt by resback.txt?

Reply With Quote
  #5  
Old August 21st, 2000, 03:49 AM
focus focus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Location: Luxembourg
Posts: 87 focus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 55 sec
Reputation Power: 8
Hi freebsd

>you were trying to overwrite the existing >reservations.txt by resback.txt?

Correct.
So I move the flock DB,8 to after the overwrite?
What about the flock ARCHIVE,8 ? Is this redundant as the file name has changed?

But how do I truly lock reservations.txt to prevent other routines ("reserve", "cancel reservation", "modify an existing reservation") from modifying the file until the archiving and renaming is completed?

Thanks for any help, as my client is more than upset at the data loss (!)

Reply With Quote
  #6  
Old August 21st, 2000, 03:54 AM
focus focus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Location: Luxembourg
Posts: 87 focus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 55 sec
Reputation Power: 8
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by freebsd:
Your script doesn't need to use "flock" at all. While running your script, no other scripts will write to history.txt, resback.txt and $newname.txt.[/quote]

Well, the archive run is triggered by a player making a menu selection - to clear all played games (as they can only make a limited number of bookings, so his played games must be removed to show a current allocation). Hence a second player entering the system can trigger the archive run while it is running for the first player. I thought flocking would prevent any simultaneous runs.

Reply With Quote
  #7  
Old August 21st, 2000, 04:21 AM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
>>So I move the flock DB,8 to after the overwrite?

Yes. In fact, you just need to flock DB, no others.

>>But how do I truly lock reservations.txt to prevent other routines

Simply flocking DB, that is it. All others are redundant. Also remove all the "sleep".

>>I thought flocking would prevent any simultaneous runs

Check here -> http://www.devshed.com/Talk/Forums/Forum6/HTML/000349.html

Also,
1 - Shared
2 - Exclusive
4 - Don't block when locking
8 - Unlock

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > flocking not working?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway