A PID-based approach to process exclusion suffers from the possibility of PID re-use. Also, there is usually no atomic way of checking the pid *and* registering it. That doesn't mean the approach can't be used in practice, but there is a small probability of malfunction.
A safe approach can be based on file locking (flock), which is *meant* to guarantee exclusive access. The following code shows how that can be done in a Unix-ish environment. I'm sure there is already a module on CPAN (or two) that does this in a more portable way, but I couldn't find it in a hurry.
#!/usr/local/bin/perl
use strict; use warnings; $| = 1;
use Fcntl qw( :flock);
my $name = 'ttt'; # process name
my $pid_file = "/var/run/$name.pid";
open my $pid_handle, '+<', $pid_file or die "Can't access '$pid_file':
+ $!";
flock $pid_handle, LOCK_EX | LOCK_NB or die "Process $name is still ac
+tive";
truncate $pid_handle, 0 or die "Truncating '$pid_file' failed: $!";
print $pid_handle "$$\n";
# Normal processing starts here
#
# ...
sleep 10; # for testing
Update: I forgot to mention that the file /var/run/ttt.pid must be pre-created and given ownership and permissions so that the user that runs the process can write it.
Anno