Here, we are going to learn about semaphores in operating systems. Before this, I would request to go through process synchronization first.

Dijkstra proposed the semaphore in 1965. It is a highly important technique for managing concurrent activities using a basic integer value called a semaphore. The term “semaphore” refers to an integer variable that several threads are sharing. In a multiprocessing context, this variable is useful to solve the critical section problem and establish process synchronization.

Types of Semaphores in Operating Systems:

1. Binary Semaphore- Mutex lock is another name for this. It can only have two possible values: 0 and 1. Its value is set to one at the start. It’s used to implement several processes to solve critical section problems.

2. Counting Semaphore- Its value basically can span a wide range of possibilities. It’s used to restrict access to a resource with many copies.

Limitations of Semaphores:

  • Priority inversion is one of semaphore’s most serious flaws.
  • Assume a process is attempting to wake up another process that is not in a condition of sleep. As a result, a deadlock can last indefinitely.
  • Operating System must track all calls to wait and signal the semaphore .

Producer Consumer Problem Using Semaphore:

Problem statement – We have a buffer with a predetermined size. A producer can create a product and keep it in the buffer. A consumer has the power to choose and consume items. When a producer places an item in the buffer, we must verify that the consumer does not consume any of the objects. In this situation, the buffer portion is critical.


We’ll need two counting semaphores to tackle this problem, for example Full and Empty. “Full” keeps track of how many items are in the buffer at any given time, whereas “Empty” keeps track of how many slots are empty.

Initially we have mutex =1, Full = 0, Empty = n

  // producer will produce an item 
  // will place it in buffer signal(mutex);
  // will remove item from buffer 
  // consumers will consume item