What is Semaphore in OS

 

What is Semaphore in OS
What is Semaphore in OS


Semaphore in OS

A Semaphore is a regular integer value, and it cannot have a negative value. Semaphores in OS used to resolve the problems of critical section and synchronize the processes by using operations. Semaphore has two operations that are wait and signal.

 

Wait

The wait operation of semaphore is used to decrease the positive value of its argument S. If the value of argument S is zero or negative, then no actin is taken.

wait(S)

{

   while (S<=0);

 

   S--;

}

 

Signal

The signal operation of semaphore is used to increase the value of its parameter S.

signal(S)

{

   S++;

}

Semaphores are utilized to create critical sections, segments of code that require exclusive execution by one process at a time. They facilitate the coordination of process access to shared resources like memory or I/O devices.


Types of Semaphores

Types of semaphores are :

  • Counting semaphores  
  • Binary semaphores

These are described in more detail as follows:


Counting semaphores  

Counting semaphore has value in whole numbers and unlimited value domain. To arrange the resource access these semaphores are used.

 When resources are added, the semaphore count is automatically increased; when resources are removed, the count is automatically decreased.

 

Binary semaphores

Binary semaphores are semaphores that value is 0 or 1. If it value is equal to 1 then wait operation is perform and if value is 0 then signal operation perform.

 

Advantages of Semaphores

  1. Offers a straightforward and efficient method for synchronizing processes
  2. Facilitates coordination among multiple processes
  3. Offers a versatile and resilient approach to handling shared resources
  4. Enables the implementation of critical sections within a program
  5. Helps prevent race conditions and ensures smooth execution.
  6. A single process can enter in critical section.   
  7. Semaphores synchronization techniques are more effective than some others because they strictly adhere to the mutual exclusion principle.


Disadvantages of Semaphores:

  1. May cause performance decline due to the overhead from wait and signal operations.
  2. Incorrect use can result in deadlock.
  3. Improper usage may lead to program performance issues.
  4. Debugging and maintenance can be challenging.
  5. Incorrect application may cause race conditions and synchronization problems.
  6. Vulnerable to specific attacks, like denial of service attacks.