Adam Turoff on Sun, 19 Nov 2000 20:47:32 -0500 (EST)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Timing out flock (fwd)


----- Forwarded message from owner-phl@lists.pm.org -----
Reply-To: <barry@veinotte.com>
From: "Barry Veinotte" <barry@veinotte.com>
To: "Phl@Lists.Pm.Org" <phl@lists.pm.org>
Subject: Timing out flock
Date: Sun, 19 Nov 2000 17:13:31 -0400

Hi Folks.

I have yet another stupid question. I have a sub called get_lock
that is shared by different scripts. I need to time out flock if it is
slow as waiting forever ( or until the system times out the script )
is causing process limit problems. When the processes get backed
up I am taking eight other websites down with me, so I must make this
work.

I have used the code from the camel (3rd edition) on page 417
but admit to not fully understanding it, and need to know if the
context I have used it in will actually work or if I have just made
a mess of things...

Here is the altered sub:

sub get_lock {

my $type = $_[0];

eval {
	local $SIG{ALRM} = sub { die "alarm clock restart" };
	alarm 10;

	eval {

  		  unless ($type == 1)
			  {
	 		  open (LOCK,">$lock_file") or die "Can't open lock file $lock_file : $!";  # $lock_file is global.
	 		  flock(LOCK, LOCK_EX) or die "Can't lock the lock file $lock_file : $!";
			  }
 		else
    		{
    		open (LOCK,"<$lock_file") or die "Can't open lock file $lock_file : $!";
    		flock(LOCK, LOCK_SH) or die "Can't lock the lock file $lock_file : $!";
    		}

	}; # Second eval
		alarm 0;
}; # first eval
    alarm 0;
    die if $@ && $@ !~ /alarm clock restart/;

}

#######

Will this work this way?  Any help would be appreciated.
If I am being totally idiotic here, say so, but please tell me if this will
work as expected... 

Thanks,
Barry


----- End forwarded message -----
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**