Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...

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:-
between-mutex-and-semaphore/

 Is This Answer Correct ? 62 Yes 9 No

What is the difference between mutex and semaphore?..

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

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

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

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

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

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

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

More Operating Systems General Concepts Interview Questions

What are the different replacement policies ?

Term stickily bit is related to a)kernel b)undeletable file c)none

How do you know if you need 32 bit or 64 bit software?

Do I need program files and program files x86?

What are the different functions of Syntax phase, Scheduler?

Why would a parent process terminate a child process?

Explain booting the system and bootstrap program in operating system.

Why are wait states used?

What is the best suited algorithm to avoid internal fragmentation?

How long should a chkdsk take?

How much ram do I need laptop?

An Infosys Question : I + Opposite of, W + Initial of, Ice + twice of the letter b4T + 3/4 of X + 15th letter + 1/2 of 0. Please find the answer & mail me

Categories