What Is Semaphore
What Is Semaphore
A semaphore either allows or disallows access to the resource, which depends on how it is
set up.
Characteristic of Semaphore
Here, are characteristic of a semaphore:
Types of Semaphores
The two common kinds of semaphores are
Counting semaphores
Binary semaphores.
Counting Semaphores
This type of Semaphore uses a count that helps task to be acquired or released numerous
times. If the initial count = 0, the counting semaphore should be created in the unavailable
state.
However, If the count is > 0, the semaphore is created in the available state, and the
number of tokens it has equals to its count.
Binary Semaphores
The binary semaphores are quite similar to counting semaphores, but their value is
restricted to 0 and 1. In this type of semaphore, the wait operation works only if
semaphore = 1, and the signal operation succeeds when semaphore= 0. It is easy to
implement than counting semaphores.
Example of Semaphore
The below-given program is a step by step implementation, which involves usage and
declaration of semaphore.
After the semaphore value is decreased, which becomes negative, the command is held up
until the required conditions are satisfied.
Copy CodeP(S)
{
while (S<=0);
S--;
}
Signal operation
This type of Semaphore operation is used to control the exit of a task from a critical section.
It helps to increase the value of the argument by 1, which is denoted as V(S).
Copy CodeP(S)
{
while (S>=0);
S++;
}
The wait and signal operations can modify a It is modified only by the process
Modification
semaphore. or release a resource.
If no resource is free, then the process requires a If it is locked, the process has to w
Resource
resource that should execute wait operation. It should should be kept in a queue. This ne
management
wait until the count of the semaphore is greater than 0. accessed only when the mutex is
Value can be changed by any process releasing or Object lock is released only by the
Ownership
obtaining the resource. has obtained the lock on it.
Advantages of Semaphores
Here, are pros/benefits of using Semaphore: