You are on page 1of 3

11/24/22, 5:37 AM Difference between Semaphore and Monitor - javatpoint

What is Monitor?
It is a synchronization technique that enables threads to mutual exclusion and the wait() for a given condition to become true. It is an
abstract data type. It has a shared variable and a collection of procedures executing on the shared variable. A process may not directly
access the shared data variables, and procedures are required to allow several processes to access the shared data variables
simultaneously.

At any particular time, only one process may be active in a monitor. Other processes that require access to the shared variables must
queue and are only granted access after the previous process releases the shared variables.

Syntax:

The syntax of the monitor may be used as:

monitor {

//shared variable declarations


data variables;
Procedure P1() { ... }
https://www.javatpoint.com/semaphore-vs-monitor#:~:text=are as follows%3A-,A semaphore is an integer variable that allows many processes,given condition to become true. 1/3
11/24/22, 5:37 AM Difference between Semaphore and Monitor - javatpoint

Procedure P2() { ... }


.
.
.
Procedure Pn() { ... }
Initialization Code() { ... }
}

Advantages and Disadvantages of Monitor

Various advantages and disadvantages of the monitor are as follows:

Advantages

1. Mutual exclusion is automatic in monitors.

2. Monitors are less difficult to implement than semaphores.

3. Monitors may overcome the timing errors that occur when semaphores are used.

4. Monitors are a collection of procedures and condition variables that are combined in a special type of module.

Disadvantages

1. Monitors must be implemented into the programming language.

2. The compiler should generate code for them.

https://www.javatpoint.com/semaphore-vs-monitor#:~:text=are as follows%3A-,A semaphore is an integer variable that allows many processes,given condition to become true. 2/3
11/24/22, 5:37 AM Difference between Semaphore and Monitor - javatpoint

3. It gives the compiler the additional burden of knowing what operating system features is available for controlling access to crucial
sections in concurrent processes.

Main Differences between the Semaphore and Monitor


Here, you will learn the main differences between the semaphore and monitor. Some of the main differences are as follows:

1. A semaphore is an integer variable that allows many processes in a parallel system to manage access to a common resource like a
multitasking OS. On the other hand, a monitor is a synchronization technique that enables threads to mutual exclusion and
the wait() for a given condition to become true.

2. When a process uses shared resources in semaphore, it calls the wait() method and blocks the resources. When it wants to
release the resources, it executes the signal() In contrast, when a process uses shared resources in the monitor, it has to access
them via procedures.

3. Semaphore is an integer variable, whereas monitor is an abstract data type.

4. In semaphore, an integer variable shows the number of resources available in the system. In contrast, a monitor is an abstract data
type that permits only a process to execute in the crucial section at a time.

5. Semaphores have no concept of condition variables, while monitor has condition variables.

6. A semaphore's value can only be changed using the wait() and signal() In contrast, the monitor has the shared variables and the
tool that enables the processes to access them.

https://www.javatpoint.com/semaphore-vs-monitor#:~:text=are as follows%3A-,A semaphore is an integer variable that allows many processes,given condition to become true. 3/3

You might also like