in reply to mkfifo/mknode

You should use the warnings and strict pragmas so the second and third lines of your program should be:
use warnings; use strict;
chomp($pid = $$);
$$ does not contain a newline so there is no point in using chomp().
$LOCK_EXCLUSIVE = 2; $UNLOCK = 8;
You should use the constants from the Fcntl module:
use Fcntl ':flock';
$fifo = "ENV{HOME}/a_file";
You probably meant to use the %ENV hash there:
$fifo = "$ENV{HOME}/a_file";
close(FILE) || warn "update queue file exited $?\n";
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(LOCK) || warn "lock file exited $?\n";; $str=`cat $fifo.lock`; flock LOCK, $UNLOCK;
close(LOCK) will also unlock the file so the subsequent attempt to unlock it is superfluous.
system("rm $fifo.lock");
Is there a reason that you couldn't use the built-in unlink function?

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

    First thanks for your detailed reply. I used the following code but there is a problem with the flock package -

    Usage: Fcntl::constant(name, arg) at Fcntl.pm line 225.

    Can you please help ?

    azaria

    -------------------------------------------------------

    #! /usr/local/bin/perl chomp($hostname=`hostname`); chomp($date = `date \'\+\%y\%m\%d\%H\%M\%S\'`); 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__

    Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

      I used the following code but there is a problem with the flock package -
      Usage: Fcntl::constant(name, arg) at Fcntl.pm line 225.
      The Fcntl module that I am using does not have any "usage" messages in it however the Tie::File module does. Which version of the Tie::File module are you using? You can find out like this:
      $ perl -MTie::File -le'print $Tie::File::VERSION' 0.97
      If the Fcntl module does not work you could go back to using:
      my $LOCK_EX = 2;
      And change all occurances of LOCK_EX to $LOCK_EX.

      Also, to get the hostname you could use the Sys::Hostname module:

      use Sys::Hostname; my $hostname = hostname;
      And to get a formatted date you could use the POSIX module:
      use POSIX 'strftime'; my $date = strftime '%y%m%d%H%M%S', localtime;