Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timestamp help

Status
Not open for further replies.

Mute101

Programmer
Jun 28, 2001
428
GB
Hi,

I have been working on some locking code but now I'm stuck on the last hurdle!

I need a 'catch all' piece of code that will check the time stamp of the lock file and if its old delete it and start the locking proccess anew.

After looking at the Stat function I presume I can use the ctime information to tell me how old the file is, my question is what do I use to test the ctime against (ie. time now since epoch)?

Any ideas?

----------------------------------------
Of all the things I have lost in my life, the thing I miss the MOST is my mind!
----------------------------------------
 
Correct, just do a seconds calculation and if its outside that range delete and start over.
 
erm, heres my code so far;

Code:
sub lock
{
	my $tries = 10;
	my $nam = "$ENV{'DOCUMENT_ROOT'}/lock/lock.lf";
	my $lock = $nam.'.lock';
	my $temporary = $nam.'.'.$$;

	if (-e $lock)
	{
		#check time stamp and delete if old
		$stats = stat($lock);
		if ($stats->ctime > 0.0014)
		{
			link ($lock, $nam.'.'.$stats->ctime);
			unlink $lock;
		}
	}

	my $tfh = new FileHandle $temporary, O_RDONLY|O_CREAT|O_EXCL, 0600
		or die "Cannot create temporary lock file '$temporary': $!";
	while (--$tries >= 0) {
		link ($temporary, $lock)
			or next;
		unlink $temporary;
		return;
	} continue {
		sleep 1;
	}
	
	die "Unable to obtain lock on $nam";
}

What do I need to do to make it actually check time stamps properly? ie. what do I need to do to the if statement below to make it delete the lock file if its say 20 seconds old?

Code:
if ($stats->ctime > 0.0014)

Thats my problem, any help much appreciated.

----------------------------------------
Of all the things I have lost in my life, the thing I miss the MOST is my mind!
----------------------------------------
 
Code:
@stats=stat($lock);
$ctime=$stats[10];  # 10th item in array;
$now=localtime;
if (($now-$ctime) > 10) {
# lockfile is older than 10 seconds
} else {
# do something else
}

HTH
--Paul
untested
 
Ofcourse!

I got that involved in the all new spangly stat function (I have never used it before) I just didnt even think of using localtime.

Many many thanks.

----------------------------------------
Of all the things I have lost in my life, the thing I miss the MOST is my mind!
----------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top