Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

What is the difference between mutex and semaphore?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why do we use semaphore?

903


What do you mean by thread starvation?

1085


What is short term scheduler in operating system (os)?

1130


What are the different types of kernel?

1061


Can you please explain the difference between trap and trapdoor?

1058


What is turnaround time?

1004


What is virtual memory and where it exhist ?

1182


Explain translation lookaside buffer?

1219


Does dual boot affect performance?

980


What is a sector?

999


Explain the typical elements of a process image?

1048


Mention the steps involved in the execution of a program.

943


What is the max ram for a 64 bit?

930


How many types of fragmentation occur in operating system?

954


What is the purpose of ram in a computer?

1055