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:
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). , , .
.