SICP Exercise 3.47 semaphore b (in terms of atomic test-and-set operations)

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.

While implementing in terms of atomic test and set operations, the semaphore logic is similar to that of the mutex but instead of the binary (true or false) cell that the mutex uses,  the semaphore uses a number with a maximum possible value of 'size'.

When the semaphore is acquired, the number is incremented (up to a max of 'size') and when the semaphore is released, the number is decremented.

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