1.semaphore and It's Types

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

1.

Semaphore and its types


Semaphores
In 1965, Dijkstra proposed a new and very significant technique for managing
concurrent processes by using the value of a simple integer variable to synchronize the
progress of interacting processes.
Semaphores are integer variables that are used to solve the critical section problem by
using two atomic operations, wait and signal that are used for process synchronization.
This integer variable is called a semaphore
In very simple words, the semaphore is a variable that can hold only a non-negative
Integer value, shared between all the threads, with operations wait and signal
The definitions of wait and signal are as follows −

 Wait
The wait operation decrements the value of its argument S, if it is positive. If S is
negative or zero, then no operation is performed. wait() operation was originally
termed as P; so it is also known as P(S) operation
This Operation mainly helps you to control the entry of a task into the critical
section
wait(S)
{
while (S<=0);

S--;
}

 Signal
The signal operation increments the value of its argument S. This Operation is
mainly used to control the exit of a task from the critical section.
signal() operation was originally termed as V; so it is also known as V(S)
operation
signal(S)
{
S++;
}
Properties of Semaphores

1. It's simple and always have a non-negative integer value.


2. Works with many processes.
3. Can have many different critical sections with different semaphores.
4. Each critical section has unique access semaphores.
5. Can permit multiple processes into the critical section at once, if desirable

Types of Semaphores
There are two main types of semaphores i.e. counting semaphores and binary
semaphores. Details about these are given as follows −

 Counting Semaphores
These are integer value semaphores and have an unrestricted value domain.
These semaphores are used to coordinate the resource access, where the
semaphore count is the number of available resources. If the resources are
added, semaphore count automatically incremented and if the resources are
removed, the count is decremented.

 Binary Semaphores
The binary semaphores are like counting semaphores but their value is restricted
to 0 and 1. The wait operation only works when the semaphore is 1 and the
signal operation succeeds when semaphore is 0. It is sometimes easier to
implement binary semaphores than counting semaphores.

Advantages of Semaphores
Some of the advantages of semaphores are as follows −

 Semaphores allow only one process into the critical section. They follow the
mutual exclusion principle strictly and are much more efficient than some other
methods of synchronization.
 There is no resource wastage because of busy waiting in semaphores as
processor time is not wasted unnecessarily to check if a condition is fulfilled to
allow a process to access the critical section.
 Semaphores are implemented in the machine independent code of the
microkernel. So they are machine independent.
Disadvantages of Semaphores
Some of the disadvantages of semaphores are as follows −
 Semaphores are complicated so the wait and signal operations must be
implemented in the correct order to prevent deadlocks.
 Semaphores are impractical for last scale use as their use leads to loss of
modularity. This happens because the wait and signal operations prevent the
creation of a structured layout for the system.
 Semaphores may lead to a priority inversion where low priority processes may
access the critical section first and high priority processes later.

You might also like