Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!pasteur!ucbvax!decwrl!sun!pitstop!sundc!seismo!uunet!ingr!crossgl From: crossgl@ingr.com (Gordon Cross) Newsgroups: comp.unix.questions Subject: Re: Shared Libraries -- experiences etc. Message-ID: <3747@ingr.com> Date: 2 Feb 89 09:08:30 GMT References: <81584@felix.UUCP> Reply-To: crossgl@ingr.UUCP (Gordon Cross) Organization: Intergraph Corp. Huntsville, Al Lines: 26 In article <81584@felix.UUCP> martin@felix.UUCP (Martin McKendry) writes: > > o) Are semaphores the only appropriate technique for implementing > mutual exclusion on small objects? For example, if I had a > large number (say, 2000) of objects that were small (say, 20 bytes), > and I wanted to put locks on each object. Assume that there > are many processes active in the total structure, but they > tend not to interfere with one another. Are semaphores the > mechanism of choice for this problem? It seems like a lot > of overhead to allocate one semaphore per record. Most machines have an instruction called a "test and set" operation. These instructions allow you to test the current value of a memory location and simultaneously (ie. non-interruptable) set a bit in that location. Using this instruction you can implement what is called a software lock. This approach for locking many small data items (presumably kept in memory that is shared between multiple processes) is MUCH MUCH better than semaphores (even if it was possible to allocate 20,000 of them). In System V there is no routine already provided to use software locks (I don't know about BSD and others) so I had to write my own in assembler (quite small just a couple of instructions). -- Gordon Cross UUCP: uunet!ingr!crossgl "all opinions are 111 Westminister Way INTERNET: crossgl@ingr.com mine and not those Madison, AL 35758 MA BELL: (205) 772-7842 of my employer."