# Semaphores and its types

Semaphores are compound data types with two fields one is a Non-negative integer S.V. and the second is a 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 semaphores.

1. Running: It states the Process in execution.
2. Ready: It states that the process wants to run.
3. Idle: The process runs when no processes are running
4. Blocked: The processes are not ready and 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 the set component, and the state of the process p becomes 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 is called General Semaphore. They are kind of weak semaphores.

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 for 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 semaphores, S.L. remains unchanged as in weak semaphores whereas S.V. is replaced by the queue. Because removal of arbitrary process in a weak semaphore it may lead to starvation whereas in this case, it remains free from starvation.

Initialization

` S <- (0,empty)`

Wait for 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 for 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 waste CPU time that could be used for computation.

Wait for Operation Algorithm

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

Signal Operation Algorithm

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

1. They do not allow more than one process to enter the critical section. In this way, mutual exclusion is achieved and thus they are extremely efficient than other techniques for synchronization.
2. Due to busy waiting in semaphore, there is no wastage of process time and resources. This is because the processes are only allowed to enter the critical section after satisfying a certain condition.
3. They are machine-independent as they run in the machine-independent code of the microkernel.
4. They allow flexible management of resources.

1. There may be a situation of priority inversion where the processes having low priority get access to the critical section than the processes having higher priority.
2. To avoid deadlocks, the wait() and signal() operations have to be executed in the correct order.
3. Semaphore programming is complicated and there are chances of not achieving mutual exclusion.

### Conclusion

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

### Q,1:What is the purpose of using semaphores?

Semaphores are used to prevent race conditions and coordinate the execution of concurrent threads or processes. They ensure that only one thread/process can access a shared resource at a time, preventing conflicts and maintaining consistency.