Gossamer Forum
Home : General : Perl Programming :

My counter script was broken when it increase to # > 6000 ? Why? Please help

Quote Reply
My counter script was broken when it increase to # > 6000 ? Why? Please help
Hi !

I wrote my own counter script - I know it's simple and maybe it likes "Sit" - I need help from an advance programmer who can tell me why the flat file (which hold the number inside) got number 6200 yesterday and sudden today it went down to 1500 ?

Here is my script :)

Code:


open (FILE, "<$count_log");
flock (FILE,2);
$count = <FILE>;
flock (FILE,8);
close (FILE);

open (FILE, ">$count_log");
flock (FILE,2);
$count2 = $count + 1 ;
print FILE $count2;
flock (FILE,8);
close (FILE);
What is the important thing do I need to concern to create the good counter (I mean Do not get damage the flat file)

Thanks for the help

N.A
Quote Reply
Re: [newage24] My counter script was broken when it increase to # > 6000 ? Why? Please help In reply to
Really don't find any logic the number will drop from 6200 to 1500!!
Quote Reply
Re: [newage24] My counter script was broken when it increase to # > 6000 ? Why? Please help In reply to
If that is the entire script, you probabaly had a race condition.

First, don't use the flock 8. you never need to release the flock... close() will do that automatically, at the atmoic level, alleviating a possible race condition between unlocking and closing.

Second, don't open the file twice...again, a race condition can occur. Instead open the file in read/write mode, lock it, read your data, seek to the begining of the file, write yout new data, and close it.

For more info:
man open

--mark
Quote Reply
Re: [Mark Badolato] My counter script was broken when it increase to # > 6000 ? Why? Please help In reply to
This is my master creation:

Code:
my $log = '/apache2/cgi-bin/test.txt';
my $count = 0;

open CNT, "+<$log" or die $!;
flock CNT, 2;

# Set the new count.
$count = <CNT> + 1;

seek CNT, 0, 0;
print CNT $count;
close CNT;
Quote Reply
Re: [Paul] My counter script was broken when it increase to # > 6000 ? Why? Please help In reply to
You probably want a truncate in there to make sure the file is 0 bytes before you rewrite it. See:

http://www.perldoc.com/....--How-can-I-do-this-

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] My counter script was broken when it increase to # > 6000 ? Why? Please help In reply to
The version without truncate is here:

http://213.106.15.160/cgi-bin/test.cgi

It works without but I guess truncate just makes sure nothing weird happens?
Quote Reply
Re: [Paul] My counter script was broken when it increase to # > 6000 ? Why? Please help In reply to
I guess it would be ok as you are always putting an equal amount of data or larger in it. Only be problematic if the counter was decreasing. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] My counter script was broken when it increase to # > 6000 ? Why? Please help In reply to
Yeah I guess if you had 10 in your file and removed 1, without truncate you'd be left with mmm 90?