SPUG: File locking with flock and NFS

Doug Beaver doug at beaver.net
Thu Apr 5 03:46:08 CDT 2001


On Wed, Apr 04, 2001 at 03:09:59PM -0700, Bill Alford wrote:
> If every instance of the script is running on the same box, you can
> put empty lockfiles outside of the NFS volume.  E.g
> /tmp/my_app/target_file.lockfile
> 
> If they are running on multiple machines sharing the same volume, then
> you can touch a file in the volume and use the existence of the file
> as a lock.  You'd have to 
> 
> 1) check that the file doesn't exist, 
> 
> 2) sleep long enough that NFS propagation isn't an issue (seems like 1
> second should be long enough, but I've been on loaded systems when
> it's not), 
> 
> 3) make sure the lock file still isn't there and create it (put your
> ID (machine:pid) in the file and close it), 
> 
> 4) sleep the same duration or more, 
> 
> 5) check the file, if it exists and it still contains your ID, no one
> stomped you and it's your turn to run.
> 
> Admittedly, it's clunky.  But it should work almost all the time,
> especially if these are processes that run infrequently or slowly.  You
> can also put the above into a function that returns true when the lock is
> achieved and:

You mentioned that this is a hack and that it probably has gaping holes.
You were right, it does have a few holes.  :-/

I have heard that directory creation over nfs is atomic on many/most
implementations of nfs.  Since mkdir fails if the directory already
exists, you can use a directory as your lockfile and it should work as
long as directory creation is atomic on your implementation of nfs.  I
also did a search on search.cpan.org for lock and found lots of modules,
but my searches for 'lock nfs' and 'lock network' and didn't find
anything.  File::Flockdir said that File::Lockdir was described in the
perl cookbook, but I don't have my copy handy so I don't know if it's
what we are looking for.

You could also use fcntl if client and server supported the same locks.
You'll have to read your fcntl(2) manpage and figure out what flags and
features you have since they differ from OS to OS.

Doug
 
> On Wed, 4 Apr 2001, Thomas Whitney wrote:
> 
> > Greetings SPUG,
> > 
> > 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?
> > 
> > Thanks
> > Tom
> > 
> > 
> >  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> >      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/
> > 
> > 
> 
> 
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>      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/
> 
> 

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     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