• Operating System Video Tutorials

Operating System - Semaphores



Semaphores are integer variables used to address the critical section problem through two automatic operations: wait and signal, which are crucial for process synchronization. They include a set of procedures and non-negative integers within a waiting list.

By controlling access to shared resources, semaphores prevent critical section issues in systems like multitasking processes. Counting semaphores allow for a random resource count.

A semaphore is a tool used in operating systems to manage different processes that share resources, such as memory or data, without causing conflicts. It provides a unique mechanism for synchronization.

The two atomic operations for semaphores: wait(S), which decrements S and blocks if S is non-positive, and signals(S), which increments S, allowing process synchronization.

The definitions of wait and signal are as follows

Wait Operation

The wait operation decrements its argument, S, if it is positive. If S is negative or zero, no operation is performed. This operation checks the semaphore's value. If the value is greater than 0, the process continues and S is decremented by 1. If the value is 0, the process is blocked(waits) until S becomes positive.

wait(S) {
   while (S <= 0);
   S--;
}

Signal Operation

The signal operation increments its argument, S. After a process finishes using the shared resource, it performs the signal operation, which increases the semaphore's value by 1, potentially unblocking other waiting processes and allowing them to access the resource.

signal(S) { 
   S++; 
}

Types of Semaphores

There are two main types of semaphores: counting semaphores and binary semaphores. Details about these are as follows−

  • Counting Semaphores:These integer-value semaphores have an unrestricted value domain. They are used to coordinate resource access, with the semaphore count representing the number of available resources. If resources are added, the semaphore count is incremented automatically; if resources are removed, the count is decremented.

  • Binary Semaphores: Binary semaphores are similar to counting semaphores but their value is restricted to 0 and 1. The wait operation only works when the semaphore is 0. Implementing binary semaphores is sometimes easier than counting semaphores.

Advantages of Semaphores

Some of the advantages of semaphores are as follows

  • Semaphores strictly follow the mutual exclusion principle, allowing only one process into the critical section at a time. They are much more efficient than some other synchronization methods.
  • Semaphores eliminate resource wastage caused by busy waiting, as processor time is not needlessly spent checking if a condition is met for process to access the critical section.
  • Semaphores are implemented in the machine independent code of the microkernel, making them machine-independent.
  • Due to the busy queue within the semaphore, there is no consumption of processing time and resources. This is because operations are allowed to enter the critical section only after satisfying a certain condition.
  • Users allow for the flexible management of resources.
  • They do not allow more than one operation to enter the critical section. Mutex is implemented, and these are significantly more efficient than other synchronization methods.

Disadvantages of Semaphores

Some of the disadvantages of semaphores are as follows −

  • Semaphores are complex, so the wait and signal operations must be implemented in the correct order to prevent deadlocks.
  • Semaphores are impractical for large scale use as they lead to loss of molecularity. This occurs because the wait and signal operations specify the creation of a structured system layout.
  • Semaphores may cause priority inversion, where low-priority processes access the critical section before high-priority processes.

Levels of the Operation

There are various stages in life that can help in understanding semaphores. These stages are as follows −

  • Running: This indicates the operation is currently being executed within an implementation.

  • Ready: This indicates the operation that is ready to run.

  • Idle: The operation is tracking when no procedures are being organized.

  • Blocked: This indicates that the procedures may not be ready for splitting operation and might be triggered by some peripheral events.

  • Complete: This indicates that a procedure has executed its final statement.

  • Inactive: This indicates that the operation is initiated at some point, representing the early stage of the process.

Initialization

Semaphore S should be initialized with a non-negative integer value greater than 0 and an empty set of procedures.

S ← (k, φ)

Semaphores in Terms of Process Synchronization

Semaphores are synchronization mechanisms used to coordinate the activities of multiple procedures within a mainframe. They are employed to implement Mutex, avoid race conditions, and achieve synchronization among processes.

The wait process decreases the value of the semaphore, while the signal process increases it. Semaphores are used to execute critical sections, which are parts of the program that must be executed by only one operation at a time. OPerations can manage shared resources, such as shared memory or input/output devices.

Types of Semaphores

There are different types of semaphores, including −

  • General semaphore: A semaphore whose number of elements can take on random non-negative values within the set of procedures is known as a general semaphore.

  • Binary semaphore: A semaphore whose element set of procedures can only take the values zero and one is known as a binary semaphore, also referred to as a mutual exclusion.

  • Strong semaphore: The set of procedures remains unaffected like weak semaphores, while the non-negative integer changes through the waiting list. The removal of uninformed methods in weak semaphores can lead to starvation, whereas in this case, it remains free from starvation.

    S<-(0, empty)

  • Busy-wait semaphore: This type of semaphore does not use a non-negative integer element and is recognized through a set of actions. In a multi-mainframe system, the waiting process gets its mainframe and does not waste central processing time that could be used for computation.

Advertisements