HEX
Server: Apache
System: Linux s198.coreserver.jp 5.15.0-151-generic #161-Ubuntu SMP Tue Jul 22 14:25:40 UTC 2025 x86_64
User: nagasaki (10062)
PHP: 7.1.33
Disabled: NONE
Upload Files
File: //usr/share/doc/perl-File-NFSLock/examples/lock_test
#!/usr/bin/perl -w

### Written by Rob Brown
### This script is designed to be ran on multiple boxes
### by multiple processes with a high increment number.
### The processes should all compete, but a successful
### test occurs if all of the specified inc's add up to
### the final number in the specified file.

use strict;
use File::NFSLock ();
use Fcntl qw(O_RDWR O_CREAT LOCK_EX);

my $datafile = shift;
my $inc      = shift || do {
  print "Usage: $0 <filename> <increment>\n";
  exit;
};

while ( $inc -- > 0 ) {
  my $lock = new File::NFSLock ($datafile, LOCK_EX) 
    or print "Ouch1\n"; # blocking lock (Exclusive)

  sysopen(FH, $datafile, O_RDWR | O_CREAT)
    or die "Cannot open [$datafile][$!]";

  ### read the count and spit it out
  my $count = <FH>;
  $count ++;

  print "[$$] I win with [$count]            \r";

  seek (FH,0,0);
  print FH "$count\n";
  close FH;
  # $lock leaves scope and unlocks automagically
}
print "\n\n";