Difference Between Semaphore and Monitor
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.
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.
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.
Page 1 sur 8
Syntax:
1. // Wait Operation
2. wait(Semaphore S) {
3. while (S<=0);
4. S--;
5. }
6. // Signal Operation
7. signal(Semaphore S) {
8. S++;
9. }
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.
Page 2 sur 8
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:
1. monitor {
2.
3. //shared variable declarations
4. data variables;
5. Procedure P1() { ... }
6. Procedure P2() { ... }
7. .
8. .
9. .
10. Procedure Pn() { ... }
11. Initialization Code() { ... }
12. }
Advantages
Disadvantages
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
Page 3 sur 8
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.
Various head-to-head comparisons between the semaphore and monitor are as follows:
monitor {
// Wait Operation
//shared variable declarations
wait(Semaphore S) {
data variables;
while (S<=0);
Procedure P1() { ... }
S--;
Procedure P2() { ... }
Syntax }
.
// Signal Operation
.
signal(Semaphore S) {
.
S++;
Procedure Pn() { ... }
}
}
When a process uses shared resources, it calls the When a process uses shared resources in
Access wait() method on S, and when it releases them, it the monitor, it has to access them via
uses the signal() method on S. procedures.
Page 4 sur 8
Condition
No condition variables. It has condition variables.
Variable
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.
Page 5 sur 8
A Monitor is an object designed to be accessed from multiple threads. The member functions or
methods of a monitor object will enforce mutual exclusion, so only one thread may be performing
any action on the object at a given time. If one thread is currently executing a member function of the
object then any other thread that tries to call a member function of that object will have to wait until
the first has finished.
A Semaphore is a lower-level object. You might well use a semaphore to implement a monitor. A
semaphore essentially is just a counter. When the counter is positive, if a thread tries to acquire the
semaphore then it is allowed, and the counter is decremented. When a thread is done then it releases
the semaphore, and increments the counter.
If the counter is already zero when a thread tries to acquire the semaphore then it has to wait until
another thread releases the semaphore. If multiple threads are waiting when a thread releases a
semaphore then one of them gets it. The thread that releases a semaphore need not be the same thread
that acquired it.
A monitor is like a public toilet. Only one person can enter at a time. They lock the door to prevent
anyone else coming in, do their stuff, and then unlock it when they leave.
A semaphore is like a bike hire place. They have a certain number of bikes. If you try and hire a bike
and they have one free then you can take it, otherwise you must wait. When someone returns their
bike then someone else can take it. If you have a bike then you can give it to someone else to return
--- the bike hire place doesn't care who returns it, as long as they get their bike back.
https://stackoverflow.com/questions/7335950/semaphore-vs-monitors-whats-the-difference
Page 6 sur 8
Monitor vs Semaphore
Both semaphores and monitors are used to solve the critical section problem (as they allow processes
to access the shared resources in mutual exclusion) and to achieve process synchronization in the
multiprocessing environment.
Monitor:
A Monitor type high-level synchronization construct. It is an abstract data type. The Monitor type
contains shared variables and the set of procedures that operate on the shared variable.
When any process wishes to access the shared variables in the monitor, it needs to access it through
the procedures. These processes line up in a queue and are only provided access when the previous
process release the shared variables. Only one process can be active in a monitor at a time. Monitor
has condition variables.
Syntax:
monitor {
Semaphore:
A Semaphore is a lower-level object. A semaphore is a non-negative integer variable. The value of
Semaphore indicates the number of shared resources available in the system. The value of semaphore
can be modified only by two functions, namely wait() and signal() operations (apart from the
initialization).
When any process accesses the shared resources, it performs the wait() operation on the semaphore
and when the process releases the shared resources, it performs the signal() operation on the
semaphore. Semaphore does not have condition variables. When a process is modifying the value of
the semaphore, no other process can simultaneously modify the value of the semaphore.
1. Binary semaphore
Page 7 sur 8
2. Counting semaphore
Syntax:
// Wait Operation
wait(Semaphore S) {
while (S<=0);
S--;
}
// Signal Operation
signal(Semaphore S) {
S++;
}
Advantages of Monitors:
Advantages of Semaphores:
Semaphores are machine independent (because they are implemented in the kernel services).
Semaphores permit more than one thread to access the critical section, unlike monitors.
In semaphores there is no spinning, hence no waste of resources due to no busy waiting.
Page 8 sur 8