chb has asked for the wisdom of the Perl Monks concerning the following question:

I read all the stuff about flock and its traps,and the biggest one is always at the end, where it always sounds like "Some versions of flock() can't lock files over a network (e.g. on NFS file systems)" etc. I need to lock files over NFS, so I read the stuff about fcntl, but it looked rather complicated. I found a different solution in the manpage open(2) in the section about O_EXCL. It goes like this:
sub lock { my $lockhelper="lockhelper-$ENV{HOSTNAME}-$$.LCK"; my *LOCK; my $nlink; open(LOCK, ">$lockhelper") or die "Couldn't open() $lockhelper: $!"; + print LOCK "$ENV{HOSTNAME};$$\n"; close(LOCK); link($lockhelper, "lockfile.LCK"); (undef,undef,undef,$nlink,undef,undef,undef,undef, undef,undef,undef,undef,undef) = stat($lockhelper); unlink($lockhelper); return $nlink==2; }
If this sub returns true, you have got the lock, it is nonblocking. My Question is: Is this really the way to do it if you have to deal with NFS, or is there a more elegant way ?
- chb

Replies are listed 'Best First'.
Re: File locking on NFS
by lhoward (Vicar) on Apr 04, 2001 at 17:52 UTC
    File locking in NFS is broken (with respect to how you'd expect it to work on a local filesystem). You'll either have to do some sort of lock-file hack (like you propose) or use a different networking filesystem (I thin that DFS and AFS handle this well, but that may not be an option for you). Instead of writing something yourself I recomend you use a module like LockFile::Simple, DotLock or IPC::Locker, which is designed to manage lockfiles, these modules can still have problems w/ NFS due to possible race conditions (see the individual module's docs for details). Writing locking code yourself is tricky and subject to problems unless you're really careful.

      File locking in NFS is broken

      I recall that being true a long time ago and then I recall it working quite well after that. Has perhaps Linux brought back the badly broken NFS lock?

      You'll either have to do some sort of lock-file hack

      Last I checked, NFS didn't support atomicity of file creation, deletion, nor rename, which would make a lock-file hack not work well under NFS.

      So am I just really out of date?

              - tye (but my friends call me "Tye")
        I am not positive on rename, but I believe that it will actually be atomic on NFS partitions as long as the data does not cross file-system boundaries. If you know of solid documentation on this one way or another, that would be good.

        As for locking, Linux dropped the use of fcntl/lockf for flock a while ago due to some nasty recurring bugs. So flock does not work over NFS for Linux any more. Also fcntl did not when I tried it work totally smoothly. But I did not tinker much, that was a while ago, and I know that in the last few 2.2 releases (and of course in the new 2.4 series) NFS was improved greatly.

Re: File locking on NFS
by Anonymous Monk on Apr 04, 2001 at 22:39 UTC
    The flock() system call does not work over NFS, the fcntl() system call does lock files over NFS. The perl flock() function can be configured at compile time to either use the flock() system call or the fcntl() system call (it will default to using flock() if available). Offhand, I forget which variable to change to do this, but I've done it before so I'm sure it's documented in a relatively easy to find location.
      Found it, you can pass -Ud_flock to Configure.