Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Monitors vs Semaphores
Monitors and semaphores are synchronization mechanisms used to control process access to shared resources through mutual exclusion. While both achieve process synchronization, they differ significantly in their implementation, usage complexity, and error handling capabilities.
Monitors
Monitors are high-level synchronization constructs designed to overcome timing errors and programming complexity associated with semaphores. They are abstract data types that encapsulate shared data variables and procedures within a single unit.
In a monitor, shared data variables cannot be accessed directly by processes. Instead, processes must use the monitor's procedures to interact with the shared data. Only one process can be active inside a monitor at any given time, ensuring mutual exclusion automatically.
monitor monitorName
{
data variables;
Procedure P1(....)
{
// Process operations on shared data
}
Procedure P2(....)
{
// Process operations on shared data
}
Procedure Pn(....)
{
// Process operations on shared data
}
Initialization Code(....)
{
// Initialize shared variables
}
}
When multiple processes need to access the monitor, they are placed in a waiting queue. Each process gains access only after the previous process completes its operations and exits the monitor.
Semaphores
A semaphore is a signaling mechanism that uses an integer variable to control access to shared resources. Unlike mutexes, semaphores can be signaled by any thread, not just the one that initiated the wait operation.
Semaphores use two atomic operations for process synchronization −
Wait Operation
The wait() operation decrements the semaphore value if it is positive. If the value is zero or negative, the calling process is blocked.
wait(S)
{
while (S <= 0);
S--;
}
Signal Operation
The signal() operation increments the semaphore value and may wake up a waiting process.
signal(S)
{
S++;
}
Types of Semaphores
Counting Semaphores − Integer-valued semaphores with unrestricted value domains. They coordinate access to resources where the semaphore count represents the number of available resources.
Binary Semaphores − Restricted to values 0 and 1, similar to mutexes. The wait operation succeeds only when the semaphore is 1, and signal operation works when the semaphore is 0.
Comparison
| Aspect | Monitors | Semaphores |
|---|---|---|
| Complexity | High-level, easier to use | Low-level, more error-prone |
| Mutual Exclusion | Built-in automatically | Must be programmed explicitly |
| Data Encapsulation | Encapsulates data and procedures | Only controls access, no encapsulation |
| Error Handling | Less prone to timing errors | Susceptible to deadlocks and timing issues |
| Language Support | Requires language-level support | Can be implemented with basic primitives |
Conclusion
Monitors provide a higher-level, safer approach to synchronization by automatically ensuring mutual exclusion and encapsulating shared data. Semaphores offer more flexibility but require careful programming to avoid timing errors and deadlocks. The choice depends on the specific requirements and available language support.
