Re: At most 1 running copy

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

 



On Sat, Nov 27, 2004 at 07:41:46AM +0800, John Summerfield wrote:
> On Friday 26 November 2004 22:44, Ed Wilts wrote:
> > Yesterday 22:44:50
> >
> Potential race condition there. Junk it.

I did!  What I inherited was worse than a race condition - it flat out
didn't work.
 
> I suggest you create  /var/run/lock-$USER-APPLICATION and lock it.

Yup, that's what I just ended up doing today.  It turns out that this is
discussed in the Perl faq.
 
> Once you get to lock it, you're the only one running.
> 
> Release the lock when you're finished. I don't see that it matters if you 
> leave the file there, and I'd want to test it before telling you it's alwasy 
> safe to remove the file. If you can remove it the unlock it that woild be 
> okay, but that flies in the face of my experience in oter environments.

For the record, this snippet works just fine (a per-user lock).

use FileHandle;
use Fcntl qw(:DEFAULT :flock);
my $FH = new FileHandle;
sysopen(FH, "ftphandler.lock", O_RDWR | O_CREAT) or die "can't open ftphandler.lock: $!";
flock(FH, LOCK_EX | LOCK_NB) or exit;
print "running...\n";
[rest of code here]
close (FH);
unlink ("ftphandler.lock");

It doesn't matter if the file is closed or deleted - the right thing
just happens and it doesn't matter if the lock file is there the next
time around or not.  The snippet above silently exits if the process
can't get the lock.  If I remove the LOCK_NB, the process will wait
until it can get the lock.

Thanks,
        .../Ed

-- 
Ed Wilts, RHCE
Mounds View, MN, USA
mailto:ewilts@xxxxxxxxxx
Member #1, Red Hat Community Ambassador Program


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux