Reputation: 13
A lock variable and a semaphore appear to be identical. Explain the one significant difference between them
I've looked through all my notes and all I can find is the similarities between them, such as they are atomic actions and its a share resource. I can't seem to find that "one significant difference".
Upvotes: 0
Views: 1206
Reputation: 1957
I assume lock variable
as mutex
.
Yes. Semaphores and mutex seem similar. Some people use binary semaphore as mutex.
But they are not the same because of two major reasons.
Intention: Mutex is to be used with critical section of the code. They are used mainly to make sure that a resource is used by one thread in a program. If a thread is able to lock the mutex, that means it has exclusive access to that resource.
On the other hand, semaphores are to be used in producer-consumer case: When a producer is producing data and consumer is consuming data. If you consider a container where data is stored as a resource, producer and consumer can work simultaneously on different part of the data in the container. If there are multiple consumers, then number of consumers accessing the container (resource) to be limited by the amount of data present in the container.
In semaphore terms, if producers do sem_post after producing each piece of data and putting in container and consumers do sem_wait, before accessing data in container, you are controlling number of users accessing the container. Semaphores are not meant to provide exclusive access of a resource to a user. The intention is to limit the number of users of a resource.
Usage: Mutex should be unlocked by the thread which has locked it. In C, if a thread tries to unlock a mutex which is not locked by it, the behavior is undefined. In case of semaphore, one thread can just do semaphore_wait and another thread can semaphore_post (This is how normally it is used). ("one significant difference"?? :D )
Some developers use binary semaphores are used as mutex. It is risky because of 2. Usage I have mentioned above. Also, in my opinion it is like a work around for mutex. It is almost like replacing semaphores (not binary) with a mutex, a counter, a flag and polling mechanism in case of sem_wait. The code will work with this but it is a overkill.
There are more resources on this:
Difference between binary semaphore and mutex
https://www.geeksforgeeks.org/mutex-vs-semaphore/
Upvotes: 1
Reputation: 10445
Do you suspect your teacher is trying to trick you? If not, there is an old adage "if you have checked everything and still can't find the answer, then something you know isn't wrong".
The difference between synchronization mechanisms is often subtle; and may seem insignificant. For example, it might not seem important that a semaphore and a condition variable are stateless -- anybody can wake them up (post, release, signal, ...); whereas a mutex is strictly stateful -- only the last actor (process, thread, task, ...) that claimed it ( lock, enter, ...) can wake it up. If you need to answer the question "who owns this resource", there is no answer for a semaphore or condition variable; but there is for a mutex.
Upvotes: 1