# Semaphores, Mutexes, and a poorly worded programming book

This topic is 3031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 on other sites
Quote:
 Original post by LordShadeThink 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 on other sites
Moved to General Programming by OP request.

##### Share on other sites
It sounds like a typo to me.

##### 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 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?).

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5

• 30
• 22
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001800
×