Sei sulla pagina 1di 2

Difference between:

1. Monitor VS Semaphore
Monitor Semaphore
A monitor is a programming language
construct that is used to control access to
shared data.
Semaphore is a data structure that is used
to provide mutual exclusion to critical
sections. Semaphores mainly support two
operations called wait (historically known
as P) and signal (historically known as
V).
In monitor the code that is used to
achieve mutual exclusion is in a single
place and is more structured.
While code for semaphores are
distributed as wait and signal function
calls
There are two types of monitors named
Hoare monitors and Mesa monitors.
There are two types of semaphores called
mutex semaphores and counting
semaphores.
Wait() always blocks the caller. Wait() does not always block the caller
(i.e., when the semaphore counter is
greater than zero).
Signal() either releases a blocked thread,
if there is one, or the signal is lost as if it
never happens.
Signal() either releases a blocked thread,
if there is one, or increases the semaphore
counter.
If Signal() releases a blocked thread, the
caller yields the monitor (Hoare type) or
continues (Mesa Type). Only one of the
caller or the released thread can continue,
but not both.
If Signal() releases a blocked thread, the
caller and the released thread both
continue.


2. Monitor VS Mutex:
Monitor Mutex
Provides mutual exclusion to
an object i.e. at any point in time, at most
one process may access any of the
object's members/ execute any of its
methods.
Mutex is used to
provide mutual exclusion i.e. ensures at
most one process can do something (like
execute a section of code, or access a
variable) at a time.
Monitor's scope depends on the scope of
the object you lock on.
A mutex's scope is system-wide
monitor is used to synchronies between
threads running in an application
Mutex is used to synchronise between
different applications.



3. Mutex and Semaphore
Mutex Semaphore
Mutex is used to
provide mutual exclusion i.e. ensures at
most one process can do something (like
execute a section of code, or access a
variable) at a time.

Semaphore is a data structure that is used
to provide mutual exclusion to critical
sections. Semaphores mainly support two
operations called wait (historically known
as P) and signal (historically known as
V).
Mutex can be released only by thread that
had acquired it.
Semaphore can be signal from any other
thread (or process).
A mutex provides mutual exclusion A semaphore is a generalized mutex.
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 semaphore restricts the number of
simultaneous users of a shared resource
up to a maximum number.

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.
Threads can request access to the
resource (decrementing the semaphore),
and can signal that they have finished
using the resource (incrementing the
semaphore).




Reference:
http://www.differencebetween.com/difference-between-semaphore-and-vs-
monitor/#ixzz38kwRvhii
http://koti.mbnet.fi/niclasw/MutexSemaphore.html
http://stackoverflow.com/questions/62814/difference-between-binary-semaphore-
and-mutex
http://www.barrgroup.com/Embedded-Systems/How-To/RTOS-Mutex-
Semaphore
http://social.msdn.microsoft.com/Forums/vstudio/en-US/c60e5a8c-42a9-4769-
83cc-dc1e50d67994/difference-between-mutex-and-monitor

Potrebbero piacerti anche