Online Tutorials & Training Materials |
Register Login

Semaphore in OS

|| || 0

Semaphore in OS

What are Semaphores in OS?

In order to manage a concurrent process in operating systems In 1965, Dijkstra proposed a new technique known as Semaphore.

Semaphores are integer variables that are used to solve the critical section problem. They are primarily used in multiprocessing environments for process synchronization. It can be accessed through two atomic operations that are wait and signal. They can be considered abstract data types that can be used to solve race conditions in programs. They can also be used for accessing files in the shared memory.

Properties of Semaphores

The common properties of semaphores are:

  • The variable will always hold a nonnegative integer value.
  • There are two main types of semaphores, namely binary and counting semaphores.
  • They can be implemented using test operations and interrupts. They can also be executed using file descriptors.
  • The semaphores have a set of permits that can restrict the threads from accessing the resources. However, they can allow many processes to enter the critical section.
  • To avoid the race condition, the semaphores are initialized with a value that depicts the availability of a resource for usage.  

Types of Semaphores with an explanation

The two main types of semaphores are:

Binary Semaphore

Here the value is restricted to 0 and 1. When there is only one shared resource, this semaphore is used. They generally have two states, namely Acquired and Released. When the semaphore value is 1, the wait operation is executed. When the value is 0, the signal operation is executed. They have no ownership and any task can release them. This is why they are mostly used for task synchronization. It is commonly called Mutex.

Counting Semaphore

Here, more than one resource is handled. They are initialized using the count function and the resources are allocated until the count becomes zero. The task that is requesting the resources will enter the blocked state. They have a value domain that is not restricted. When more resources are added, the count value is incremented and when the number of resources is decreased, the count is lowered.

Wait and Signal Operations in Semaphores (P and V operations in semaphore) with examples.

Wait operation

This operation is used to control the entry of a task into the critical section.  If the value of Wait is positive, then the value of the wait argument S is decremented. No operation is executed if the value of the argument is negative or zero. It is also called P(S) operation. After the semaphore value is decreased and becomes negative, the command is held up until the required conditions are satisfied.

    while (S<=0);

Signal operation

This operation is used to control the exit of a task from a critical section. This increases the value of the argument by 1. It is denoted as V(S).

    while (S>=0);

Both of the operations are used to implement process synchronization. The aim is to achieve mutual exclusion.

For example, let us consider two processes P1 and P2 and a semaphore initialized with a value of 1. The value of the semaphore becomes 0 if the process P1 enters the critical section. If the process P2 intends to enter the critical section then the value of the semaphore has to be greater than 0, until that the process has to wait. This is only possible when P1 completes the critical section and calls the Signal operation on the semaphore. Mutual exclusion is obtained this way.       

Advantages of Semaphore

The different advantages of semaphores are given below:

  • 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.
  • 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.
  • They are machine independent as they run in the machine independent code of the microkernel.
  • They allow flexible management of resources.

Limitations of Semaphores

The different limitations of semaphores are given below:

  • 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.
  • To avoid deadlocks, the Wait and Signal operations have to be executed in the correct order.

Semaphore programming is complicated and there are chances of not achieving mutual exclusion.

Related Articles