still issues with file lock on NFS

intitially i was using the code  on NFS mounted server
#sub save_email() {
#    my $file = "$application";
#    open(FILE,">>  $CONFIG::DATA/$file.txt") or die("$! Cant open file\n") ;
#    flock FILE,LOCK_EX ;
#    print FILE "$email\n";

#    flock FILE,LOCK_UN ;
#    close(FILE) ;
#}
and i used to get error:
Software error:
Can't flock: No record locks available at /v/blah.com/testl/www/temp.cgi line 40


then i change the code to :
sub save_email() {

   my $file = "$application";
   die "couldn't lock file $file\n" unless file_lock($file);

   open FILE, ">>  $CONFIG::DATA/$file.txt" or die "$!\n";
   print FILE "$email\n";
   close FILE;

  die "couldn't remove lock\n" unless file_unlock($file);

}

sub file_lock {
    $timeout = 0;

    while($timeout++ < 2400){
     if( -f "$CONFIG::DATA/$_[0].lock" ){
         select(undef, undef, undef, 0.2);
     } else {
         open LF, ">$CONFIG::DATA/$_[0].lock" or die "$!\n";
         close LF;
         return 1;
       }
    }
    return 0;
}

sub file_unlock {
    return unlink "$CONFIG::DATA/$_[0].lock";
}


STILL, I am getting the same error:

What locking mechanism should i use
LVL 1
shahrahulbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

shahrahulbAuthor Commented:
basically the line 40 contains my $c = File::CounterFile->new("$CONFIG::DATA/$file");

so i am not sure is it because of this line 40 it is throwing th eerror or is it because of the above code
shahrahulbAuthor Commented:
and the line 41 is $c->inc;

basically i m keeping track of number of hits to this url


THanks

Rahul
shahrahulbAuthor Commented:
i m sure its the perl module to keep track of hits is causing the problem and not my subroutines

if i can't use $c = File::CounterFile->new("$CONFIG::DATA/$file");  then how can i keep track of counters

Rahul
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

stefan73Commented:
Hi shahrahulb,
flock on NFS mounts is problematic - as NFS is stateless, there's no real guarantee that flock will show the desired behaviour. This is the reason why many NFS implementations don't support locks.
You need a different approach - such as creating an own lock file. Again, this won't guarantee that there are no race conditions.

Cheers!

Stefan

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmcgOwnerCommented:
You can implement locks via hard links over NFS -- it's quite a bit slower and more cumbersome than the lockf primitives, but the creation of a hard link is still an atomic operation on the remote filesystem, so you can use that fact to build reliable locks.
ZiaTioNCommented:
Question has already been PAQ'd but I do not see the following suggestion above which incidentally is the proper way of using a lock on an NFS filesystem.

________________________
#!/usr/bin/perl -w

use strict;
use Fcntl ':flock';

open(FILE, ">", "somefile.txt") ||
   die "Unable to open somefile.txt: ($!)\n";
flock(FILE, LOCK_EX);
print FILE "Do something here!\n";
flock(FILE, LOCK_UN);
close(FILE);
________________________

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.