home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / shell / 3216 < prev    next >
Encoding:
Internet Message Format  |  1992-07-30  |  4.3 KB

  1. Path: sparky!uunet!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!friedman
  2. From: friedman@gnu.ai.mit.edu (Noah Friedman)
  3. Newsgroups: comp.unix.shell
  4. Subject: Re: stale lock removal
  5. Message-ID: <FRIEDMAN.92Jul30162914@nutrimat.gnu.ai.mit.edu>
  6. Date: 30 Jul 92 20:29:14 GMT
  7. References: <1992Jul29.161417.2473@ddsw1.mcs.com>
  8. Sender: news@ai.mit.edu
  9. Organization: Free Software Foundation, 675 Mass Ave. Cambridge, MA 02139
  10. Lines: 86
  11. In-reply-to: dattier@ddsw1.mcs.com's message of 29 Jul 92 16:14:17 GMT
  12. X-Disclaimer: The article, except for quotations, is public domain.
  13.  
  14. In article <1992Jul29.161417.2473@ddsw1.mcs.com> dattier@ddsw1.mcs.com (David W. Tamkin) writes:
  15. >Say a process finds that another process has the lock and it's going to wait
  16. >a bit and then try again.  How can it determine whether the process that has
  17. >the lock is actively using it or has stalled?
  18.  
  19.    Most programs simply don't.  They'll wait forever (either by checking
  20. periodically, implementing some sort of semaphore, or whatever) until the
  21. lock goes away.  After all, there are lots of reasons why a program may be
  22. wedged (not merely zombified).  Perhaps in some circumstances you can use
  23. some empirical guestimates for how long a process can reasonably hold a
  24. lock, and then steal it if it takes longer than that.  Depends entirely on
  25. the nature of the data and the programs, I suppose (and you may still be
  26. subject to nasty but unlikely race conditions).
  27.  
  28.    Emacs has an advisory locking mechanism similar to the one you described
  29. later in your article.  The lockfile contains the process-ID of the emacs
  30. process locking the file.  When emacs notices that a file has a lock, it
  31. checks to see if the other process still exists (using a kill 0 and looking
  32. at the resulting errno).  This implementation fails miserably with
  33. NFS-mounted files, because they could easily be edited on remote machines.
  34. We've been thinking informally about a new locking mechanism, but no solid
  35. ideas have been proposed.  My guess is that emacs 19 will use the same
  36. fairly brain-dead locking mechanism which was fine when everyone worked on
  37. a single vax without networked file systems, but is now obviously
  38. inadequate.  *sigh*.  Anyone who has ideas, please send them to me!
  39.  
  40. >Potentially process A could find that process B had the lock and go into these
  41. >tests; B could end and process C could get the lock but not yet have created
  42. >the signature file by the time A tests for a signature file to find out which
  43. >process has the lock.
  44.  
  45.    You should never assume you have a lock until you have successfully
  46. negotiated whatever protocol is necessary to demonstrate you have it.
  47.  
  48. >Finally, is there some good way simply to tell the age of a lock (if not one
  49. >made with /etc/link, at least one make with mknod p or with mkdir)?  If a
  50. >lock is older than a certain age, one can figure that it is stale.  Various
  51. >ls options will display last inode modification time or access time, but how
  52. >does one compare them to the current time efficiently?
  53. >
  54. >Any suggestions?  This will have to be done with shell scripts or existing
  55. >utilities.
  56.  
  57.    I wrote the following for bash, though I admit I cheated by using perl
  58. to get at various system calls.  :-)
  59.  
  60.    To use these, you basically just stat the file and get the mtime (or
  61. atime, whatever), and use "time2" to get the current system time.  Then you
  62. can just do some simple arithmetic using expr or builtin bash routines to
  63. get the age of a file. 
  64.  
  65.    Getting the age of locks that were acquired with lockd may be more
  66. difficult.  :-)
  67.  
  68.  
  69. # dev ino mode nlink uid gid rdev size atime mtime ctime blksize blocks
  70. #
  71. # Converts mode to canonical 3-digit octal number (type bits can be gotten
  72. # with "test" anyway)
  73. function stat ()
  74. {
  75.     perl - "$@" <<- '__EOF__'
  76.     for ($i = 0; $i <= $#ARGV; $i += 1)
  77.       {
  78.         @st = stat("$ARGV[$i]");
  79.         $st[2] = $st[2] & 0777; 
  80.         printf ("%ld %u %o %d %lu %lu %ld %lu %lu %lu %lu %lu %lu\n", @st);
  81.       }
  82.     __EOF__
  83. }
  84.  
  85. # WARNING: In order to optimize computations after the first call to this
  86. # function, the `SECONDS' magic shell variable is used to compute changes
  87. # from the previous value.  If this variable is ever assigned (or unset),
  88. # time2 will no longer provide the correct value.
  89. #:end docstring:
  90.  
  91. function time2 ()
  92. {
  93.     # Calling an external program is slow
  94.     if [ "${_time_seconds+set}" != "set" ]; then
  95.        _time_seconds=$(perl -e 'print(time());')
  96.     fi
  97.  
  98.     echo $[ _time_seconds + SECONDS ]
  99. }
  100.