note
stevieb
<p>First thing I thought of was shared memory. In [mod://IPC::Shareable], if one attempts to create a shared memory segment that has already been created in a separate process with <c>exclusive</c> set, the second process will <c>croak()</c>. So what I did is add a <c>graceful</c> flag to its options, and when set, the second process that tries to create the exclusive segment simply exits gracefully with no noise or anything. Observe:</p>
<p>lock.pl:</p>
<c>
use warnings;
use strict;
use IPC::Shareable;
tie my $lock, 'IPC::Shareable', {
key => 'LOCK',
create => 1,
exclusive => 1,
destroy => 1,
graceful => 1
};
$lock = $$;
print "procID: $lock\n";
sleep 5;
</c>
<p>Run it in one window:</p>
<c>
spek@scelia ~/scratch $ perl lock.pl
procID: 21241
</c>
<p>...it sleeps for a few seconds, during which we run it in the second window:</p>
<c>
spek@scelia ~/repos/ipc-shareable $ perl ~/scratch/lock.pl
spek@scelia ~/repos/ipc-shareable $
</c>
<p>...it exited before it printed anything. After the five second sleep in proc one is done, run it in window two:</p>
<c>
spek@scelia ~/repos/ipc-shareable $ perl ~/scratch/lock.pl
procID: 21339
</c>
<p>So, in essence, this question prompted me to update the distribution to handle your very issue, ummm, well, gracefully. I just published it, so it may not yet be available at your mirror. Version 1.01 has the new 'graceful' feature.</p>
11134206
11134206