Locking NFS files using File::NFSLock

--0015173fe7ec9627f404a0b2f2fd
Content-Type: text/plain; charset=UTF-8

Hi,

I was just exploring File::NFSLock to lock files on the NFS server but seems
like its not working. It ends up creating stale files and only option left
is to abort the script. I am testing it on NFSv3

Anybody had any success in using this module?

Here is my sample code.

use strict;
use warnings;

use File::NFSLock qw(uncache);
use Fcntl qw(LOCK_EX LOCK_NB);
print "Please enter the file to be locked:\n";
chomp(my $file = <STDIN>);
open my $fh, '>', $file or die $!;
print $fh "Hello World";
print "Please try to write to the file\n";
my $wait = <STDIN>;
#my $lock = File::NFSLock->new($file,LOCK_EX|LOCK_NB,10*60,30*60);
my $lock = File::NFSLock->new($file,LOCK_EX,10*60,30*60);
print "File is now locked. Now again try to write to the file\n";
$wait = <STDIN>;
print "Lock operation over\n";

~Parag

--0015173fe7ec9627f404a0b2f2fd--
Parag Kalra [ Di, 12 April 2011 08:37 ] [ ID #2057983 ]

Re: Locking NFS files using File::NFSLock

On Mon, Apr 11, 2011 at 23:37, Parag Kalra <paragkalra [at] gmail.com> wrote:
> Hi,
>
> I was just exploring File::NFSLock to lock files on the NFS server but seems
> like its not working. It ends up creating stale files and only option left
> is to abort the script. I am testing it on NFSv3
>
> Anybody had any success in using this module?
>
> Here is my sample code.
>
> use strict;
> use warnings;
>
> use File::NFSLock qw(uncache);
> use Fcntl qw(LOCK_EX LOCK_NB);
> print "Please enter the file to be locked:\n";
> chomp(my $file = <STDIN>);
> open my $fh, '>', $file or die $!;
> print $fh "Hello World";
> print "Please try to write to the file\n";
> my $wait = <STDIN>;
> #my $lock = File::NFSLock->new($file,LOCK_EX|LOCK_NB,10*60,30*60);
> my $lock = File::NFSLock->new($file,LOCK_EX,10*60,30*60);
> print "File is now locked. Now again try to write to the file\n";
> $wait = <STDIN>;
> print "Lock operation over\n";
>
> ~Parag
>

Hmmm, works pretty well for me on Solaris 9 and Solaris 10 where the
lock is on an NFS partition that all three machines share. I don't
think I've had any stale lock problems (perhaps only one) and no hung
processes and I no longer experience text file corruption that I was
experiencing previously when more than one person wanted to write to
the same text file at the same time. Make sure that the lock file is
on the NFS partition.

Ken Wolcott

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Kenneth Wolcott [ Di, 12 April 2011 08:53 ] [ ID #2057984 ]
Perl » gmane.comp.lang.perl.beginners » Locking NFS files using File::NFSLock

Vorheriges Thema: [moonbuzz@gmail.com: Re: Help using cgi]Re: Help using cgi
Nächstes Thema: Wanted: Help inventing poor man's FTP server