SPUG: File locking with flock and NFS

Andrew Sweger andy at n2h2.com
Wed Apr 18 22:34:36 CDT 2001

For the free Unix users, there is a library called lockfile
(liblockfile.so) that provides NFS-safe file locking using known atomic
NFS operations (file/directory creation). On Debian systems, this is
delivered in a package called liblockfile1 which provides a command-line
utility called dotlockfile for this purpose. This package is maintained by
Miquel van Smoorenburg. I don't know if he is also the author (didn't
look). I'm sure source is available somewhere. If you wish to wrap the
library in a Perl module, you will need the liblockfile-dev package and
would do something like:

h2xs -x -n File::Lock::NFS -p lockfile_ -v 0.01 lockfile.h -llockfile

But I'll bet there's an easier way to do it using Inline that I just don't
know. So far, I haven't seen anyone publish this particular wrapping.

In your case, it would be simpler and more appropriate to write a Perl
module that simply performs the system calls directly (to create/monitor
semaphore lockfiles). The algorithm is pretty well documented in the
packages above.

The timeouts suggested by Bill ('sup, Bill?) will fall prey to local NFS
attribute cache settings which can vary or be unbearably long. Come on.
You all remember the fiasco at the Perl Golf Apocalypse, right? Everyone
was sitting around waiting for the directory permissions to change so
folks could get in and start the contest.

If you need very short duration file locks (under two minutes), NFS might
still be a problem if you will have competing processes waiting for a lock
to expire due to attribute caching. Do not trust the NFS locking daemon.

On Apr 4, 2001 @ 10:13am, Thomas Whitney wrote:

> I am developing Perl applications where I need to do file locking. These will be
> running on SunOS 5.5.1 with a NFS file server. All the books and references I
> have researched say that flock will not work with NFS, but none offer any
> alternatives. Does anybody know if this is true, and if so what alternatives are
> there?

 Andrew B. Sweger <andy at n2h2.com> |  N2H2, Incorporated
 Manager, ICE Development Dept.   |  900 Fourth Avenue, Suite 3600
 Operations Division              |  Seattle WA 98164-1059
 v=206.336.2947  f=206.493.0906   |  http://www.n2h2.com/

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/

More information about the spug-list mailing list