Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!decwrl!sgi!shinobu!odin!thestepchild!rhartman From: rhartman@thestepchild.sgi.com (Robert Hartman) Newsgroups: comp.unix.shell Subject: Re: Creating a lock file in csh? Message-ID: <1991Apr16.175347.1082@odin.corp.sgi.com> Date: 16 Apr 91 17:53:47 GMT References: <1991Apr15.205654.26253@unhd.unh.edu> Sender: news@odin.corp.sgi.com (Net News) Organization: Silicon Graphics, Inc., Mountain View, CA Lines: 39 In article <1991Apr15.205654.26253@unhd.unh.edu> al@unhd.unh.edu (Anthony Lapadula) writes: >I'll soon be writing a script (most likely in csh) that has at least >one critical section ... > >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. > >-- Anthony (uunet!unhd!al, al@cs.unh.edu) Lapadula Seems to me you could make this work in csh as follows: set noclobber (echo "${user}: $$ `date`" > $file.lock) >& /dev/null if ($status == 0) then if ($$ == "`awk '{ print $2 }' $file.lock`") then # enter critical section # ... # exit critical section else echo "$file in use by `cat $file.lock`" exit 1 endif else echo "$file in use by `cat $file.lock`" exit 1 endif unset noclobber Noclobber insures that if the file exists your attempt to overwrite it wil fail. The redirect to /dev/null is a standard way to peel off the standard error message (the "else" statements are more meaningful). I suppose that there are more efficient comparisons than ($status == 0), but that one is quite clear. The nested if double-checks to make sure that the current process actually holds the lock. This is probably overkill, but it's easy enough to comment out or delete. Can't see how NFS could matter here. -r