home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- Path: sparky!uunet!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!nucsrl!ddsw1!dattier
- From: dattier@ddsw1.mcs.com (David W. Tamkin)
- Subject: stale lock removal
- Message-ID: <1992Jul29.161417.2473@ddsw1.mcs.com>
- Organization: Contributor Account at ddsw1, Chicago, Illinois 60657
- Date: Wed, 29 Jul 1992 16:14:17 GMT
- X-Disclaimer: Material posted in this article is the sole responsibility of
- the poster and does not represent MCSNet or the system owners.
- Lines: 50
-
- The discussion of file locking from the shell has raised a question in my
- mind.
-
- Say a process finds that another process has the lock and it's going to wait
- a bit and then try again. How can it determine whether the process that has
- the lock is actively using it or has stalled?
-
- One thing I considered is to make the lock with mkdir and then > $lockdir/$$
- to put the locking process's signature on it. Then, if a process finds a
- lock in existence, it can ps -p `(cd $lockdir; echo *)`, but [1] a stalled
- process might not necessarily disappear from the ps output, and [2] the
- process that had the lock when mkdir failed could have given it up since.
- Maybe [2] isn't a problem, because then ps -p would still return a nonzero
- exit status.
-
- Potentially process A could find that process B had the lock and go into these
- tests; B could end and process C could get the lock but not yet have created
- the signature file by the time A tests for a signature file to find out which
- process has the lock. Then, for absence of a signature file, A would get a
- nonzero exit from ps -p, think B had gone zombie, steal a lock that really
- belongs to process C, and make a mess. [As I muse, I see some relief to that
- if a process giving up its lock does rm -r $lockdir so that the directory and
- the signature file disappear together; that way an empty lock directory could
- be recognized as one where a fresh lock hasn't been signed yet.]
-
- Another thought I had was mkdir lockdir.* and then mv lockdir.* lockdir.$$;
- if another process has the lock, sh will expand the asterisk in the mkdir
- command to the existing name and mkdir will fail, even if the process that
- has the lock hasn't yet renamed it (in which case the asterisk will be
- expanded to a literal asterisk). Unfortunately, in sh another fork will be
- needed (expr or tr, for example) to get the owning process out of the lock
- name.
-
- I don't want an unremoved stale lock to make another process to keep
- repeating its sleeps until the system goes down. One could put a numeric
- limit on the number of sleep cycles it takes and then, if the signature
- hasn't changed, figure the older process has stalled and then steal the lock.
- That does allow for a race condition, though.
-
- Finally, is there some good way simply to tell the age of a lock (if not one
- made with /etc/link, at least one make with mknod p or with mkdir)? If a
- lock is older than a certain age, one can figure that it is stale. Various
- ls options will display last inode modification time or access time, but how
- does one compare them to the current time efficiently?
-
- Any suggestions? This will have to be done with shell scripts or existing
- utilities.
-
- David W. Tamkin Box 59297 Northtown Station, Illinois 60659-0297
- dattier@ddsw1.mcs.com CompuServe: 73720,1570 MCI Mail: 426-1818
-