OS Menu9>
- OS Home
- Introduction
- CPU Scheduling
- What is Process?
- Process Lifecyle
- Process Control Block
- Process Scheduling
- Context Switching
- CPU Scheduling
- FCFS Scheduling
- SJF (non-preemptive)
- SJF (Preemptive - SRTF)
- Round Robin
- Priority Scheduling
- Convoy Effect
- Scheduler Vs Dispatcher
- Preemptive Vs non
- Preemptive scheduling
- Non preemptive scheduling
- Process Synchronization
- Deadlock
- Popular Algorithms
- Memory Management
- Memory Management Introduction
- Partition Allocation Method
- First Fit
- First Fit (Intro)
- First Fit in C
- First Fit in C++
- First Fit in Python
- First Fit in Java
- Best Fit
- Best Fit (Intro)
- Best Fit in C
- Best Fit in C++
- Best Fit in Java
- Worst Fit
- Worst Fit (Intro)
- Worst Fit in C++
- Worst Fit in C
- Worst Fit in Java
- Worst Fit in Python
- Next Fit
- First fit best fit worst fit (Example)
- Memory Management 2
- Memory Management 3
- Page Replacement Algorithms
- LRU (Intro)
- LRU in C++
- LRU in Java
- LRU in Python
- FIFO
- Optimal Page Replacement algorithm
- Optimal Page Replacement (Intro)
- Optimal Page Replacement Algo in C
- Optimal Page Replacement Algo in C++
- Optimal Page Replacement Algo in Java
- Optimal Page Replacement Algo in Python
- Thrashing
- Belady’s Anomaly
- Static vs Dynamic Loading
- Static vs Dynamic Linking
- Swapping
- Translational Look Aside Buffer
- Process Address Space
- Difference between Segmentation and Paging
- File System
- Off-campus Drive Updates
- Get Hiring Updates
- Contact us
PREPINSTA PRIME
Semaphore in Operating System (OS)
Semaphore in Operating System
Semaphore in Operating System (OS) is an entity devised by Edsger W. Dijkstra to solve the Process Synchronization problem in OS. Its most popular use is to solve the Critical Section problem, where multiple processes need access to shared resources without interfering with each other. Semaphores act as signaling mechanisms that help manage concurrent processes by using simple integer variables.
These variables can be incremented or decremented using operations like wait and signal.

Semaphore in Operating System
A Semaphore is a synchronization tool used in operating systems to control access to a shared resource by multiple processes. It is a type of signaling mechanism that helps prevent race conditions, ensuring that only one process can access a critical section at a time.
Semaphore uses signalling mechanism to allow access to shared resources namely by two –
- Wait
- Signal

Prepare for Interview with us:
What is Semaphore in Operating System
A Semaphore is a synchronization tool used in operating systems to manage access to shared resources by multiple processes in a concurrent system. It serves as a signaling mechanism to control the execution order of processes and ensure mutual exclusion, preventing race conditions and deadlocks. The concept of semaphores was introduced by Edsger Dijkstra.
Semaphore Types
There are two types of Semaphores –
- Binary Semaphore – Only True/False or 0/1 values
- Counting Semaphore – Non-negative value
Semaphore Implementation
Semaphore can have two different operations which are wait and signal. In some books wait signals are also denoted by P(s) and signal by V(s). Where s is a common semaphore.
Wait p(s) or wait(s)
- Wait decrements the value of semaphore by 1
Signal v(s) or signal(s)
- Signal increments the value of semaphore by 1
Semaphore
- Semaphore can only have non-negative values
- Before the start of the program, it is always initialised to
- n in Counting semaphore (Where n is the number of processes allowed to enter critical section simultaneously)
- 1 in the case of a binary semaphore


Signal Operations
- Signal operation is simple
- It increases the value of semaphore by 1 as shown below
signal(S) { S++; }
Wait Operations
- Wait operation decrements the value of semaphore S if S is a positive number
- Else if S is 0 or negative then code gets stuck at while loop as it keeps implementing infinitively
- The semi-colon after while forces while loop definitively if S is 0 or negative
- Thus the code doesn’t move ahead in hopes that the value of S will increase because of some other signal operation elsewhere
Code Logic for Incrementing – Decrementing value of Semaphore –
wait(S) { while (S<=0); S--; }
Actual Working for both functions together to achieve access of critical section –
// some code wait(s); // critical section code signal(s); // remainder code
The eventual goal is to protect the critical section code using wait and signal operations.
You can visualize the whole operation on how the Semaphore system works with the example below

Monitors
Monitors are a synchronisation construct that were created to overcome the problems caused by semaphores such as timing errors.
Semaphores can also be used but may cause timing errors. Mutex can never be used for process synchronisation
Mutex can not be used for the purpose of Process Synchronization
Ending statement
semaphores play a crucial role in operating systems by ensuring proper synchronization between processes, particularly when managing access to shared resources. Introduced by Edsger W. Dijkstra, semaphores prevent issues like race conditions and deadlocks. They come in two types: binary semaphores and counting semaphores, each serving different purposes depending on the system’s needs. By using simple operations like wait and signal, semaphores control process execution and enable mutual exclusion in critical sections. This synchronization mechanism is essential for maintaining system stability and efficiency in concurrent computing environments.
FAQs
The concept of semaphores was introduced by Edsger W. Dijkstra to address synchronization issues in concurrent programming.
Yes, improper use of semaphores like failing to release them can lead to deadlocks where processes wait indefinitely for each other.
A mutex allows only one thread access to a resource at a time, while a semaphore can allow multiple accesses, depending on its value.
No, semaphores are also widely used in multithreaded applications and embedded systems to handle resource sharing and synchronization.
Login/Signup to comment