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: Beginner:
Locking a script during execution



Jun 14, 2010, 9:21 PM

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


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):


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,


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


Jun 15, 2010, 8:03 AM

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

Anyone have any suggestions at all?



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

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