1#!/usr/bin/perl -w
2
3### Written by Rob Brown
4### This script is designed to be ran on multiple boxes
5### by multiple processes with a high increment number.
6### The processes should all compete, but a successful
7### test occurs if all of the specified inc's add up to
8### the final number in the specified file.
9
10use strict;
11use File::NFSLock ();
12use Fcntl qw(O_RDWR O_CREAT LOCK_EX);
13
14my $datafile = shift;
15my $inc      = shift || do {
16  print "Usage: $0 <filename> <increment>\n";
17  exit;
18};
19
20while ( $inc -- > 0 ) {
21  my $lock = new File::NFSLock ($datafile, LOCK_EX) 
22    or print "Ouch1\n"; # blocking lock (Exclusive)
23
24  sysopen(FH, $datafile, O_RDWR | O_CREAT)
25    or die "Cannot open [$datafile][$!]";
26
27  ### read the count and spit it out
28  my $count = <FH>;
29  $count ++;
30
31  print "[$$] I win with [$count]            \r";
32
33  seek (FH,0,0);
34  print FH "$count\n";
35  close FH;
36  # $lock leaves scope and unlocks automagically
37}
38print "\n\n";
39