Sign in to follow this  
way2lazy2care

Semaphores, Mutexes, and a poorly worded programming book

Recommended Posts

READ THROUGH. I AM NOT ASKING FOR A SOLUTION, JUST A CLARIFICATION. Ok. I am doing a Consumer/Producer problem in a programming book for one of my classes. To my understanding, a Mutex is a binary semaphore (the book and other sources have said this). A similar problem is gone over earlier in the chapter, where semaphores are used to to say how many spots are available in a buffer and a binary semaphore is used to grant access to the buffer. The problem I am doing has a buffer, but instead of locking the whole buffer it wants you to lock only individual spaces in the buffer. Here is how the problem is worded: "The solution presented in 6.6.1 uses three semaphores 'empty' and 'full' which count the number of empty and full slots in the buffer, and 'mutex' which is a binary semaphore that protects the actual insertion or removal of items in the buffer. For this project, standard counting semaphores will be used for empy and full, and a mutex lock rather than a binary semaphore will be used to represent mutex..." earlier in the chapter it says this: "On some systems, binary semaphores are known as mutex locks, as they provide mutual exclusion" So in the problem, what does it mean by a mutex lock? I thought mutex locks were the same as binary semaphores, which would make the above sentences redundant. I am so confused by the way this book words this problem. I am wondering if it wants me to have a mutex for each slot in the buffer or what it's supposed to be.

Share this post


Link to post
Share on other sites
Think of a semaphore as a "count of accesses" which, when set to a maximum of 1, turns into a Mutex.

Using a semaphore to determine how much allocated space is available is loony.

Share this post


Link to post
Share on other sites
Quote:
Original post by LordShade
Think of a semaphore as a "count of accesses" which, when set to a maximum of 1, turns into a Mutex.

Using a semaphore to determine how much allocated space is available is loony.


while I do not disagree, I am still quite stuck on the mutex lock vs binary semaphore thing and i can't make much progress until I know that part.

If I had to guess I'm guessing their using the empty/full semaphores to get the producer/consumer threads to wait while the buffer is full/empty, not to see how many open buffer spots there are.

Share this post


Link to post
Share on other sites
Quote:
Original post by way2lazy2care
"The solution presented in 6.6.1 uses three semaphores 'empty' and 'full' which count the number of empty and full slots in the buffer, and 'mutex' which is a binary semaphore that protects the actual insertion or removal of items in the buffer. For this project, standard counting semaphores will be used for empy and full, and a mutex lock rather than a binary semaphore will be used to represent mutex..."

Here's my guess: it is a programming book, and has 2 sets of terminology to use - firstly, the generic terms for the concepts as used in Computer Science, and secondly, the specific objects your language uses to represent the concepts. The "for this project..." part seems to be saying that instead of using a "binary semaphore" object, whatever that might be in the book, use a "mutex lock" object. The two are essentially equivalent but appear to be different objects as far as the language in your book is concerned, which is understandable because a lock usually has a slightly simpler interface than a semaphore.

Share this post


Link to post
Share on other sites
This is how I read it.

The solution in 6.6.1 used three semaphores.
Semaphore 1: 'full' semaphore
Semaphore 2: 'empty' sempahore
Semaphore 3: 'mutex' semaphore (aka, binary semaphore)

Now, for this project, they want you to use counting semaphores for empty and full (just like in the solution for 6.6.1), and a "real" mutex for locking access to the buffer instead of a binary semaphore.

Basically, re-do solution 6.6.1 using a mutex in place of the binary semaphore. Semaphores generally have higher overhead than mutexes, so if you're using a semaphore as a mutex, it's better to just use a mutex.

It's unclear as to whether or not the solution in 6.6.1 locked the whole buffer, or only individual spaces in the buffer, or if that's even required (did you infer this need, or is it explicitly spelled out that way?).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this