Luinrandir Hernson wrote:
> ## opens, reads number of hits and closes the file acording to the web page its 
>called from.
> open(FILE,"/home/homepage/cgi-bin/data/$file");
> flock (FILE, 2);
> $count = <FILE>;
> flock (FILE, 8);
> close(FILE);
> 
> ## adds a hit to the variable
> $count++;
> 
> ## opens, prints the new total of hits and closes the file.
> open(FILE,">/home/homepage/cgi-bin/data/$file");
> flock (FILE, 2);
> print FILE "$count";
> flock (FILE, 8);
> close(FILE);

FWIW (and a bit off-topic, since you didn't ask about this and it
doesn't address your primary problem), this file locking is a bit
bad. Yeah, a file should be locked as short a while as possible,
but $count++ doesn't really take much time. Anyway, what happens
if your program gets through the first lock, and then never sees
the processor again for ten seconds; a thousand hits are made to
your page in between by other visitors, each of which might
successfully update the count; then this instance continues and
clobbers the new count with what it has been planning to write for
ten seconds now.

Granted, that's not the end of the world, nor even likely to throw
off your count by a 'significant' magnitude, but it does show that
the count is not guaranteed accurate (which, again, may not matter
to you) and that the file locking technique is both wrong and
lends a false sense of correctness.

Like I said, FWIW,
Christian

__________________________________________________________________
117 NW 15th Street # S107                            [EMAIL PROTECTED]
Gainesville, FL  32603-1973                         (352) 392-0851

Reply via email to