Creating a lock file in csh?

Tom Christiansen tchrist at convex.COM
Tue Apr 16 09:04:27 AEST 1991


>From the keyboard of al at unhd.unh.edu (Anthony Lapadula):
:I'll soon be writing a script (most likely in csh) that has at least
:one critical section (updating a file).  I need to ensure that only
:one executing copy of the script actually accesses the file at any
:given time.
:
:I had thought that creating a lock file -- and checking for its presence
:when starting up -- would be a reasonable way to solve the problem.
:I can't seem to figure out how to do it (short of invoking a C program
:that invokes open(2) with O_EXCL).  We're trying to keep the script
:machine-independent, so the open(2) solution is out.
:
:I looked at mktemp(1), but it also seems insufficient.
:
:Lastly, does it matter that we're using NFS?

I would strongly counsel against using csh.  Use a Bourne-compatible
shell for shell programming.  Yes, it makes a difference whether you're
using NFS: old assumptions about idempotency of operations (like create
or unlink or O_EXCL) are no longer valid; you have to go through the 
lockdaemon_from_hell to make these things work.  Of course, not all
hosts have a lockdaemon, so so much for machine-independence.  You
could use fcntl() so it works both w/ and w/o a lockd, but old BSD
hosts don't do file-locking that way.  And anyway, you can't get 
to fcntl() from a shell script anyway.  

You *can* get at these functions from perl scripts, and you still
get machine-independence because you don't have to recompile.
You might even use #ifdef __convex__ or whatever if you really want.

--tom



More information about the Comp.unix.shell mailing list