Professional Documents
Culture Documents
Critical Section
Types of Semaphores
There are two types of Semaphores.
1. Binary Semaphore
2. Counting Semaphore
1. Binary Semaphore
It is a special form of semaphore used for implementing mutual exclusion,
hence it is often called a Mutex Locks.
A binary semaphore is initialized to 1 and only takes the
values 0 and 1 during the execution of a program.
In Binary Semaphore, the wait operation works only if the value of
semaphore = 1, and the signal operation succeeds when the semaphore= 0.
Binary Semaphores are easier to implement than counting semaphores.
If the Value of Binary Semaphore is 1, then the process has the capability
to enter the critical section area.
If the value of Binary Semaphore is 0 then the process does not have the
capability to enter the critical section area.
P1
NCS
Entry section
P (S)
{
While (S<= 0); //no operation
S - -;
}
Critical Section
Exit Section
V (S)
{
S++;
}
NCS
2. Counting Semaphore
Here the semaphore count is used to indicate the number of available
resources.
If the resources are added then the semaphore count automatically gets
incremented and if the resources are removed, the count is decremented.
Counting Semaphore has no mutual exclusion.
P1
NCS
Entry section
P (S)
{
S value= S value – 1;
If (S value < 0)
{
Put process (PCB) in suspended list Sleep ( );
}
Else
Return;
}
Critical Section
Exit Section
V (S)
{
S value= S value +1;
If (S value < = 0)
{
Select a process from suspended list
&&
wake up ( );
}
}
NCS