Play and learn 300 000+ tabs online

Thursday, August 12, 2010

Semaphore Vs Mutex

Semaphore owns the resource ownership whereas mutex doesn't.Means when the semaphore enters the critical section by setting the sem_flag as one but because of some means(lets say dead lock) it gets blocked inside the critical section waiting for another resource,which is being used by another process/thread.So now no other process can enter the  critical section ...almost deadlock..means no other process can unset the sem_flag to zero..=> implies the process which sets the lock/flag has to unlock the flag.No other process/thread has the permission to unlock..


On the other hand, mutex doesn't own the ownership means even if it gets blocked inside the crtitical region,any other process can enter the critical section.

1. A semaphore post (or basically unlock) can be performed by  a different thread. However, in the case of mutex, it should be unlocked only by the same thread.
2. A semaphore post is always remembered. In other words, a producer thread can signal an unlock even when no consumer thread is waiting for data. In this case, when a consumer thread comes up and tries to block on a call to sem_wait (or locking), it can proceed further and gain control of the semaphore (which had been signalled before by the producer thread). This does not happen with a mutex - any unlock from another thread is lost.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.