Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

locking a NFS file

by Helter (Chaplain)
on Oct 30, 2002 at 16:24 UTC ( [id://209128]=perlquestion: print w/replies, xml ) Need Help??

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

I've super searched, I've google searched, I've read FAQS, and I cannot find a solution.

I'm helping to write an internal tool (portability is not very important) that needs to lock some log files.
flock, lockd, and sysopen all do not support NFS, at least from the docs I've read.

I work for a large corporation so the 2 most typical answers do not apply, (don't use NFS, and re-compile Perl with some flags to make flock work using fcntl).

Using modules is a little touchy as well, if I can find a simple solution that would work better (cross site issues of installing modules, it's a nightmare).

From what I've read fcntl should do what I want (NFS is up to date and Linux boxes were running on got upgraded to 2.4 not too long ago).

I have not been able to find a good example of using fcntl to lock a file. Can anyone help?

Thanks!

Replies are listed 'Best First'.
Re: locking a NFS file
by artist (Parson) on Oct 30, 2002 at 16:33 UTC
      I did see that module, it seems like overkill (a little), and like I said using a module is not the best solution for my situation. You have my thanks Artist.

      We are trying to play with fcntl in a test script, but can't get it do the right thing, we don't quite understand what parameters it needs.

      So does anyone have experience using fcntl?
        AFAIK using fcntl to lock a file on an NFS partition either does not work at all, or is extremely iffy.

        Personally I've used the technique described in Stevens' Unix Network Programming, which goes something like this:

        Create a temp file on the NFS partition
        Try to link this temp file to a lock file (using the link() call)
        If the link fails with EEXIST someone else already has the lock, so sleep for a bit and try again
        If the link succeeds you've got your lock, so delete the tempfile
        Releasing a lock is simply a matter of deleting the lock file.

        As lock files don't get automatically cleaned up you in case of abnormal termination you have to add some form of timeout and stale lock handling.

        All in all I'd say that the NFSlock module suggested above is probably the best solution (unless you want to roll your own to work around new module installation issues...)

        Michael

      We ended up downloading the NFSLock module and including it in the distribution. Seems to work in our tests.
      Thanks!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://209128]
Approved by valdez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-25 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found