0

I read one of the differences between semaphore and mutex is in case of mutex the process/thread (which ever is having the lock) can only release the lock. But in the case of the semaphore any other process can release the semaphore. My doubt arises when a process that does not have the semaphore with it can release the semaphore. What is the use of having a semaphore?

Let's say I have two processes A and B. Assume process A is having a semaphore with it and executing some critical task. Now let us say process B sends a signal to release the semaphore. In this scenario, will process A release the semaphore even if it is executing some critical task?

chembrad
  • 887
  • 3
  • 19
  • 33
Bhanu
  • 1
  • Please see this link http://koti.mbnet.fi/niclasw/MutexSemaphore.html , it has good explanation on semaphore vs. mutex – ramana_k Oct 14 '15 at 18:41

5 Answers5

0

You are making half-sense. It is not about ownership. Partner-release in semaphores (and mutexes) is usable, for instance, in my favorite interview question of thread ping-pong. As a matter of fact, I have specifically tried to partner-release a mutex on 3 implementations available to me at a time (Linux/Solaris/AIX) and partner-release did work for mutexes as expected - i.e. mutex was successsfully released and threads blocking on it resumed execution. However, this is, of course, prohibited by Posix.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
0

I think you might be confused on the whole set of differences between a semaphore and a mutex. A mutex provides mutual exclusion. A semaphore counts until it reaches a level where it starts excluding. A semaphore that counted to one would give similar semantics to a mutex though.

A good example would be a television set. Only so many people can watch the same television set, so protecting it with a semaphore would make sense. Anyone can stop watching the television. The remote control for the television can only be operated by one person at a time though, so you could protect it with a mutex.

Some reading...

https://en.wikipedia.org/wiki/Mutual_exclusion

https://en.wikipedia.org/wiki/Semaphore_%28programming%29

Jason
  • 3,777
  • 14
  • 27
0

"Let's say I have two processes A and B. Assume process A is having a semaphore with it and executing some critical task. Now let us say process B sends a signal to release the semaphore. In this scenario, will process A release the semaphore even if it is executing some critical task?"

One key point to note here is the role of OS kernel. Process B can't send a signal to Process A 'to release the semaphore'. What it can do is request the kernel to give it access to the resource. Process A had requested the kernel and the kernel granted it access to the resource. Now process A, after it finishes its job, will let the kernel know that it is done with the resource and then kernel grants access to B.

"My doubt arises when a process that does not have the semaphore with it can release the semaphore. What is the use of having a semaphore?"

The key difference between a mutex and a semaphore is, a semaphore serializes access to multiple instances of a resource. Mutex does the same when there is one instance of the resource.

A count is maintained by kernel in case of semaphore and mutex is a special case where the count is 1.

Consider the processes as customers waiting in line at a bank. The use of semaphore is analogous to the case where there are multiple tellers serving the customers. Usage of mutex is analogous to the case where there is just one teller.

Say there are processes A, B and C that need concurrent access to a resource (lock, file or a data structure in memory, etc.). Further suppose there are 2 instances of the resource. So at most two processes can be granted access at a time.

Process A requests access to an instance of the resource following the required semantics. This request to the kernel involves data structures to identify the resource and maximum number of instances as 2. kernel creates the semaphore with a count of 2, grants A access to the resource and decrements the count to 1, because now only one other process can get access.

Now process B requests access to the resource by following the same semantics. Kernel grants it access and decrements the count to 0.

Now process C requests access, but kernel keeps it in waiting state, because count is 0 and no more than 2 processes can get concurrent access.

Process A is done with the resource and lets kernel know. Kernel notices this and grants access to process C that has been waiting.

In case of mutex, kernel grants access to the resource only one process at a time.

ramana_k
  • 1,933
  • 2
  • 10
  • 14
0

A normal binary semaphore is basically used for synchronization. However, the mutex is for exclusive access to a resource. A mutex is a special variant of semaphore that allows only one locker at a time and with more stringency on ownership than a normal semaphore such as the mutex should be released only by the thread that acquired it. Also, please note that in case of pthreads, fast mutex may not check for this error related to ownership, whereas the error checking mutex shall return error.

For the query related to 2 process A and B, the Process A shall intimate via kernel that it is done with its critical work so that the resource can be made available for waiting processes like B.

You could find some related information in this link too : When should we use mutex and when should we use semaphore

Community
  • 1
  • 1
Karthik Balaguru
  • 7,424
  • 7
  • 48
  • 65
-1

There is no such thing as "having" a semaphore. Semaphores don't have ownership like mutexes do. The code you describe would simply be buggy. Mutexes won't work if your code is buggy either.

Consider the most classic example of a semaphore -- allowing one train at a time on a section of track. You could implement this with a mutex if the train is a thread. The train would lock the track mutex before going on the track and unlock it after leaving the track.

But what if the train itself is multi-threaded? Which thread should own the track?

And what if the signalling devices are the threads, not the train? Here, the signalling device that detects the train entering the track has to lock the track while the signalling device that detects the train leaving the track has to unlock it.

Mutexes are suitable for cases where there is something that is owned by a particular thread for a short period of time. That thread can "own" the mutex. Semaphores are useful for cases where there is no thread to own anything or nothing for the thread to own.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Has nothing to do with the question. – SergeyA Oct 14 '15 at 19:03
  • @SergeyA Really? Can you explain why you think so? (Or just answer the question yourself.) I understand his question as not understanding how it could make sense to have a synchronization primitive that isn't owned or why it wouldn't always be a bug for a shared resource to be released by a thread other than the one that locked it. – David Schwartz Oct 14 '15 at 19:06
  • Because you are making false analogies and are incorrect in terms of applicability. There is nothing wrong with using semaphores for thread-symchronization (provided you know what you are doing). – SergeyA Oct 14 '15 at 19:08
  • @SergeyA Where did I say there's something wrong with using semaphores for thread synchronization? – David Schwartz Oct 14 '15 at 19:09
  • ":Mutexes are suitable for cases where there is something that is owned by a particular thread for a short period of time. That thread can "own" the mutex. Semaphores are useful for cases where there is no thread to own anything or nothing for the thread to own." I am having a hard time making any sense of it, but that's how I understood it after mulling for 5 minutes. – SergeyA Oct 14 '15 at 19:10
  • @SergeyA I guess I'm just baffled at how you got that. I don't give any examples of things semaphores are unsuitable for nor ever mention that semaphores are unsuitable for any purpose. The big semantic difference between mutexes and semaphores is that mutexes are owned by threads while semaphores have no concept of ownership unless you make one. – David Schwartz Oct 14 '15 at 19:11