Online Tutorials & Training Materials |
Register Login

Semaphore in OS

|| || 0

Semaphore in OS

What is Semaphores in OS?

In order to manage a concurrent process in operating systems In 1965, Dijkstra proposed a new technique known as Semaphore. A semaphore in operating system is like an integer variable, the only difference is that its both increment, as well as decrement operations, are guaranteed to be minutely small, you cannot be halfway through incrementing the semaphore and be interrupted by another thread trying to do the similar thing.

It is a synchronization tool to call a Semaphore, it is basically a variable. We can say that is a protected variable that facilities and restricts the access of shared resource in a multi-processing environment.  

It is used to solve critical section problem we can say to achieve the process synchronization in the multi-processing environment. Semaphores used to signal synchronization among processes.

Properties of Semaphores

  • Always starts with a non-negative integer value.
  • Works with concurrent processes.
  • Many semaphores for different critical sections.
  • A new semaphore is allowed/assigned to every critical section.
  • At once multiple processes are allowed in the critical region

Types of Semaphores

In the Counting Semaphores, the two types of operation can be performed.

  • Binary semaphore
  • Counting semaphore

1) Binary semaphore:

Binary semaphore works with 0 or 1 only. 0 means locked resources and 1 means resources are free. Binary semaphores are mostly used for implementing lock which enables/allows only one process into the critical section. Initially, the semaphore is assigned with value = 1 and then when a thread finally approaches to the critical region, it waits for the semaphore to decrement the value and "take out" the lock, and then signals the semaphore at the end of the critical region to release the lock.

Note: While the lock is in use, any process arriving at the critical region will be blocked when trying to decrement the semaphore, because it is already at 0.

2) Counting semaphore:

It works on non-negative integer values. The internal state is defined by a signed integer variable, the counter.

The counter value (N) has a precise meaning:

(a) Negative, there are exactly -N no of process queued on the semaphore.

(b) Zero, no waiting process, a wait operation would put in queue the invoking thread.

(c) Positive, no waiting threads, a wait operation would not put in queue the invoking thread.

Wait and Signal Operations in Semaphores (P and V operations in semaphore) Examples

There are two operations are defined for counting semaphores:

Wait P(S) and Signal V(S) are the only operations through you can access the Semaphore. A semaphore has two methods wait and signal, a counter integer of semaphore S whose private counter has been initialized to a non-negative integer. The Wait is represented by P(S) and Signal is represented by V(S).

  • Wait Operations: This operation decreases the semaphore counter; if in case the result is negative then the invoking thread is queued.
    while (S<=0);
  • Signal Operations:  In signal there is a counter as the process arriving operation increases the semaphore counter, if in case the counter is non-negative then a thread removed from waiting else it queue.
    while (S>=0);

Advantage of Semaphore

  1. A semaphore restricts the entry to one process at a time into the critical section. It follows the principle of mutual exclusion strictly and is much more efficient than some other methods of synchronization.
  2. With the help of Semaphore, resources are optimum utilized 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.

Limitations of Semaphores

The limits in operating systems of semaphores about the concurrent process coordination or synchronization problems. This can be just resolved with multiple solutions using semaphores and its types, locks, condition variables counter variables etc. Mutual exclusion and signalling can handle the locks or condition variables are similar to semaphores. Semaphores only cannot  help in this the problem of synchronisation

We need to identify the problem first with the limitation of semaphore then only we can get an atomic operation to wait and signal and help to concurrent process synchronisation issue. We need additional variables to check process to check the value of semaphore protected with the lock, against values are checked is not allowed. A flexible system that helps simple atomic operations.

Summary of Article

Semaphores are flexible and powerful writing concurrent programs.  Programmers use them in an effective manner, imposing locks and condition variable, to get the optimum utilisation of the resources with help of the utility and simplicity. Safety and liveness hold in all states, to provide better utilization of resources with help of the variables can say nothing bad and property of eventually something good.

Related Articles