Introduction
The term semaphore has constantly disrupted students. This is one of the most important concepts of an operating system and plays a vital role in achieving process synchronization in a multiprogramming environment.
This blog will help you in getting a better understanding of the topic. Before moving on, let’s first understand some other important concepts that might come in handy while understanding the semaphores better.
Also see: Multiprogramming vs Multitasking, Open Source Operating System
Process Synchronization
A process is defined as a program under execution.
The process is classified into two types based on the synchronization:
- Independent Process- Execution of one process does not depend on any other process.
- Cooperative Process- Execution of one process affects the other processes. In this case synchronization problem arises because resources are shared between processes.
In a multiprogramming environment, when multiple processes are present, all the processes need to be synchronized in an appropriate order to avoid deadlock or maintain the process's credibility so that incorrect output is not produced.
Race Condition
When two or more processes try to read, write over the same data or work on the same shared variable, there is a possibility that it may produce an incorrect output as it depends on the order in which access takes place.
Such a type of race condition can occur in the critical section. To avoid such a situation, we need to treat the critical section to use atomic instruction, which can be done using process synchronization and introducing locks or atomic variables.
Critical Section
It is a region in a program containing shared variables and can lead to race conditions. To avoid that, we must ensure that only one process is executed instantly by introducing a proper synchronized schedule and locks to maintain the consistency of the data.
The solution to a critical section problem
Any solution to a Critical Section problem must satisfy three conditions:
- Mutual Exclusion: If a process is already present in the critical section, another process cannot enter into the critical section.
- Progress: If no process is executing in CS, and other processes are present outside CS, then the processes not executing in their remainder section can participate in deciding which process will move to CS in such a way that it does not create a situation of indefinite postponement.
- Bounded waiting: Bound must exist on a given process that decides the number of times it can enter the critical section.
Semaphore is one of the solutions to the critical section problem.