CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:



Jan 20, 2000, 7:42 PM

Post #1 of 10 (11682 views)
flock Can't Post

OK, First of all I want to apologize for just one more annoying counter question.

I need a little more info on the flock function

I have written a counter script that is in use on a heavily traveled site. It records the IPs of the visitors and the checks against that file each time to give a close to accurate unique count. I open the file and then use the flock command to lock it for exclusive access, but it still seems that I am not capturing every hit to the site.

Is this because of the fact that the open command is prior to the flock command and that if the file is locked when it is being attempted to be opened by another visitor the open request fails and that person is then not updated to the list? That is my thought at the moment…

If this is the case can this be resolve by using an until (open) in place of just the open statement?

Am I on the right track in solving this?

Any thoughts that can point me in the right direction would be greatly appreciated.



Jan 20, 2000, 9:27 PM

Post #2 of 10 (11682 views)
Re: flock [In reply to] Can't Post

perlfaq5 has writen this script for you
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

I still don't get locking. I just want to increment the
number in the file. How can I do this?

Didn't anyone ever tell you web-page hit counters were
useless? They don't count number of hits, they're a waste
of time, and they serve only to stroke the writer's
vanity. Better to pick a random number. It's more

Anyway, this is what you can do if you can't help

use Fcntl ':flock';
sysopen(FH, "numfile", O_RDWR|O_CREAT) or die "can't open numfile: $!";
flock(FH, LOCK_EX) or die "can't flock numfile: $!";
$num = <FH> | | 0;
seek(FH, 0, 0) or die "can't rewind numfile: $!";
truncate(FH, 0) or die "can't truncate numfile: $!";
(print FH $num+1, "\n") or die "can't write numfile: $!";
# Perl as of 5.004 automatically flushes before unlocking
flock(FH, LOCK_UN) or die "can't flock numfile: $!";
close FH or die "can't close numfile: $!";

Here's a much better web-page hit counter:

$hits = int( (time() - 850_000_000) / rand(1_000) );

If the count doesn't impress your friends, then the code
might. :-)
If you really need to keep track of the people coming to your site use the Log Files.


[This message has been edited by Borderline (edited 01-20-2000).]


Jan 21, 2000, 5:37 AM

Post #3 of 10 (11682 views)
Re: flock [In reply to] Can't Post

thank you for you quick responce.
But isn't it still possible that on a heavily traficed site that one request might try and open the file while someone else has it locked and thus would the script would die at that point and not incrament for that person?


Jan 21, 2000, 5:42 AM

Post #4 of 10 (11682 views)
Re: flock [In reply to] Can't Post

The process that tries to flock a file that is already flocked will wait untill it is not flocked. It will only wait not crash.



Jan 21, 2000, 6:16 AM

Post #5 of 10 (11682 views)
Re: flock [In reply to] Can't Post

Yes, I do understand that once it gets to the flock command that it will wait, but prior to the flock command you open the file or die if unable to open. If the file is locked wouldn't the open command fail to open the file and thus the script die before it gets to the flock command?


Jan 21, 2000, 6:22 AM

Post #6 of 10 (11682 views)
Re: flock [In reply to] Can't Post

Flock does not prevent an open from occuring. Flock only makes other processes that are also flocking wait when they try to flock. The file will still get opened but when you go to flock it if anouther process has a flock on it already it will wait.
Flocking only works if all processes that will be writting to the file are also using flock.



Jan 21, 2000, 6:52 AM

Post #7 of 10 (11682 views)
Re: flock [In reply to] Can't Post

ok, all of this makes sense except for the fact that I still seem to be dropping some people and they are not being counted...

I want to thank you so much for helping me out. all of your help is greatly appriciated...

one laast thought before we close this subject. all the flock isues aside, what about the simple fact of the file being open.

I am using open(FILE "+>>fliename") as I understand and it seems to work this way so that it will create the file if it doesn't exist. is it possible for the same file to be opened in this manner at the same time while then waiting for the flock to release it. Or could this be the problem?

Again, thank you so much...


Jan 21, 2000, 8:02 PM

Post #8 of 10 (11682 views)
Re: flock [In reply to] Can't Post


What you are trying to do will not work. The only way to open a file and create it if necessary and still have read permission on it is to use sysopen like the code I posted from perlfaq does.
If you know ahead of time the file will be there you can do this
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl -w
# Import flocking codes for this system
use Fcntl qw(:flock);

# Open the file for reading and writing
open FH,'+<test.txt' or die "Could not open test.txt: $!";

# did not print $! here cause flock does not return
# anything in $!. IMHO it should
flock FH,LOCK_EX or die 'Could not flock';

# Read the number from the file
my $num = <FH> &#0124; &#0124; 0;

# Go back to the first line of the file
seek(FH,0,0) or die "Could not seek test.txt: $!";

# Print the new number to the file
print FH $num+1,"\n";

# Un-flock the file
flock FH,LOCK_UN or die 'Could not un-flock';

# Close the file
# If you are using Perl4 you should close
# the file before you Un-flock it because
# Perl4 did not automaticaly flush output
# buffers
close FH;



Jan 22, 2000, 6:47 AM

Post #9 of 10 (11682 views)
Re: flock [In reply to] Can't Post

the way the scrit is written,if it is the first hit for the counter the file will not already exist and it will create it. so what you are telling me in this case is that I will have to use the sysopen command to allow it to create the file and not the open for read and apend like I currently have...

Am I understanding you correctly in this now?

You have been a great help, thank you so much...


Jan 24, 2000, 5:26 AM

Post #10 of 10 (11682 views)
Re: flock [In reply to] Can't Post


Yep Smile



Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives