[comp.unix.shell] Creating lock files revisited

rhartman@thestepchild.sgi.com (Robert Hartman) (04/20/91)

In article <1991Apr20.020506.8656@twinsun.com> eggert@twinsun.com (Paul Eggert) writes:
>rhartman@thestepchild.sgi.com (Robert Hartman) writes:
>
>>BTW, does this imply that SCCS and RCS can also fail over NFS?
>>I'm not aware of them using the NFS lock manager!
>
>I don't know about SCCS, but RCS 5.5 uses a slightly different
>technique.  The lock file never exists in writable form, avoiding the
>NFS protocol bug that Jonathan Kamens described.  A similar method can
>be used in the shell.  For example (Bourne shell):
>
>	if (umask 777;  echo >lock)
>	then
>		Perform whatever operation required locking.
>		rm -f lock
>	else
>		Report that you could not obtain the lock.
>	fi

Ah, I see!  As long as write access is disallowed, the redirection does not
overwrite the file.  Since there are no writes pending in the NFS server,
there is no way to clobber the lock file once created.  Whoever gets it
first keeps it!  Very elegant!

Well, this seems to answer the person who wanted to use a lock file for a
critical section in a shell script.  Thanks!  That's what I love about UNIX.
There's usually always a way to get it to do what you want.

-r