Monitor Vs Semaphore ? What Are the Main Differences?


monitor vs semaphore

*This post may contain affiliate links. As an Amazon Associate we earn from qualifying purchases.

The monitor vs semaphore question is quite common in IT interviews. Let us tell you the key facts you should mention to provide a complete answer to this question and ace your interview. Let?s start with the basics.

What Is a Semaphore?

Semaphores are data structures used for higher level synchronization. They are aimed to provide mutual exclusion to the critical sections indicated by the programmer. Semaphores are meant to accomplish two types of operations:

  • Wait ? Wait ()

This operation will block a thread until an indicated condition is carried out. Essentially the thread will be allowed to continue only if the semaphore is open and it will be blocked on the queue if the semaphore is closed.

  • Signal ? Signal ()

The signal operation will allow a thread to enter. When one thread is waiting in the queue, the signal () will unblock it by opening the semaphore. If there are no threads on queue, the signal will be remembered for the following thread, acting like history feature.

Semaphore Types

There are two main types of semaphores you should know about. Each of them can be used for a certain type of higher level synchronization.

The Mutex Semaphore

This is the basic type of semaphore. It will provide single access to a certain resource. The Mutex will assure mutual exclusion for a critical section.

The Counting Semaphore

This type of semaphore is able to allow access to multiple threads through the semaphore. It can be used to provide various types of simultaneous access that is not synchronized. This semaphore will also determine the number of threads that have passed it by using the ?count? feature.

What Is a Monitor?

A monitor is another type of data structure utilized for higher level synchronization. It is meant to control the access to shared data between users. It will also protect the data from any type of unstructured access because the information may only be accessed from within the monitor by using the provided procedures.

A monitor guarantees mutual exclusion. This means that one thread only may execute a monitor procedure at any given time. When the monitor procedure is invoked by a second thread, the second thread will be blocked.

Monitor Vs Semaphore – The Essential Differences

In spite of the fact that both semaphores and monitors are used to serve the same purpose (higher level synchronization), the are a few key differences between them that may condition certain users to only one of them. This is the?difference between semaphore & monitor you should mention during your interview if you get the monitor vs semaphore question:

  1. Semaphores are?integer variables while monitors are abstract data types.
  2. While the Wait () feature does not always block the caller for semaphores, in monitors it is meant to block the caller every time.
  3. In semaphores, the Signal () can release a blocked thread and then both this thread and the called can continue. This is impossible for monitors, though, because they are meant to provide mutual exclusion. This means that either the caller or the released thread will be allowed to continue, but never both.
  4. The value of semaphores is the?number of shared resources available in the system while monitors contain?shared variables?and the set of procedures that operate on them.

Image Source:?Staticflickr

Recent Posts