in reply to mkfifo/mknode
use warnings; use strict;
$$ does not contain a newline so there is no point in using chomp().chomp($pid = $$);
You should use the constants from the Fcntl module:$LOCK_EXCLUSIVE = 2; $UNLOCK = 8;
use Fcntl ':flock';
You probably meant to use the %ENV hash there:$fifo = "ENV{HOME}/a_file";
$fifo = "$ENV{HOME}/a_file";
The $? variable will only contain useful information if you are running an external program. which you are not. However, you should include the $! variable in any system error messages.close(FILE) || warn "update queue file exited $?\n";
close(LOCK) will also unlock the file so the subsequent attempt to unlock it is superfluous.close(LOCK) || warn "lock file exited $?\n";; $str=`cat $fifo.lock`; flock LOCK, $UNLOCK;
Is there a reason that you couldn't use the built-in unlink function?system("rm $fifo.lock");
Have you read the entries in perlfaq5 on file locking?
You are locking the PID file but not the data file. This may work better:
#!/usr/local/bin/perl use warnings; use strict; use Fcntl ':flock'; use Tie::File; my $fifo = "$ENV{HOME}/a_file"; my $lock = "$fifo.lock"; tie( my @pid, 'Tie::File', $lock )->flock( LOCK_EX ) or die "Cannot open '$lock' $!"; @pid = $$; tie( my @lines, 'Tie::File', $fifo )->flock( LOCK_EX ) or die "Cannot open '$fifo' $!"; ( my $pop, @lines ) = sort @lines; print "$hostname is poping $pop\n"; untie @lines; print "PID lock file is going to be removed\n"; untie @pid; unlink $lock or die "Cannot unlink '$lock' $!"; __END__
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: mkfifo/mknode
by azaria (Beadle) on Jun 14, 2006 at 12:15 UTC | |
by jwkrahn (Abbot) on Jun 15, 2006 at 01:16 UTC |