Semaphore and Monitor both allow processes to access the shared resources in mutual exclusion. Both are the process synchronization tool. Instead, they are very different from each other. Where Semaphore is an integer variable which can be operated only by wait() and signal() operation apart from the initialization.
On the other hand, the Monitor type is an abstract data type whose construct allow one process to get activate at one time. In this article, we will discuss the differences between semaphore and monitor with the help of comparison chart shown below.
Content: Semaphore Vs Monitor
Comparison Chart
Basis for Comparison | Semaphore | Monitor |
---|---|---|
Basic | Semaphores is an integer variable S. | Monitor is an abstract data type. |
Action | The value of Semaphore S indicates the number of shared resources availabe in the system | The Monitor type contains shared variables and the set of procedures that operate on the shared variable. |
Access | When any process access the shared resources it perform wait() operation on S and when it releases the shared resources it performs signal() operation on S. | When any process wants to access the shared variables in the monitor, it needs to access it through the procedures. |
Condition variable | Semaphore does not have condition variables. | Monitor has condition variables. |
Definition of Semaphore
Being a process synchronization tool, Semaphore is an integer variable S. This integer variable S is initialized to the number of resources present in the system. The value of semaphore S can be modified only by two functions wait() and signal() apart from initialization.
The wait() and signal() operation modifies the value of the semaphore S indivisibly. Which means when a process is modifying the value of the semaphore, no other process can simultaneously modify the value of the semaphore. Further, the operating system distinguishes the semaphore in two categories Counting semaphores and Binary semaphore.
In Counting Semaphore, the value of semaphore S is initialized to the number of resources present in the system. Whenever a process wants to access the shared resources, it performs wait() operation on the semaphore which decrements the value of semaphore by one. When it releases the shared resource, it performs a signal() operation on the semaphore which increments the value of semaphore by one.
When the semaphore count goes to 0, it means all resources are occupied by the processes. If a process need to use a resource when semaphore count is 0, it executes wait() and get blocked until a process utilizing the shared resources releases it and the value of semaphore becomes greater than 0.
In Binary semaphore, the value of semaphore ranges between 0 and 1. It is similar to mutex lock, but mutex is a locking mechanism whereas, the semaphore is a signalling mechanism. In binary semaphore, if a process wants to access the resource it performs wait() operation on the semaphore and decrements the value of semaphore from 1 to 0.
When process releases the resource, it performs a signal() operation on the semaphore and increments its value to 1. If the value of the semaphore is 0 and a process want to access the resource it performs wait() operation and block itself till the current process utilizing the resources releases the resource.
Definition of Monitor
To overcome the timing errors that occurs while using semaphore for process synchronization, the researchers have introduced a high-level synchronization construct i.e. the monitor type. A monitor type is an abstract data type that is used for process synchronization.
Being an abstract data type monitor type contains the shared data variables that are to be shared by all the processes and some programmer-defined operations that allow processes to execute in mutual exclusion within the monitor. A process can not directly access the shared data variable in the monitor; the process has to access it through procedures defined in the monitor which allow only one process to access the shared variables in a monitor at a time.
The syntax of monitor is as follow:
monitor monitor_name { //shared variable declarations procedure P1 ( . . . ) { } procedure P2 ( . . . ) { } procedure Pn ( . . . ) { } initialization code ( . . . ) { } }
A monitor is a construct such as only one process is active at a time within the monitor. If other process tries to access the shared variable in monitor, it gets blocked and is lined up in the queue to get the access to shared data when previously accessing process releases it.
Conditional variables were introduced for additional synchronization mechanism. The conditional variable allows a process to wait inside the monitor and allows a waiting process to resume immediately when the other process releases the resources.
The conditional variable can invoke only two operation wait() and signal(). Where if a process P invokes a wait() operation it gets suspended in the monitor till other process Q invoke signal() operation i.e. a signal() operation invoked by a process resumes the suspended process.
Key Differences Between Semaphore and Monitor
- The basic difference between semaphore and monitor is that the semaphore is an integer variable S which indicate the number of resources available in the system whereas, the monitor is the abstract data type which allows only one process to execute in critical section at a time.
- The value of semaphore can be modified by wait() and signal() operation only. On the other hand, a monitor has the shared variables and the procedures only through which shared variables can be accessed by the processes.
- In Semaphore when a process wants to access shared resources the process performs wait() operation and block the resources and when it release the resources it performs signal() operation. In monitors when a process needs to access shared resources, it has to access them through procedures in monitor.
- Monitor type has condition variables which semaphore does not have.
Conclusion
Monitors are easy to implement than semaphore, and there is little chance of mistake in monitor in comparison to semaphores.
Yagni Madhav says
clearly differentiated.
Mohammad Mahdi says
Very good content…
Alek says
I’m amazed at your tenacity writing this article, well written…
AbdulQayyum Kashmiri says
Excellent details can be improved further if the examples in the written code are also added.