problem with sysopen() on nfs

M

Michele Dondi

I want to implement a locking system for a script that shouldn't allow
more than one instance on each machine of a cluster by means of a
lockfile. The relevant code snippet is:

my $lockfile = "$lockdir/$pfx-$host";
sysopen my $lock, $lockfile, O_CREAT | O_EXCL
or die "$0 already running on $host";

Now I'm aware that file operations on nfs are not atomic, but this is
not a big issue since *in this case* the probability of collisions if
ridiculously small.

However it seems that I have another problem, and a more serious one.
Namely the lockfile is actually created in the specified directory,
but it is named .nfs* (I mean, it matches that wildcard...)

Am I missing something obvious? Any workaround/alternative strategy?


TIA
Michele
 
J

Joe Smith

Michele said:
However it seems that I have another problem, and a more serious one.
Namely the lockfile is actually created in the specified directory,
but it is named .nfs* (I mean, it matches that wildcard...)

Am I missing something obvious? Any workaround/alternative strategy?

The workaround is to not delete the file while it is open for reading.

When a local file is openned for reading, and it then deleted (unlinked),
the file is still there even though it does not have a directory entry.
So the process that openned the file can continue reading this now
nameless file.

When a file on an NFS mount is openned for reading, and then is deleted
on the NFS client, the client tells the NFS server to rename the file
to be .nfs{something}, so it can still be read on the client.

To avoid the symptoms you're seeing, just be sure to close all open
file handles that reference the lockfile before deleting the lockfile.
-Joe
 
M

Michele Dondi

The workaround is to not delete the file while it is open for reading.

Huh?!? I don't *think* I'm deleting it anywhere. Granted, I've set a
pair of signal handlers and an END block in which I do delete it
(before closing it). But they shouldn't have been called at all...

[snip explanation]
To avoid the symptoms you're seeing, just be sure to close all open
file handles that reference the lockfile before deleting the lockfile.

D'Oh! Now that I think of it I make my program C<fork and exit> to go
in bg, so the END block *is* executed. I'll have to change the logic
of the script.


TY,
Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top