File: lock_test

package info (click to toggle)
libfile-nfslock-perl 1.29-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 208 kB
  • sloc: perl: 429; makefile: 2
file content (38 lines) | stat: -rwxr-xr-x 950 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/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";