×

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

Test Set Lock Mechanism | Operating System

Modification in the Assembly Code The problem in the lock variable is that, in some cases, the process reads the previous or old value of the lock variable and enters into the critical...

4 minutes read.

Process State in OS

The Process passes from different states, from its formation to completion. The following are the states of the Process. NewReadyRunningBlock or WaitTerminated or CompletedSuspend readySuspend wait or suspend blocked 1. New: - The state in which a process...

2 minutes read.

Need of Operating System

There are various needs of operating system: Managing Input-Output UnitThe Operating system as a platform for application programsMultitaskingConsistent user interface Managing Input-Output Unit: - With the help of the operating system, the...

2 minutes read.

MS-DOS Operating System

It is also referred to occasionally as "DOS", an abbreviation for disc operating systems. Operating systems with a graphical user interface (GUI) in different iterations of the graphical Microsoft Windows...

5 minutes read.

SJF Scheduling in OS

Shortest Job First (SJF) Scheduling in OS Shortest Job First is a Preemptive or Non-Preemptive algorithm. In the shortest job first algorithm, the job having shortest or less burst time will...

4 minutes read.

Bit Vector in Operating System

What is Bit vector? Bit vector is one of the most important methods used in the free space management of operating system. Free space management which is used by the operating...

3 minutes read.

What is Scheduling on OS

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

5 minutes read.

Process Management in OS

A Program will not do anything unless a CPU executes its instructions. An executing program is called a process. The Process requires computer resources to complete its task. More than...

3 minutes read.

Multitasking Operating Systems

An operating system is the fundamental piece of software that enables a computer system to function and execute a command as intended. The user interface, hardware, and every other piece...

7 minutes read.

Trap vs Interrupt in Operating System

What is Trap in Operating System? A trap is a synchronous interrupt used to perform functionality when an error occurs in a user process. A trap in an operating system can...

4 minutes read.

Multiprogramming Operating System

You will discover more about the multiprogramming operating system in this post, including how it functions and its benefits and drawbacks. Each process needs several types of system time, including CPU...

4 minutes read.

Priority Scheduling in OS

Priority Scheduling is a type of CPU Scheduling algorithm which is used for process scheduling. In Priority Scheduling, we assign some priorities to each process. The process which has higher priority among all...

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

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.

Multi-user Operating Systems

Introduction Since only one user may interact with the computer at any given time, the operating system we use on our personal computers, laptops, tablets, and phones is sometimes referred to...

8 minutes read.

Operating System Services

The Operating System provides various types of services:   I/O operationProgram executionFile system manipulationCommunicationError HandlingResource allocationAccountingProtection 1. I/O Operation: - To execute a program, needs I/O, which consists of a file, or I/O device. Due to the protection...

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

Difference between SCAN and FCFS Disk Scheduling Algorithm

SCAN / Elevator Algorithm A different form of disc scheduling method is the SCAN disc scheduling algorithm. We move the disc arm in a certain direction in this process (the direction...

3 minutes read.

Real-time Operating System

In this essay, we will thoroughly understand the real-time operating system. Real-Time Operating System: What do you mean? For any task to be completed, a real-time operating system (RTOS), a special-purpose operating...

5 minutes read.

Unix Operating System

Unix Operating System Unix Operating System is a multiuser, portable, time-sharing, and multitasking operating system that was developed in 1969. Unix operating system was developed by a group of AT &...

3 minutes read.