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