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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Locking a script during execution

 



Zhris
Enthusiast

Jun 14, 2010, 9:21 PM

Post #1 of 2 (423 views)
Locking a script during execution Can't Post

Hey,

I have a Perl/CGI script that multiple users may execute at the same time. It is part of a website, and ran on a linux web server. As it deals with flat databases, interwinding executions could corrupt the data. Multiple scripts access the same database in a similar way. Therefore I decided I may need to lock my script during execution i.e. lock it near the beginning and unlock it again at the end. If it is locked on execution, then keep checking until its unlocked before continueing.

Firstly, is a locking system even neccessary? I'm not sure if scripts are automatically locked. I'm finding it difficult to test this.

Secondly, here is my script (if you could please provide me with some feedback) (i'm not even sure it does anything useful or is working at all, again i'm finding it difficult to test this):


Code
   

LockScript ('Lock', $lockpath);

#Main part of script e.g. read "database" into hash, edit hash, write hash to temp file, rename temp file to "database"

LockScript ('Unlock', $lockpath);

sub LockScript{
my $dowhat = shift;
my $lockpath = shift;
if ($dowhat eq 'Lock') {
open (LOCK, "<$lockpath") || die qq(Cannot Open Lock);
flock (LOCK, 2) || LockScript ('Lock', $lockpath);
return 'Locked';
}
elsif ($dowhat eq 'Unlock') {
close (LOCK) || die qq(Cannot Close Lock);
return 'Unlocked';
}
}



Any comments will be much appreciated as I really don't want any problems to occur when live.

Thanks alot,

Chris


(This post was edited by Zhris on Jun 14, 2010, 9:27 PM)


Zhris
Enthusiast

Jun 15, 2010, 8:03 AM

Post #2 of 2 (408 views)
Re: [Zhris] Locking a script during execution [In reply to] Can't Post

Anyone have any suggestions at all?

Chris

 
 


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

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