Mutex is locking mechanism used to synchronize access to a resource. Only one task ( thread or process ) can acquire the mutex. It means there will be ownership associated with mutex, and only the owner can release the lock. Mutex is a key to a changing room. One person can have the key occupy the changing room at the time. When finished, the person gives (release) the key to the next person in the queue.
Semaphore is wait signal mechanism. A semaphore is a way to limit the number of tasks that can simultaneously operate on a shared resource. Semaphores is the number of identical room keys.we have four room with identical locks and keys. The semaphore count , the count of keys is set to 4 at beginning (all four rooms are free), then the count value is decremented as people are coming in. If all rooms are full, there are no free keys left, the semaphore count is 0. Now, when one person leaves the room, semaphore is increased to 1 (one free key), and given to the next person in the queue.