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
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
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.
Another difference between semaphore and monitor is that Semaphore is an integer variable while Monitor is an abstract data type.
Moreover, there is no condition variable concept in semaphores whereas monitor has condition variables.
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.
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.
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