Gossamer Forum
Home : General : Perl Programming :

flock question (guru needed)

Quote Reply
flock question (guru needed)
just wondering if you think that this code is secure (file locking). I'm not sure about flock way to handle files in case of 2 or more users at the same time.

Code:
open(OLDCLICK,"clicks");
$NEWCLICK=<OLDCLICK>+1;
close(OLDCLICK);

open(CLICK,">clicks");
flock(CLICK,LOCK_EX);
seek(CLICK, 0, 2);
print CLICK $NEWCLICK;
flock(CLICK,LOCK_UN);
close (CLICK);


Thanks
Quote Reply
Re: [robyone] flock question (guru needed) In reply to
i don't understand if
when there are two write requests they can be both executed (something like a queue).
Quote Reply
Re: [robyone] flock question (guru needed) In reply to
Eugh...I wouldn't do it that way Wink

Code:
open(OLDCLICK,"clicks") || die "Error: $!";
flock(OLDCLICK, 2);
$new_click = <OLDCLICK> + 1;
close(OLDCLICK);

open(CLICK,">>clicks") || die "Error: $!";
flock(CLICK, 2);
print CLICK $new_click;
close(CLICK);

Note, you don't need to un-flock the file... Wink

I'm pretty sure that will work. I don't quitre understand why you were doing a seek Crazy

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] flock question (guru needed) In reply to
i used seek since i found it on perldoc ... i supposed it was correct :)

what i need is a secure way to count clicks. is this working for any click even if they arrive at the same time?
Quote Reply
Re: [robyone] flock question (guru needed) In reply to
Yeah, that should stop any problems of people accessing the file at the same time. It should just queue them until the file is free for them, and then it will do its stuff Tongue

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] flock question (guru needed) In reply to
are u sure? i'm writing a script for my job and i don't want to be fired ;)
Quote Reply
Re: [Andy] flock question (guru needed) In reply to
what is the "2" usage?
Quote Reply
Re: [Andy] flock question (guru needed) In reply to
>>Eugh...I wouldn't do it that way <<

What are you "Eugh"ing for?....the way he had it was fine except that he didn't need to unlock the file as close(CLICK) does that for you.

flock(CLICK, 2) is exactly the same as flock(CLICK, LOCK_EX) except he decided to export from the Fcntl module.

Last edited by:

Paul: Jul 5, 2002, 5:55 AM