SICP Exercise 3.47 semaphore a (in terms of mutexes)

Exercise 3.47.  A semaphore (of size n) is a generalization of a mutex. Like a mutex, a semaphore supports acquire and release operations, but it is more general in that up to n processes can acquire it concurrently. Additional processes that attempt to acquire the semaphore must wait for release operations. Give implementations of semaphores

a. in terms of mutexes

b. in terms of atomic test-and-set! operations.

SOLUTION

The code is here.

The make-semaphore procedure object manages within its local state, a set of mutexes in two lists: a free list and an acquired list. (See code documentation for details of the implementation.)

Instead of a serializer, which does not make sense for a semaphore, I have implemented a concurrency limiter that internally maintains the semaphore.

The test shown in the program tries to run ten threads simultaneously with a concurrency-limit of 3. The test results reflect this fact. Only 3 threads are able to run at a time.

Comments

Popular posts from this blog

SICP Exercise 2.56 differentiation rule

SICP Exercise 1.28 (Miller-Rabin Test)

SICP Exercise 4.18 a alternative strategy for interpreting internal definitions