locking files over NFS 
Author Message
 locking files over NFS

hi people

i have the following situation:

executable "program" is on file system "FS" mounted on machines A and B
the program uses lock files to make sure of mutual exclusion during resource
sharing, which might be writing to a file (in which case file should not be
corrupted during writing). the lock files are also formed on file system "FS"
accessible by both A and B.

now when i run "program" from A and a lock file is formed, the existance of the
lock file may not be immediately reflected on B. so if i almost immediately
start the "program" on B, it may also attempt to write to the data file and
corrupt it.

is there a unix utility to take care of this sitation? does flock take this
problem into account and resolve it? iare there any suggestions that any of u
would like to make as to how to resolve this problem? i m aware of the idea of
waiting some time (like 10 secs) before checking the existance of the lock
file, but that kinda looks kludgy to me and i was hoping for a more elegant
soln.

Kuntal M. Daftary   Cisco Systems Inc
Ph: 408 527 9789    170 W. Tasman Dr



Tue, 07 Mar 2000 03:00:00 GMT  
 locking files over NFS



Quote:
>executable "program" is on file system "FS" mounted on machines A and B
>the program uses lock files to make sure of mutual exclusion during resource
>sharing, which might be writing to a file (in which case file should not be
>corrupted during writing). the lock files are also formed on file system "FS"
>accessible by both A and B.

>now when i run "program" from A and a lock file is formed, the existance of the
>lock file may not be immediately reflected on B. so if i almost immediately
>start the "program" on B, it may also attempt to write to the data file and
>corrupt it.

>is there a unix utility to take care of this sitation? does flock take this
>problem into account and resolve it? iare there any suggestions that any of u
>would like to make as to how to resolve this problem? i m aware of the idea of
>waiting some time (like 10 secs) before checking the existance of the lock
>file, but that kinda looks kludgy to me and i was hoping for a more elegant
>soln.

I presume you are using 'NFS' to 'mount' your filesystem. There are a number
of things that NFS stands for - Sun would have you believe that it means
'Network File System', but those of us who use it prefer 'Not a filesytem'
or even 'No F***ing Security'. You have discovered one of the reasons for this.

There are a number of things you can do. The best one is to use an NFS call
which is reasonably atomic, such as 'mkdir'. You don't look for the lock
directory at all, you just try and make it. If the mkdir succeeds, then
you have the lock, otherwise someone else does. e.g.

if mkdir lock
then
        : we have the lock
else
        echo 'the directory "lock" exists - exiting' >&2
        exit 99
fi

The 'flock' is a bodge on top of a bodge. It is better than nothing but for
your problem using mkdir would be better.
Icarus



Wed, 08 Mar 2000 03:00:00 GMT  
 locking files over NFS

Quote:

> executable "program" is on file system "FS" mounted on machines A and B
> the program uses lock files to make sure of mutual exclusion during resource
> sharing, which might be writing to a file (in which case file should not be
> corrupted during writing). the lock files are also formed on file system "FS"
> accessible by both A and B.

> now when i run "program" from A and a lock file is formed, the existance of the
> lock file may not be immediately reflected on B. so if i almost immediately
> start the "program" on B, it may also attempt to write to the data file and
> corrupt it.

> is there a unix utility to take care of this sitation? does flock take this
> problem into account and resolve it?

> Kuntal M. Daftary   Cisco Systems Inc
> Ph: 408 527 9789    170 W. Tasman Dr


I would have thought you would have problems both when using NFS and
when not. The only reliable method for locking files is to to use the
'c' functions provided by the development system in your executable
program.

If you wrote the program or have access to the source I suggest you
use the locking 'c' functions in the program. If not complain to your
supplier that the program is not programmed properly for true
multi-user operation.

NOTE: NFS file locking is 'advisory' rather than the 'manadtory'
locking available when directly accessing a disk. It should however
all work fine as long as the NFS drive does not 'dissappear' while the
program is running.

Hope this helps

Al
--

CSC Ra Group Ltd.
In this posting/mail I am speaking for me, not my employer



Wed, 08 Mar 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Locking files across NFS

2. File Locking over NFS

3. Need to Lock files (NFS)

4. Seeking working file locking on NFS Solaris

5. fcntl() for NFS file-locking

6. fcntl () for NFS file locking

7. NFS locking, the right way to do it?

8. Samba, NFS locking

9. Fcntl locking and NFS.

10. Best locking method for NFS - flock() OK?

11. PERLFUNC: flock - lock an entire file with an advisory lock

12. PERLFUNC: flock - lock an entire file with an advisory lock

 

 
Powered by phpBB® Forum Software