Newbie: counter sometimes resets 
Author Message
 Newbie: counter sometimes resets

Hi,
For a few days, my counter sometimes has reset, and the count rolled
back to zero. The count is around 28000 or 29000 hits.
My Perl code seems quite simple, but as I'm new to Perl, I hope some
of you will be able to help me...
Here is the main code:

open(CNTFILE, "<cnt.txt");
flock CNTFILE, 2;
$count = <CNTFILE>;
chop($count);
close(CNTFILE);

$count++;

open(CNTFILE, ">cnt.txt");
flock CNTFILE, 2;
print CNTFILE "$count\n";
close(CNTFILE);

What's wrong ??
Thanks in advance.

Yan

----------------------------------------------
c h r o m a           http://www.*-*-*.com/
16 millions de couleurs, oui mais lesquelles ?
----------------------------------------------



Wed, 18 Jun 1902 08:00:00 GMT  
 Newbie: counter sometimes resets
Issue #1)
There are certain "magic" number one must be aware of in programming.
Usually associated with the binary counting process. One of them is at
32768. Signed 16bit numbers go negative there.

Issue #2)
If more than one process can read/write the "cnt.txt" file then the lock
MUST hold while incrementing the count. Otherwise some other process could
write to the file in-between the two open-lock-close sets. The counting must
be kept "atomic", only one process can use it at a time.
----------------------------------------------------------------------------
----

Quote:
> Hi,
> For a few days, my counter sometimes has reset, and the count rolled
> back to zero. The count is around 28000 or 29000 hits.
> My Perl code seems quite simple, but as I'm new to Perl, I hope some
> of you will be able to help me...
> Here is the main code:

> open(CNTFILE, "<cnt.txt");
> flock CNTFILE, 2;
> $count = <CNTFILE>;
> chop($count);
> close(CNTFILE);

> $count++;

> open(CNTFILE, ">cnt.txt");
> flock CNTFILE, 2;
> print CNTFILE "$count\n";
> close(CNTFILE);

> What's wrong ??
> Thanks in advance.

> Yan

> ----------------------------------------------
> c h r o m a          http://pourpre.com/chroma
> 16 millions de couleurs, oui mais lesquelles ?
> ----------------------------------------------




Wed, 18 Jun 1902 08:00:00 GMT  
 Newbie: counter sometimes resets

Quote:

>open(CNTFILE, ">cnt.txt");
>flock CNTFILE, 2;
>What's wrong ??

You delete the contents of the file before locking it. Besides, the
"28000 to 29000" is coincidence. It's a matter of simultanious accesses
with a bad file lock.

You can open the file in mode "+<", do the lock, read the file,
increment the counter, seek back to the start of the file, write the new
value (which has equal or larger length as the original value, so
there's no need to truncate the file), and close the file which also
unlocks it. So you open the file only once.

--
        Bart.



Wed, 18 Jun 1902 08:00:00 GMT  
 Newbie: counter sometimes resets
Thanks !



Quote:
>You delete the contents of the file before locking it. Besides, the
>"28000 to 29000" is coincidence. It's a matter of simultanious accesses
>with a bad file lock.

>You can open the file in mode "+<", do the lock, read the file,
>increment the counter, seek back to the start of the file, write the new
>value (which has equal or larger length as the original value, so
>there's no need to truncate the file), and close the file which also
>unlocks it. So you open the file only once.

>--
>    Bart.

----------------------------------------------
c h r o m a          http://pourpre.com/chroma
16 millions de couleurs, oui mais lesquelles ?
----------------------------------------------



Wed, 18 Jun 1902 08:00:00 GMT  
 Newbie: counter sometimes resets
It works !
I added a "truncate" function, as I also store an IP address after the
count.
Thanks again for your help.

Yan



Quote:

>>open(CNTFILE, ">cnt.txt");
>>flock CNTFILE, 2;

>>What's wrong ??

>You delete the contents of the file before locking it. Besides, the
>"28000 to 29000" is coincidence. It's a matter of simultanious accesses
>with a bad file lock.

>You can open the file in mode "+<", do the lock, read the file,
>increment the counter, seek back to the start of the file, write the new
>value (which has equal or larger length as the original value, so
>there's no need to truncate the file), and close the file which also
>unlocks it. So you open the file only once.

>--
>    Bart.

----------------------------------------------
c h r o m a          http://pourpre.com/chroma
16 millions de couleurs, oui mais lesquelles ?
----------------------------------------------



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Newbie:Sometimes it works and sometimes it won't

2. Why text counter reset?

3. Counter problem - resets

4. Counter resetting

5. Net::SMTP works sometimes and sometimes not??

6. sometimes it works, sometimes it doesn't.....

7. Newbie, split function, and counter

8. hash and counter - newbie

9. newbie....and counters..

10. Newbie: graphic counter:shes-a no work!

11. Forking sometimes (occationaly) makes my program crash!

12. Bug report: \Z sometimes matches BEFORE end of string

 

 
Powered by phpBB® Forum Software