home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / CNEWS / _CNEWS.TAR / usr / doc / cnews / docs / newslock < prev    next >
Encoding:
Text File  |  1994-09-02  |  3.1 KB  |  112 lines

  1. .Ch "Locking in C News"
  2. .Ix lock
  3. .LP
  4. Several parts of
  5. C News
  6. need some way of locking parts of the news
  7. subsystem against concurrent execution.
  8. Various system-specific locking system calls exist,
  9. but none of them
  10. is truly portable,
  11. and most of them provide far more functionality than
  12. we need.
  13. .LP
  14. C News
  15. .Ix link
  16. locking uses the \fIlink\fR(2) system call and pre-agreed names.
  17. \fILink\fR has the necessary characteristic for safe locking:
  18. it is an atomic test-and-set operation.
  19. Furthermore,
  20. it exists in all Unixes.
  21. .LP
  22. .Ix lock names
  23. All locks are created in the NEWSCTL directory
  24. (see \fIConfiguration Mechanisms in C News\fR for where this directory
  25. is to be found and how programs can determine this)
  26. and have names starting with `LOCK'.
  27. To acquire a lock,
  28. .Ix lock acquiring
  29. .Ix LOCK
  30. .Ix files LOCK
  31. first create a temporary file in NEWSCTL with a name
  32. of the form `L.\fIn\fR', where \fIn\fR is your process id.
  33. You are urged to also write your process id,
  34. in decimal ASCII,
  35. into this file.
  36. Then attempt to link the temporary file to `LOCK\fIx\fR',
  37. where \fIx\fR is chosen based
  38. on what sort of locking you wish to do.
  39. Existing lock names are:
  40. .TS
  41. center;
  42. ll.
  43. LOCK    relaynews, modifications to control files
  44. LOCKinput    input subsystem processing spooled input
  45. LOCKbatch    batcher preparing batches
  46. LOCKexpire    expire expiring articles
  47. .TE
  48. If the link fails,
  49. sleep and try again.
  50. If it succeeds,
  51. proceed.
  52. The temporary file may be removed then or at the same time as the lock
  53. is removed.
  54. Programs are expected to make a determined effort to remove lock files
  55. when they terminate,
  56. normally or as a result of signals.
  57. .LP
  58. .Ix Unix "System V"
  59. .Ix "System V" breakage
  60. .Ix ln
  61. Shell programs have an additional problem in that System V has broken
  62. \fIln\fR(1) so that it removes a pre-existing destination file.
  63. C News
  64. therefore provides a pure,
  65. simple locking program under
  66. the name NEWSBIN/newslock
  67. .Ix newslock
  68. (if the recommendations in
  69. \fIDirectory Layout and PATH in C News\fR are followed,
  70. this will
  71. automatically be in the search path of shell programs).
  72. Usage is `newslock\ tempfile\ lockfile';
  73. exit status is 0 for success,
  74. 1 for failure,
  75. 2 for wrong number of arguments.
  76. No messages are printed for normal failure,
  77. so no redirection of output is needed.
  78. .LP
  79. A suitable locking procedure for a shell file using the standard
  80. configuration facilities is:
  81. .DS
  82. .ft B
  83. lock="$NEWSCTL/LOCKxxx"        # modify name as appropriate
  84. ltemp="$NEWSCTL/L.$$"
  85. echo $$ >$ltemp
  86. trap "rm \-f $ltemp ; exit 0" 0 1 2 15
  87. while true
  88. do
  89.     if newslock $ltemp $lock
  90.     then
  91.         trap "rm \-f $ltemp $lock ; exit 0" 0 1 2 15
  92.         break
  93.     fi
  94.     sleep 30
  95. done
  96. .ft    
  97. .DE
  98. A template of this form can be found in the file \fInewslock.sh\fR.
  99. .LP
  100. Although there are various thorny questions associated with breaking
  101. locks by dead programs,
  102. .Ix lock clearing
  103. reboot is a time when surviving locks are
  104. definitely invalid.
  105. (Although there are problems even here if a networked group of systems
  106. are not rebooted as a unit.)
  107. For this and other reasons,
  108. a system running C News should execute
  109. .Ix newsboot
  110. .Ix /etc/rc
  111. NEWSCTL/bin/newsboot at reboot time (e.g. from \fI/etc/rc\fR).
  112.