title: Perl: flock() works on Linux, ignores previous lock on AIX
body: <p>In a nutshell: wrote a Perl script using flock(). On Linux, it behaves as expected. On AIX, flock() always returns 1, even though another instance of the script, using flock(), should be holding an exclusive lock on the lockfile.</p>
  
  <p>We ship a Bash script to restart our program, relying on flock(1) to prevent simultaneous restarts from making multiple processes. Recently we deployed on AIX, where flock(1) doesn't come by default and won't be provided by the admins. Hoping to keep things simple, I wrote a Perl script called flock, like this:</p>
  
  <pre><code>#!/usr/bin/perl
  
  use Fcntl ':flock';
  use Getopt::Std 'getopts';
  
  getopts("nu:x:");
  
  %switches = (LOCK_EX =&gt; $opt_x, LOCK_UN =&gt; $opt_u, LOCK_NB =&gt; $opt_n);
  
  my $lockFlags = 0;
  
  foreach $key (keys %switches) {
      if($switches{$key}) {$lockFlags |= eval($key)};
  }
  
  $fileDesc = $opt_x || $opt_u;
  
  open(my $lockFile, "&gt;&amp;=$fileDesc") ||  die "Can't open file descriptor: $!";
  flock($lockFile, $lockFlags) || die "Can't change lock - $!\n";;
  </code></pre>
  
  <p>I tested the script by running (flock -n -x 200; sleep 60)200>lockfile twice, nearly simultaneously, from two terminal tabs. </p>
  
  <p>On Linux, the second run dies with "Resource temporarily unavailable", as expected.</p>
  
  <p>On AIX, the second run acquires the lock, with flock() returning 1, as most definitely not expected.</p>
  
  <p>I understand the flock() is implemented differently on the two systems, the Linux version using flock(1) and the AIX one using, I think, fcntl(1). I don't have enough expertise to understand how this causes my problem, and how to solve it.</p>
  
  <p>Many thanks for any advice.</p>