
- OS - Home
- OS - Needs
- OS - Overview
- OS - History
- OS - Components
- OS - Structure
- OS - Architecture
- OS - Services
- OS - Properties
- OS - TAT & WAT
- OS Processes
- OS - Processes
- OS - Process Scheduling
- OS - Scheduling Algorithms
- FCFS Scheduling Algorithm
- SJF Scheduling Algorithm
- Round Robin Scheduling Algorithms
- HRRN Scheduling Algorithms
- Priority Scheduling Algorithms
- Multilevel Queue Scheduling
- Context Switching
- Operations on Processes
- Lottery Process Scheduling
- Predicting Burst Time SJF Scheduling
- Race Condition Vulnerability
- Critical Section Synchronization
- Mutual Exclusion Synchronization
- Process Control Block
- Inter Process Communication
- Preemptive and Non-Preemptive Scheduling
- Operating System - Deadlock
- Introduction to Deadlock in Operating System
- Conditions for Deadlock in Operating System
- OS Synchronization
- Operating System - Process Synchronization
- Operating System - Critical Section
- Operating System - Semaphores
- Operating System - Counting Semaphores
- Operating System - Mutex
- Operating System - Lock Variable in Process Synchronization
- Operating System - Turn Variable in Process Synchronization
- Operating System - Bounded Buffer Problem
- Operating System - Reader Writer Locks in Process Synchronization
- Operating System - Test Set Lock in Process Synchronization
- Operating System - Peterson Solution in Process Synchronization
- Operating System - Monitors in Process Synchronization
- Operating System - Sleep and Wake in Process Synchronization
- OS Memory Management
- OS - Memory Management
- OS - Virtual Memory
- OS Storage Management
- File Systems in Operating System
- Linked Index Allocation in Operating System
- Indexed Allocation in Operating System
- Structures of Directory in Operating System
- File Attributes in Operating System
- Operating System - Page Replacement
- Operating Systems - Thrashing
- Belady’s Anomaly in Page Replacement Algorithms
- Optimal Page Replacement Algorithm
- Operating System - Types
- Types of Operating System
- Batch Processing Operating System
- Multiprocessing Operating System
- Hybrid Operating System
- Monolithic Operating System
- Zephyr Operating System
- Nix Operating System
- Blackberry Operating System
- Garuda Operating System
- Tails Operating System
- Clustered Operating System
- Haiku Operating System
- AIX Operating System
- Solus Operating system
- Tizen Operating System
- Bharat Operating System
- Fire Operating System
- Bliss Operating System
- VxWorks Operating System
- Embedded Operating System
- Single User Operating System
- OS Miscellaneous
- OS - Multi-threading
- OS - I/O Hardware
- OS - I/O Software
- OS - Security
- OS - Linux
- OS Useful Resources
- OS - Quick Guide
- OS - Useful Resources
- OS - Discussion
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.