Home » Difference between Semaphore and Monitor

Difference between Semaphore and Monitor

by Online Tutorials Library

Difference between Semaphore and Monitor

In this article, you will learn the difference between the semaphore and monitor. But before discussing the differences, you will need to know about the semaphore and monitor.

Semaphore vs Monitor

What is Semaphore?

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. It is an integer variable (S), and it is initialized with the number of resources in the system. The wait() and signal() methods are the only methods that may modify the semaphore (S) value. When one process modifies the semaphore value, other processes can’t modify the semaphore value simultaneously.

Furthermore, the operating system categorizes semaphores into two types:

  1. Counting Semaphore
  2. Binary Semaphore

Counting Semaphore

In Counting Semaphore, the value of semaphore S is initialized to the number of resources in the system. When a process needs to access shared resources, it calls the wait() method on the semaphore, decreasing its value by one. When the shared resource is released, it calls the signal() method, increasing the value by 1.

When the semaphore count reaches 0, it implies that the processes have used all resources. Suppose a process needs to utilize a resource when the semaphore count is 0. In that case, it performs the wait() method, and it is blocked until another process using the shared resources releases it, and the value of the semaphore increases to 1.

Binary Semaphore

Semaphore has a value between 0 and 1 in binary semaphore. It’s comparable to mutex lock, except that mutex is a locking method while the semaphore is a signalling method. When a process needs to access a binary semaphore resource, it uses the wait() method to decrement the semaphore’s value from 1 to 0.

When the process releases the resource, it uses the signal() method to increase the semaphore value to 1. When the semaphore value is 0, and a process needs to use the resource, it uses the wait() method to block until the current process that is using the resource releases it.

Syntax:

The syntax of the semaphore may be used as:

Advantages and Disadvantages of Semaphore

Various advantages and disadvantages of the semaphore are as follows:

Advantages;

  1. They don’t allow multiple processes to enter the critical part simultaneously. Mutual exclusion is achieved in this manner, making it much more efficient than other synchronization techniques.
  2. There is no waste of process time or resources as a result of the busy waiting in semaphore. It is because processes are only allowed to access the critical section if a certain condition is satisfied.
  3. They enable resource management that is flexible.
  4. They are machine-independent because they execute in the microkernel’s machine-independent code.

Disadvantages

  1. There could be a situation of priority inversion where the processes with low priority get access to the critical section than those with higher priority.
  2. Semaphore programming is complex, and there is a risk that mutual exclusion will not be achieved.
  3. The wait() and signal() methods must be conducted correctly to avoid deadlocks.

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:

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.
  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.

Head-to-head comparison between the Semaphore and Monitor

Various head-to-head comparisons between the semaphore and monitor are as follows:

Features Semaphore Monitor
Definition 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. It is a synchronization process that enables threads to have mutual exclusion and the wait() for a given condition to become true.
Syntax // Wait Operation
wait(Semaphore S) {
while (S<=0);
S–;
}
// Signal Operation
signal(Semaphore S) {
S++;
}
monitor {
//shared variable declarations
data variables;
Procedure P1() { … }
Procedure P2() { … }
.
.
.
Procedure Pn() { … }
}
Basic Integer variable Abstract data type
Access When a process uses shared resources, it calls the wait() method on S, and when it releases them, it uses the signal() method on S. When a process uses shared resources in the monitor, it has to access them via procedures.
Action The semaphore’s value shows the number of shared resources available in the system. The Monitor type includes shared variables as well as a set of procedures that operate on them.
Condition Variable No condition variables. It has condition variables.

Conclusion

In summary, semaphore and monitor are two synchronization mechanisms. A semaphore is an integer variable that performs the wait() and signal() methods. In contrast, the monitor is an abstract data type that enables only a process to use a shared resource at a time. Monitors are simpler to implement than semaphores, and there are fewer chances of making a mistake in monitors than with semaphores.


You may also like