On Thu, 10 Jan 2002 11:35:30 +0000 (GMT), Richard Smith wrote:
>Thank you for the pointers. Unfortunately, the wooshing sound you just heard
>was that code passing over my head at high speed. As I do not like
>'cut-and-paste' programming, I am unwilling to use your code when I don't
>understand much of it. =8O)
You want me to walk you through it? OK...
> use Fcntl;
import the constants
> {
> my @lockfiles;
array of lockfiles created by thris program
> END {
> foreach(@lockfiles) { unlink } # for 5.004
> }
When the program ends, it will delete every lockfile it created itself.
> sub lockFile ($) {
> #Create a lock file for the file passed as a parameter.
> my $lockfile = shift() . '.lock';
file name
> my $t0 = time;
starting time
> local *LOCK;
just not to stamp on any existsing global filehandle with the same name
> until(sysopen LOCK, $lockfile,
> O_CREAT|O_TRUNC|O_EXCL|O_WRONLY) {
Try to open a file for writing, just like ">", but fail if it exists.
"until(...){...}" is the same as "while(! ...){...}".
> die "Too many loops" if time-$t0 > 10;
It has been trying for over 10 seconds
> select undef, undef, undef, 0.1;
sleep() for 1/10th of a second
> }
End of loop. If we get past here, we've been successful. The file now
exists, and is open.
> push @lockfiles, $lockfile;
Marke the file for deletion when the script ends
> print LOCK $$;
Just some info. It needn't be a plain "0".
> close LOCK;
existence is enough to lock the file.
> }
end of sub
> }
end of scope for @lockfiles.
--
Bart.