×

How to implement Monitors using Semaphores

Monitors

Monitor is a type of synchronization device designed to solve difficulties caused by semaphores, such as timing errors. Monitors are the type of data types that are abstract by nature and contain common data variables and operations. A process cannot directly access shared data variables, so processes are required to allow one process to access shared data variables at a time.

In a monitor, only one process can be active at a time. Other processes that want access to the shared variable in the monitor must be queued and access is granted only after the prior process has released the shared variable.

Semaphores

A semaphore is a signaling technology that allows another thread to signal a thread that is waiting for a semaphore. This is not the same as a mutex, which can only be signaled by the thread that implements the wait function. For process synchronization, a semaphore employs two atomic operations: wait and signal. If the value of its input A is positive, the wait operation decrements it. If A is negative or zero then no operation is performed.

wait(A)
{
    while (A<=0);
     A--;
}

The value of the signal operation's parameter A is increased.

signal(A)
{  A++;
}

Counting semaphores and binary semaphores are the two most common forms of semaphores. Counting Semaphores have an unconstrained value domain and are integer value semaphores. The amount of accessible resources is represented by the semaphore count, which is used to coordinate resource access. Binary semaphores are similar to counting semaphores, except that their values are limited to 0 and 1. When the semaphore is 1, the wait action succeeds, and when the semaphore is 0, the signal operation succeeds.

Implementation

A semaphore mutex (which is initialised to 1) is given for each monitor to implement monitor utilising semaphores. Before entering the monitor, a process must perform wait(mutex), and after exiting the monitor, signal(mutex) must be executed. Because a signalling process must wait until the resumed process departs or waits, an extra semaphore is added next, which is set to 0. The signalling mechanisms can exploit this to put themselves in a state of suspension. The number of processes suspended on next is counted using the integer variable next count. As a result, each external function A is substituted with

wait(mutex);
...
body of A
...
if (next_count > 0)
   signal(next);
else
   signal(mutex);

Within a monitor, mutual exclusion is guaranteed. Let's have a look at how condition variables are implemented. We add a semaphore i_sem and an integer variable i count for each condition i, both of which are initialised to 0. The i.wait() method may now be implemented as:

i_count++;
if (next_count &gt; 0){
   signal(next);
}
   else {
   signal(mutex);
}
wait(i_sem);
i_count--;

The operation i.signal() can be implemented in the following way:

if (i _count > 0){
   next_count++;
   signal(i_sem);
   wait(next);
   next_count--;
}

Related Topics

Banker’s Algorithm in Operating System

What is Banker’s Algorithm? Bankers algorithm is an algorithm which is used for deadlock avoidance and resource allocation. It was established by Edsger Dijkstra. The reason behind the name ‘banker’s algorithm’...

8 minutes read.

Difference between C-LOOK and C-SCAN Disk Scheduling Algorithm

Disk scheduling is used by operating systems to arrange the arrival of I/O requests to the disc. I/O scheduling is another name for disc scheduling. Disk scheduling is necessary because...

4 minutes read.

Distributed Shared Memory

Distributed Shared Memory is a process of managing memory over more than one node and makes inter-process communication which transfers to end users. It is a mechanism that allows user...

2 minutes read.

Turn Variable or Strict Alternative Approach

Turn Variable or Strict Alternative Approach Turn Variable Approach is used for process synchronization mechanism, which offers synchronization between two processes. Turn variable is implemented on user mode, and it is a software mechanism. We...

3 minutes read.

Interested Variable Mechanism

Interested Variable Mechanism It is a must to provide progress to the process synchronization mechanism. In variable mechanism, if there is a process that doesn’t need to enter into the critical section, then due...

5 minutes read.

Disk Scheduling Algorithms

Disk Scheduling Algorithms Disk scheduling algorithms are the algorithms that are used for scheduling a disk. Generally, the scheduling refers to a time-table for completing any task or a job. With the help...

2 minutes read.

Feedback Queue in Operating System

In a computer or a laptop, there is a processor which do all the tasks. The processor cannot do all the tasks at a time. So, the tasks will be...

3 minutes read.

Simple Structure in Operating System

An operating system is a piece of software that lets the user applications talk to the hardware of the system. Because it is such a complicated structure, the operating system...

3 minutes read.

Deadlock Detection and Recovery

Deadlock Detection and Recovery In deadlock detection and recovery, to avoid or prevent a deadlock, the operating system uses various methods. For checking deadlock, an operating system continuously monitors the system. Then also,...

2 minutes read.

Single-User Operating System

An operating system designed and intended to be used on a computer or device with a single user at a time is known as a single-user operating system. Devices like...

8 minutes read.

File Allocation Methods in Operating System

A file allocation method is a way that an operating system stores and retrieves files on a storage device, such as a hard drive or SSD. The file allocation method...

4 minutes read.

Linux Operating System

What is Linux? The Linux operating system is used on various devices, including cellphones, automobiles, supercomputers, household gadgets, personal computers, and business servers. You can find Linux on your phones, thermostats, vehicles,...

8 minutes read.

Batch Operating System

Computers were once quite huge devices that operated from a console. Tape drives, punch cards, line printers were often utilized for the output, and tape drivers or card readers for...

4 minutes read.

Ubuntu Operating System

Ubuntu Operating System Ubuntu is an open-source and a Linux based operating system. Ubuntu is developed for the Network Servers, Computers, and Smartphones. The Ubuntu operating system was developed by Canonical...

3 minutes read.

DOS Operating System

What is DOS Operating System? An operating system that runs from a hard disk drive is known as the DOS (Disk Operating System).In other words, DOS (Disk Operating System) is defined...

7 minutes read.

Contiguous Memory Allocation in Operating System

Memory is a place where data, facts, figures, and information are stored for temporary or permanent access the data in future, which is also considered as the storage place. Allocation of...

4 minutes read.

Fixed Partitioning in Operating System

Fixed Partitioning in OS Fixed Partitioning is also known as Contiguous memory allocation. Fixed Partitioning is the easiest method, which is used to load more than one process into the main memory. In Fixed...

5 minutes read.

CPU Scheduling in OS

What is Scheduling? Scheduling is the process that is used to share the computing resources such as memory, processor time, and bandwidth to the different processes, data flows, threads, and applications that need...

5 minutes read.

Starvation in Operating System

Starvation is one of the major problems occurring when resource management is not done properly. This problem arises in the operating system when the resources are not being allocated for...

4 minutes read.

Partitioning Algorithms

Partitioning Algorithms There are various types of Partitioning Algorithm: First-Fit AlgorithmNext-Fit AlgorithmBest-Fit AlgorithmWorst-Fit AlgorithmQuick-Fit Algorithm 1. First-Fit Algorithm: - In the First-Fit Partitioning algorithm, first, the linked list is scan, and when it...

3 minutes read.