Difference Between Semaphore and Monitor in OS


Both Semaphore and Monitor are types of process synchronization tools in operating systems. Semaphores and monitors allow the different processes to utilize the shared resources in mutual exclusion, however they are different from each other. The basic difference between a semaphore and a monitor is that a semaphore is an integer variable, whereas a monitor is an abstract data type.

Read this article to find out more about semaphores and monitors and how they are different from each other.

What is Semaphore?

A semaphore is a process synchronizing tool. It is basically an integer variable, denoted by "S". The initialization of this variable "S" is done by assigning a number equal to the number of resources present in the system.

There are two functions, wait() and signal(), which are used to modify the value of semaphore "S". The wait() and signal() functions indivisibly change the value of the semaphore "S". That means, when one process is changing the value of the semaphore "S", another process cannot change the value of the semaphore at the same time.

In operating systems, semaphores are grouped into two categories− counting semaphore and binary semaphore. In a counting semaphore, the value of the semaphore is initialized to the number of resources present in the system. On the other hand, in a binary semaphore, the semaphore "S" has the value "0" or "1".

What is Monitor?

Monitor is also a process synchronization tool. It is an abstract data type that is used for highlevel synchronization of processes. It has been developed to overcome the timing errors that occur while using the semaphore for the process synchronization. Since the monitor is an abstract data type, it contains the shared data variables. These data variables are to be shared by all the processes. Hence, this allows the processes to execute in mutual exclusion.

There can be only one process active at a time within a monitor. If any other process tries to access the shared variable in the monitor, it will be blocked and lined up in the queue to get the access to the data. This is done by a "conditional variable" in the monitor. The conditional variable is used for providing additional synchronization mechanism.

Difference between Semaphore and Monitor in OS

The following table highlights all the important differences between semaphore and monitor in operating systems −

S. No. Semaphore Monitor
1. It is an integer variable. It is an abstract data type.
2. The value of this integer variable tells about the number of shared resources that are available in the system. It contains shared variables.
3. When any process has access to the shared resources, it performs the ‘wait’ operation (using wait method) on the semaphore. It also contains a set of procedures that operate upon the shared variable.
4. When a process releases the shared resources, it performs the ‘signal’ operation (using signal method) on the semaphore. When a process wishes to access the shared variables in the monitor, it has to do so using procedures.
5. It doesn’t have condition variables. It has condition variables.

Conclusion

Both semaphores and monitors are types process synchronization tools in operating systems, however they are quite different from each other, as descried in the above table. The most significant difference that you should note here is that a semaphore is an integer variable that indicates the number of resources available in the system, whereas a monitor is an abstract data type that allows only one process to be executed at a time.

Updated on: 21-Dec-2022

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements