I'm having a difficult time getting NFSLock to work as I understand it through the documentation. I was able to get the example "lock_test" provided with the CPAN module to correctly work simultaneously on 4 servers using NFS file system so

However, what I need to do is determine if a server can get an NFSLock on a new file- and if so I need the server to create and lock it, and write to it. If it is unable to get the lock, simply have the server move on. I could have up to 50 servers trying to determine if the file exists, but only need one of them to create it. The "README" file that comes with the module states "Blocking locks will wait until other locks are removed before the process continues. Non blocking locks will return undef if another process currently has the lock." It seemed to me that if I simply changed the code from "LOCK_EX" to "LOCK_NB" that this would do what I need but this isn't the case. Instead, two things happen 1) NONE of the servers are able to ever get an NFSLock, and 2)hundreds if not thousands of "tmp" files are created (depending on the increment I choose) in the directory and not removed.

Am I using this module incorrectly or missing something? I also know through research that the NFS system can be troublesome, but it seemed that occurred for older versions of NFS, and finding this module gave me hope that I'd be able to do what I need. For the code given, I just change the line comment for the "my $lock"

command line: ./my_lock_test.pl nfs_lock_test.txt 1000 > server_A.out

#!/usr/bin/perl5.10.1 -w use strict; use File::NFSLock (); use Fcntl qw( O_CREAT LOCK_EX LOCK_NB); use lib '../lib/perl/'; my $datafile = shift; my $inc = shift || do { print "Usage: $0 <filename> <increment>\n"; exit; }; while ( $inc -- > 0 ) { my $lock = new File::NFSLock ($datafile, LOCK_NB); #my $lock = new File::NFSLock ($datafile, LOCK_EX); if ($lock) { print "true\n"; } else { print "false\n"; } }

In reply to NFSLock - Continue in code if another server already has lock on file by mightyMrJawz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.