angst@batserver.cs.uq.oz.au (09/21/90)
I need to lock an entire NFS file for exclusive access (under SunOS 4.1). I tried using lockf, intending that the process should sleep until the file was free if it was already locked. It would gain a lock okay, but when it came to releasing that lock if there was a lock request pending, both processes would hang and eventually enter uninterruptable waits. So I thought I'd give fcntl a go. To start with I used F_SETLKW (1) for both the lock request and the lock release. This behaved in the same way as the lockf (not suprising really, as I expect lockf is implemented in terms of fcntl). Then I tried using F_SETLK (not F_SETLKW) for releasing the lock and it worked -- or so it seemed. Just to be thorough I had two processes on different clients requesting locks for a file that was on a different server to either client. I ran one process from my console client; the other from an rlogin to another client. The process that gained the first lock would unlock and exit with no problem; the other just slept indefinitely. This was the behaviour no matter which of the two gained the first lock. Can anyone out there explain this ? I don't usually read comp.sys.sun, so i apologise if I've just missed the largest ever discussion on how to lock NFS files in SunOS 4.1. Angst (1) F_SETLKW and F_SETLK are the commands passed to fcntl to gain a lock as specified in the lock structure. F_SETLKW will wait if there is another process that has exclusive access; F_SETLK returns immediately with an error.