SPUG: File locking with flock and NFS
Bill Alford
billa at willapabay.org
Wed Apr 4 17:09:59 CDT 2001
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:
sub get_hackish_lock {
my $file = shift;
while (! got_hackish_lock($file)) {
sleep(5*60); # assuming that once the lock is achieved
# the processing takes about 5 minutes
}
}
get_hackish_lock($file_to_lock);
# do your work
release_hackish_lock($file_to_lock);
There are probably gaping holes in this, but it might get you running well
enough that it is sufficient.
Bill
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/
More information about the spug-list
mailing list