The main difference between Semaphore and Monitor is that Semaphore is an integer variable that performs wait() and signal() operations, while Monitor is an abstract data type that allows only one process to use the shared resource at a time.
Usually, multiple processes run on an operating system. When many processes access shared resources, it can cause a race condition. Therefore, it is essential to synchronize the processes. Semaphore and Monitor are two methods to implement process synchronization.
Key Areas Covered
1. What is Semaphore
-Definition, Functionality
2. What is Monitor
-Definition, Functionality
3. Difference Between Semaphore and Monitor
-Comparison of key differences
Key Terms
Monitor, Semaphore
What is Semaphore
Semaphore is an integer variable (S). The atomic operations wait () and signal () help to modify the value of a semaphore. When one process modifies the semaphore, another cannot change it at the same time.
There are two types of semaphores. A binary semaphore can have two integer values: 0 and 1. The critical section is a code segment that accesses shared variables or shared resources. When a process needs to access the critical section, it performs wait() operation and decreases the semaphore value from 1 to 0. After exiting the critical section, it performs signal() operation and the semaphore value increases back to 1.
A counting semaphore can have multiple values. When the process needs to enter the critical section, it performs wait(). Then, the semaphore value decrement by 1. After exiting the critical section, it performs signal() operation. Then, the semaphore value increases by 1.
In both scenarios, if the semaphore value is 0, then all resources are in use. Therefore, the process performs wait() until the currently running process releases the resource and semaphore value becomes greater than 0.
What is Monitor
Monitor is an abstract data type. It contains shared data variables and the set of procedures that operate the shared variable.
A process cannot directly access a shared variable in the monitor. Instead, the process has to access it through procedures defined in the monitor. Only one process can access the shared variable in the monitor at a time. In other words, only one process is active at a time. Likewise, the monitor helps to maintain data consistency. If another process requires accessing the shared variable, it has to wait in the queue until the previous process releases it.
Moreover, there are conditional variables in monitors. These allow a process to wait inside a monitor and continue when the previous process releases the resource.
Difference Between Semaphore and Monitor
Definition
Semaphore is a variable used to control access to a common resource by multiple processes in a concurrent system such as a multitasking operating system. In contrast, monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait(block) for a certain condition to become true. Thus, this is the main difference between semaphore and monitor.
Type
Another difference between semaphore and monitor is that Semaphore is an integer variable while Monitor is an abstract data type.
Condition variable
Moreover, there is no condition variable concept in semaphores whereas monitor has condition variables.
Accessing
When a process requires access to the semaphore, it performs wait() and performs signal() when releasing the resource. On the other hand, a process uses procedures to access the shared variable in the monitor. Hence, this is also a difference between semaphore and monitor.
Conclusion
In breif, semaphore and monitor are two methods of synchronizing processes. The main difference between semaphore and monitor is that the semaphore is an integer variable that performs wait() and signal() operations, while the monitor is an abstract data type that allows only one process to use the shared resource at a time.
References
1.“Semaphore (Programming).” Wikipedia, Wikimedia Foundation, 1 May 2019, Available here.
2.“Monitor (Synchronization).” Wikipedia, Wikimedia Foundation, 19 Apr. 2019, Available here.
Image Courtesy:
1.”State diagram” By No machine-readable author provided. A3r0 assumed (based on copyright claims). – No machine-readable source provided. Own work assumed (based on copyright claims)., Public Domain, via Commons Wikimedia
Leave a Reply