What is Semaphore in Operating System ?

 What is Semaphore?

Semaphore is simply a non-negative variable that can be shared between threads. A semaphore acts as a signaling mechanism. Threads that are waiting on a semaphore may be signaled by other threads. 

It employs two atomic operations: 1)wait and 2) signal for the process of synchronization.

A semaphore allows or denies access to the resource depending on how it is set-up.

Characteristic of Semaphore 

These are the characteristics of a semaphore

  • It can be used for synchronization of tasks.
  • It's a low-level mechanism for synchronization.
  • Semaphore will always have a non-negative integer number.
  • Semaphore is possible to be implemented using interrupts and test operations, which should all be executed using file descriptors.

Different types of semaphores

Different types of semaphores


These are the two most common types of semaphores.

  • Counting semaphores
  • Binary semaphores

Counting Semaphores

This Semaphore has a count that allows for multiple times to be obtained or released. If the initial count is 0, the counting semaphore must be made in the unobtainable state.

However, If the count is > 0, the semaphore is created in the available state, and the number of tokens it has equals to its count.

Binary Semaphores

Binary semaphores work in a similar way to counting semaphores but are limited to 0 or 1. This type of semaphore has a wait operation that works only when semaphore is 1, and a signal operation that succeeds when semaphore= 0. It's easier than counting semaphores.

Advantages of Semaphores

These are the pros and benefits of using Semaphore.

  • You can access the crucial section from more than one thread.
  • Semaphores can be used in conjunction with machines.
  • The machine-independent code in the microkernel implements semaphores.
  • They prohibit multiple entry into the critical section.
  • There is always a lot of waiting in semaphore so there is no waste of time or resources.
  • These are machine-independent and should be run in the machine independent code of the microkernel.
  • They enable flexible resource management.

Disadvantage of Semaphores 

These are the cons and drawbacks of semaphore

  • Priority inversion is one of the greatest limitations of a semaphore.
  • The operating system must keep track of all calls that are waiting to be answered and to signal semaphore.
  • They are not enforced but only by convention.
  • To avoid semaphore deadlocks, the Wait and Signal operations must be performed in the correct order.
  • Because semaphore programming can be complex, there is the possibility of mutual exclusion.
  • This is not an ideal method of large-scale use, as they can cause loss of modularity.
  • Semaphore is more susceptible to programmer error.
  • It could cause deadlock, or violate mutual exclusion due programmer error.

Comments

Popular posts from this blog

What is Modular Programming? Where is it used?

What is Hibernate Framework?

Ellipse command in AutoCAD