What is the difference between mutex and semaphore?

Answers were Sorted based on User's Feedback



What is the difference between mutex and semaphore?..

Answer / sushant gupta

Mutex vs. Semaphore

The easiest way to understand the difference is to look at
it as a real life situation::

Scenario 1:

There is a room with three (could be more) chairs for
people to sit. Any number of people can attempt to enter
the room. How do you ensure
that all the people who enter the room do so only if a seat
is available.

Answer:- You assign a gatekeeper who guards the door ??

Scenario 2:

Lets us say there is another room with only one chair, so
only one person can be in at any time. How do you ensure
that.

Same answer:- assign a gatekeeper.

Mutexes and Semaphores are both gatekeepers. We now have to
make a choice between which gatekeeper to use.

The gatekeeper in the first scenario has to be an
intelligent one, as he has to do some math. He has to keep
count of how many people are
currently in, how many are going out etc. So if ten people
are waiting to get in (because the room is currently full),
the gatekeeper has to
keep all of them waiting. When two of them leave, he notes
that and allows two people to get in.

The gatekeeper in the second scenario can afford to be
dumb, he just checks if the room is full or empty and lets
one person in if it is
empty. No math, simple.

Semaphore is the intelligent gatekeeper as it keeps track
of number of threads that are allowed to access the
resopurce it protects.

Mutex is the dumb guy, he allows only one thread to access
his resource.



(reference:-
http://www.sharpprogrammer.com/multithreading/difference-
between-mutex-and-semaphore/

Is This Answer Correct ?    62 Yes 9 No

What is the difference between mutex and semaphore?..

Answer / drp3

you are partly right,but you must mention mutex is binary
semaphore.
Shemaphores are of two types , binary and counting.counting
shemaphore can range over an unrestricted domain ,where as
binary semaphore which is also known as mutex can range
between 0 and 1.

Is This Answer Correct ?    40 Yes 10 No

What is the difference between mutex and semaphore?..

Answer / rohit

Apart from counting behaviour the biggest differnce is in
scope of mutex and semaphore. Mutex have process scope that
is it is valid within a process space and can be used for
thread synchronization (hence light weight), semaphore are
can be used accross process space and hence can be used for
inter process synch.

However on RTOS this distinction is not applicable due to
flat process space hence only semaphores are supported on
typical RTOS.

Is This Answer Correct ?    31 Yes 4 No

What is the difference between mutex and semaphore?..

Answer / swetcha

The only real difference between a Semaphore and a Mutex is
that a Semaphore can have a count > 1. While a mutex will
only let one owner attempt access, a Semaphore can be
assigned a number and allow "x" number of threads access.


..........Let me know if it's wrong!

Is This Answer Correct ?    31 Yes 18 No

What is the difference between mutex and semaphore?..

Answer / rajesh aki

Mutex can be released only by the thread that had acquired
it where as in semaphore any thread can signal the
semaphore to release the critical section.

Is This Answer Correct ?    13 Yes 1 No

What is the difference between mutex and semaphore?..

Answer / mickey

Modified question is - What's the difference between A mutex
and a "binary" semaphore in "Linux"?

Ans: Compiled from above correct answers -

Following are the differences –
i) Scope – The scope of mutex is within a process address
space which has created it and is used for synchronization
of threads. Whereas semaphore can be used across process
space and hence it can be used for interprocess synchronization.

ii) Mutex is lightweight and faster than semaphore. Futex is
even faster.

iii) Mutex can be acquired by same thread successfully
multiple times with condition that it should release it same
number of times. Other thread trying to acquire will block.
Whereas in case of semaphore if same process tries to
acquire it again it blocks as it can be acquired only once.

Is This Answer Correct ?    6 Yes 1 No

What is the difference between mutex and semaphore?..

Answer / rufus v. smith

A semaphore can be claimed a preset number of times, and when this number is reached, additional claims are blocked/denied until there is a release.

A binary semaphore has a count of 1. However, this does NOT make it a mutex.

A mutex is thread-aware. Once claimed, other claims (by other threads) will be blocked/denied. However, the same thread may claim it repeatedly and not be blocked (it must be released the same number of times).

For a binary semaphore, a second claim, even from the same thread, WILL block (not a good situation if from the same thread, make sure to timeout the request)

Is This Answer Correct ?    3 Yes 1 No

What is the difference between mutex and semaphore?..

Answer / henry denial s

"Mutexes are typically used to serialise access to a
section of re-entrant code that cannot be executed
concurrently by more than one thread. A mutex object only
allows one thread into a controlled section, forcing other
threads which attempt to gain access to that section to wait
until the first thread has exited from that section."

A mutex is really a semaphore with value 1

A semaphore restricts the number of simultaneous users of a
shared resource up to a maximum number. Threads can request
access to the resource (decrementing the semaphore), and can
signal that they have finished using the resource
(incrementing the semaphore)."

Is This Answer Correct ?    2 Yes 1 No

What is the difference between mutex and semaphore?..

Answer / sreelakshmi

Mutex can be released only by the thread that had acquired
it where as in semaphore any thread can signal the
semaphore to release the critical section.

If the above concept is true why priority inversion problem
will occur ? The higher priority task can release the
semaphore from lower priority task for its execution....

Is This Answer Correct ?    1 Yes 0 No

What is the difference between mutex and semaphore?..

Answer / ravi

Sem of two types
1. Binary or mutex
2. Count

in case of Mutex it handles different type of resources
available i.e have only two states 1 or 0

in case of count type it handles same type of resources
available.

Is This Answer Correct ?    8 Yes 8 No

Post New Answer

More Operating Systems General Concepts Interview Questions

Differentiate between pre-emptive and non-pre-emptive scheduling.

0 Answers   Akamai Technologies,


Explain Memory Partitioning, Paging, Segmentation?

0 Answers   Crossdomain,


How do I go to a specific line in vi?

0 Answers  


Define the difference between preemptive and nonpreemptive scheduling.

0 Answers  


What is the Difference Between SystemCalls & Interpreter

1 Answers   Satyam,






What are your solution strategies for "Dining Philosophers Problem" ?

1 Answers  


Which are the necessary conditions to achieve a deadlock?

0 Answers  


What are necessary conditions for dead lock?

0 Answers  


What is interrupt table?

5 Answers   HP,


What is difference b/w general semaphore and binary semaphore?

3 Answers  


What is virtual memory and where it exhist ?

0 Answers   MCN Solutions,


What is a bootloader?

0 Answers  


Categories