In: Computer Science
Under what conditions would the use of a counting semaphore initialized to 5 be used instead of a mutex lock?
Semaphore:
Semaphore is nothing but is a special integer variable that can be accessed by only two system calls which are atomic in nature, known as p() and v() or wait() and signal(). It is a signaling mechanism.
The binary semaphore can have two values(0, 1) only
The counting semaphore has an unrestricted domain.
Mutex Lock:
Only one process or task can acquire the mutex lock at a time because ownership is associated with a mutex lock and only the owner can release the mutex lock. It is a locking mechanism.
The binary semaphore can have two values(0, 1) only, so it can be used as a mutex lock.
A binary semaphore is similar to the mutex.
When a process request for the resource then it performs wait() operation and decrement semaphore value from 1 to 0 and again increment from 0 to 1 when it performs the signal() operation. If the semaphore value is zero and a process requesting for the resource then it will block itself till the current process release the resource.
So, this procedure is similar to the mutex lock.
But the counting semaphore can not be used as a mutex lock because it has an unrestricted value domain.
The use of a counting semaphore initialized to 5 when there are 5 instances of resources available in the system. Every time when an instance will be assigned to a process the semaphore value will be decreased by one. When the signal() operation will be performed then the semaphore value will be increased by one.