Perl: flock () works on Linux, ignores previous AIX lock

In a nutshell: wrote a Perl script using flock (). On Linux, it behaves as expected. On AIX, the flock () function always returns 1, although another script instance, using flock (), must contain an exclusive lock on the lock file.

We send a Bash script to reload our program, relying on flock (1), to prevent simultaneous reboots from several processes. We have recently been deployed to AIX, where flock (1) does not arrive by default and will not be provided by administrators. Hoping to keep things simple, I wrote a Perl script called flock, for example:

#!/usr/bin/perl

use Fcntl ':flock';
use Getopt::Std 'getopts';

getopts("nu:x:");

%switches = (LOCK_EX => $opt_x, LOCK_UN => $opt_u, LOCK_NB => $opt_n);

my $lockFlags = 0;

foreach $key (keys %switches) {
    if($switches{$key}) {$lockFlags |= eval($key)};
}

$fileDesc = $opt_x || $opt_u;

open(my $lockFile, ">&=$fileDesc") ||  die "Can't open file descriptor: $!";
flock($lockFile, $lockFlags) || die "Can't change lock - $!\n";;

I tested the script by running (flock -n -x 200; sleep 60) 200> the lock file twice, almost simultaneously, from two tabs of the terminal.

Linux " ", .

AIX , flock() 1, .

, flock() - : Linux flock (1) AIX, , , fcntl (1). , , .

.

+3
2

AIX, open() script .

- :

open (my $lockfile, ">>", $fileDesc) # for LOCK_EX, must be write

"dup() >&=, script , .

( ​​)

first window:
$ ./flock.pl -n -x lockfile
opened lockfile
locked
second window:
$./flock.pl -n -x lockfile
opened lockfile
Can't change lock - Resource temporarily unavailable
$
+2

, ; AIX Linux.

POSIX : , , . = .

Linux, , , , : (, , -) .

script.

: man 2 fcntl, man 2 flock.

+2

All Articles