views:

119

answers:

1

What is the real difference Conceptually and Implementation point of view between

Semophore, Mutex, Monitor?

We say Semophores are resource allocation counter... OK I understand this..

For Mutex we say mutex are Binary Semophore... Gosh what's that???

Monitor guards the entry point for an objects... i.e. only one thread can acquire it and can run one of it's guarded entry points??? Then what are mutex for??

+1  A: 

Semaphores are a locking mechanism, iirc they can conceptually be configured to allow multiple aceess to an object; e.g. Access three at a time, four at a time, etc.

Mutex are a special case of a semaphore for ensuring mutual exclusion, I.e. only one can access the protected resource at any given time.

It is important to note that neither the semaphore nor mutex ensure strict ordering when waiting for access to the shared resource. When the resource becomes accessible some waiting thread will gain access but no garuantees are made about which thread that will be. Statistically, eventually all threads will (must) gain access.

A monitor enforces a precedence on the waiting threads/processes by queueing them in a particular order, not necessarily how they arrive. An operating system is an example of a monitor - ensuring a single process has the CPU at any given time.

ceretullis
Well nice explanation just one more thing. Semaphore do hold a datastructure probably a Queue with in itself to make thread waits when resources can't be allocated. Isn't this a form of ordering them?And if you can elaborate more on the working of Mutex and difference b/w monitor and mutex.
S M Kamran
No semaphores hold only the conditional variable. There is no queue with semaphores. The queue, if you want to think of it, is owned by the OS and is the normally called the "wait" queue where all blocking processes live. When the semaphore condition is ready for the next thread/process, whichever process "wakes up" first will likely get the lock. Any waiting thread has the ability to get the lock. http://en.wikipedia.org/wiki/Semaphore_(programming) has a simple implementation of semaphore.
ceretullis