Related Articles

# Semaphores and its types

• Last Updated : 31 May, 2021

Overview :
Semaphores are compound data types with two fields one is a Non-negative integer S.V and the second is Set of processes in a queue S.L. It is used to solve critical section problems, and by using two atomic operations, it will be solved. In this, wait and signal that is used for process synchronization.

States of the process :
Let’s go through the stages of the process that comes in its lifecycle. This will help in understanding semaphore.

1. Running –
It states that the Process in execution.
It states that the process wants to run.
3. Idle –
The process runs when no processes are running
4. Blocked –
The processes not ready not a candidate for a running process. It can be awakened by some external actions.
5. Inactive –
The initial state of the process. The process is activated at some point and becomes ready.
6. Complete –
When a process executes its final statement.

Initialization of semaphore :
Semaphore S must be initialized with a value of S.V. > 0 and with empty S.L.

`Semaphore S <- (k,φ)`

Atomic Operations in semaphore :
Here we will discuss the two atomic operations wait and signal as follows.

Operation-1 :
Wait(S) :
According to the value of S.V. if it is non-zero, decrement its value and process p can continue its execution and if it is zero, process p is added to set component and the state of the process p become blocked in this case process p is said to have been blocked on the semaphore.

Algorithm –

```if(S.V > 0)
{
S.V. = S.V -1
}
else{
S.L. = S.L. U p
p.state = blocked
}```

Operation-2 :
Signal(S) :
According to the value of S.L., if it is empty increment the value of the integer and if it is non-empty unblock q an arbitrary of the set of processes blocked o S.L. and change the status of p to ready.

Algorithm –

```   if(S.L. ==  φ){

S.V. = S.V.+1
}
else{
Let q be some process in S.L. S
S.L. = S.L. - {q}
}```

Types of Semaphores :
Here we will discuss the types of Semaphores as follows.

Type-1 :
General Semaphore :
A semaphore whose integer component can take arbitrary non-negative values of S.L. these are called General Semaphore. They are kind of weak semaphore.

Type-2 :
Binary Semaphore :
A semaphore whose integer component S.L. takes only the values 0 and 1 is called a binary semaphore. This is also known as “mutex” which stands for mutual exclusion.

Initialization –

`S <- (0, φ) or S <- (1, φ)`

Wait Operation –
It remains Unchanged as above.

Signal Operation –
It slightly changes as follows

Algorithm :

```Signal(S)
:

if (S.V.== 1)
{

// Undefined

// Return
}

else if (S.L == empty) { S.V.= 1 }

else
{

Let q be some process in
S.L.

S.L.
= S.L.
- { q }

q.state
}```

Type-3 :
Strong Semaphore :
In Strong semaphore, S.L. remains unchanged as like weak semaphores whereas S.V. is replaced by the queue. As because of removal of arbitrary process in weak semaphore it may lead to starvation whereas in this case, it remains free from starvation.

Initialization –

` S <- (0,empty)`

Wait Operation –
Algorithm :

```Wait(S) :
if (S.V > 0)
{

S.V.= S.V - 1
}

else
{

S.L.= S.L.U p

p.state
= blocked
}```

Signal Operation –
Algorithm :

```Signal(S)
:

if (S.L.== empty)
{

S.V.= S.V.+ 1
}

else
{

S.L.
= S.L.
- { q }

q.state
}```

Type-4 :
Busy- Wait Semaphore :
It does not have a component S.L. and Semaphore S is identified only by S.V.Busy-Wait Semaphore are appropriate in a multi-processor system where the waiting process has its own processor and is not wasting CPU time that could be used for computation.

Wait Operation –
Algorithm :

```Wait(S):
await S>0
S = S - 1```

Signal Operation –
Algorithm :

```Signal(S):
S = S + 1```

Conclusion :
In this section, we have discussed semaphores and their types, and also we have discussed it’s atomic operations. These are the basics of the Semaphore which are used for solving the problem of a critical section.

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.

My Personal Notes arrow_drop_up