home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / programm / 3333 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  3.0 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!slc6!trier
  2. From: trier@slc6.ins.cwru.edu (Stephen C. Trier)
  3. Newsgroups: comp.programming
  4. Subject: Re: Semaphores, Swap, or Test_And_Set
  5. Date: 18 Dec 1992 15:57:19 GMT
  6. Organization: Case Western Reserve University, Cleveland OH (USA)
  7. Lines: 49
  8. Message-ID: <1gsscvINNa2r@usenet.INS.CWRU.Edu>
  9. References: <1992Dec18.045022.15255@umbc3.umbc.edu>
  10. NNTP-Posting-Host: slc6.ins.cwru.edu
  11.  
  12. In article <1992Dec18.045022.15255@umbc3.umbc.edu> reagle@umbc8.umbc.edu (Mr. Joseph Reagle; MEYERHOFF (U)) writes:
  13. >    What hardware devices are supplied on MS-DOS(none?) or various
  14. >Unix machines to allow this?  Do any languages such as C or C++
  15. >provide (I guess concurent pascal does) provide mechanisms for the
  16. >protection of a shared variables, or CST?
  17.  
  18. Well, most systems end up implementing them in one way or another.
  19. Here's a simplistic way to do it, in pseudocode:
  20.  
  21.      Mask all interrupts
  22.      Do the atomic instruction
  23.      Unmask interrupts
  24.  
  25. This approach is actually quite common in MS-DOS.  I have seen it all
  26. over the place in MS-DOS network software.  The ensuing lost interrupts
  27. are nothing to sneeze at, though.  Interrupt masking should never be
  28. done when not absolutely necessary.
  29.  
  30. The 8086 family, however, supports a number of atomic operations.  XCHG
  31. is atomic.  Any other test-and-set instruction, like INC, DEC, and many
  32. others, can be made atomic through use of the LOCK prefix.  (It is not
  33. clear to me whether the LOCK prefix is necessary for single-CPU atomic
  34. operations.  It is obviously necessary for multiprocessor apps.)
  35.  
  36. Upon a suggestion from the net, I used INC and DEC to implement semaphores
  37. in CWRU's MS-DOS network kernel.  It took me all of 4 lines of assembly to
  38. implement simple non-blocking versions of the P and V primitives, 20 lines
  39. of assembly if they were callable from C.
  40.  
  41. Mainstream languages generally treat process coordination primitives as
  42. OS-dependent features, not language primitives.  A few, like the Modula
  43. family, have such primitives built in.  Concurrent languages, which are
  44. by definition the ones that have these features as part of the language
  45. spec, have not really caught on outside of the academic world.
  46.  
  47. Many Unix systems (especially those of the System V persuasion) have
  48. semaphores, shared memory, and other nifty IPC mechanisms as part of the
  49. OS services offered to applications.  BSD Unix seems to use more of an
  50. "everyone much use sockets" approach, which is, in many cases, sufficient.
  51. Of course, there are mutual exclusion mechanisms used within the OS
  52. kernel, and a user implementing a thread library at the application level
  53. is more than free to build his own coordination routines.  If he can
  54. implement a threads library, doing mutual exclusion will be easy.
  55.  
  56. -- 
  57. Stephen Trier                      "We want to offer you a price that you
  58. Network software type               just can't afford to take advantage of."
  59. Case Western Reserve University         - Sales blurb from HSC Software
  60. trier@ins.cwru.edu
  61.