SlideShare a Scribd company logo
Operating System 26
Monitors
Prof Neeraj Bhargava
Vaibhav Khanna
Department of Computer Science
School of Engineering and Systems Sciences
Maharshi Dayanand Saraswati University Ajmer
Monitors
• A high-level abstraction that provides a convenient and effective mechanism for
process synchronization
• Abstract data type, internal variables only accessible by code within the
procedure
• Only one process may be active within the monitor at a time
• But not powerful enough to model some synchronization schemes
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
procedure Pn (…) {……}
Initialization code (…) { … }
}
}
Schematic view of a Monitor
Condition Variables
• condition x, y;
• Two operations are allowed on a
condition variable:
– x.wait() – a process that invokes the
operation is suspended until x.signal()
– x.signal() – resumes one of processes (if
any) that invoked x.wait()
• If no x.wait() on the variable, then it has no
effect on the variable
Monitor with Condition Variables
Condition Variables Choices
• If process P invokes x.signal(), andprocess Q is
suspended in x.wait(), what should happen next?
– Both Q and P cannot execute in paralel. If Q is resumed, then
P must wait
• Options include
– Signal and wait – P waits until Q either leaves the monitor or
it waits for another condition
– Signal and continue – Q waits until P either leaves the
monitor or it waits for another condition
– Both have pros and cons – language implementer can decide
– Monitors implemented in Concurrent Pascal compromise
• P executing signal immediately leaves the monitor, Q is resumed
– Implemented in other languages including Mesa, C#, Java
Monitor Solution to Dining Philosophers
monitor DiningPhilosophers
{
enum { THINKING; HUNGRY, EATING) state [5] ;
condition self [5];
void pickup (int i) {
state[i] = HUNGRY;
test(i);
if (state[i] != EATING) self[i].wait;
}
void putdown (int i) {
state[i] = THINKING;
// test left and right neighbors
test((i + 4) % 5);
test((i + 1) % 5);
}
Solution to Dining Philosophers
(Cont.)
void test (int i) {
if ((state[(i + 4) % 5] != EATING) &&
(state[i] == HUNGRY) &&
(state[(i + 1) % 5] != EATING) ) {
state[i] = EATING ;
self[i].signal () ;
}
}
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
• Each philosopher i invokes the operations pickup()
and putdown() in the following sequence:
DiningPhilosophers.pickup(i);
EAT
DiningPhilosophers.putdown(i);
• No deadlock, but starvation is possible
Solution to Dining Philosophers (Cont.)
Monitor Implementation Using Semaphores
• Variables
semaphore mutex; // (initially = 1)
semaphore next; // (initially = 0)
int next_count = 0;
• Each procedure F will be replaced by
wait(mutex);
…
body of F;
…
if (next_count > 0)
signal(next)
else
signal(mutex);
• Mutual exclusion within a monitor is ensured
Monitor Implementation – Condition Variables
• For each condition variable x, we have:
semaphore x_sem; // (initially =
0)
int x_count = 0;
• The operation x.wait can be implemented as:
x_count++;
if (next_count > 0)
signal(next);
else
signal(mutex);
wait(x_sem);
x_count--;
Monitor Implementation (Cont.)
• The operation x.signal can be implemented
as:
if (x_count > 0) {
next_count++;
signal(x_sem);
wait(next);
next_count--;
}
Resuming Processes within a Monitor
• If several processes queued on condition
x, and x.signal() executed, which should
be resumed?
• FCFS frequently not adequate
• conditional-wait construct of the form
x.wait(c)
– Where c is priority number
– Process with lowest number (highest
priority) is scheduled next
• Allocate a single resource among competing
processes using priority numbers that specify the
maximum time a process plans to use the
resource
R.acquire(t);
...
access the resurce;
...
R.release;
• Where R is an instance of type ResourceAllocator
Single Resource allocation
A Monitor to Allocate Single
Resource
monitor ResourceAllocator
{
boolean busy;
condition x;
void acquire(int time) {
if (busy)
x.wait(time);
busy = TRUE;
}
void release() {
busy = FALSE;
x.signal();
}
initialization code() {
busy = FALSE;
}
}

More Related Content

PPS
Virus & Computer security threats
PPTX
Python Exception Handling
PDF
Python file handling
PPTX
Cyber security
PPT
Looping statements in Java
PPTX
presentation on hacking
PPTX
Interface in java
PPT
Python Control structures
Virus & Computer security threats
Python Exception Handling
Python file handling
Cyber security
Looping statements in Java
presentation on hacking
Interface in java
Python Control structures

What's hot (20)

PPTX
Loops in c programming
PPTX
Computer networking For Class XII
PPTX
Methods in java
PPT
Intro To Programming Concepts
PPT
Structure of a C program
PPTX
Algorithms and Flowcharts
PPTX
C introduction by thooyavan
PPTX
Email basics
PPTX
Remote login
PDF
Python exception handling
PDF
Unit Testing in Python
PPTX
Control Statements in Java
PPT
Php Error Handling
PPTX
Java if else condition - powerpoint persentation
PPTX
Programming in c Arrays
PPT
Java interfaces
PPT
Telnet
PPTX
Virus
PPTX
09. Java Methods
PDF
Python introduction
Loops in c programming
Computer networking For Class XII
Methods in java
Intro To Programming Concepts
Structure of a C program
Algorithms and Flowcharts
C introduction by thooyavan
Email basics
Remote login
Python exception handling
Unit Testing in Python
Control Statements in Java
Php Error Handling
Java if else condition - powerpoint persentation
Programming in c Arrays
Java interfaces
Telnet
Virus
09. Java Methods
Python introduction
Ad

Similar to Operating system 26 monitors (20)

PPTX
Dining Philosopher Problem and Solution
PPT
Mca ii os u-2 process management & communication
PPTX
Dining Philosopher Problem | Process Synchronisation | Operating System | Tea...
PPTX
dining philosophers problem using montiors
PPT
Classical-Problem-of-Synchronization in OS
PDF
Monitors
DOCX
UNIT III Process Synchronization.docx
PPTX
Process cooperation and synchronisation
PPT
Monitor(karthika)
PPT
Chapter 6 - Process Synchronization
PPTX
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PPTX
Operating system 25 classical problems of synchronization
PPT
PDF
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
PPTX
Deadlock _Classic problems.pptx
PPT
Inter process communication
DOCX
Critical section operating system
PPTX
Monitors
Dining Philosopher Problem and Solution
Mca ii os u-2 process management & communication
Dining Philosopher Problem | Process Synchronisation | Operating System | Tea...
dining philosophers problem using montiors
Classical-Problem-of-Synchronization in OS
Monitors
UNIT III Process Synchronization.docx
Process cooperation and synchronisation
Monitor(karthika)
Chapter 6 - Process Synchronization
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Operating system 25 classical problems of synchronization
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
Deadlock _Classic problems.pptx
Inter process communication
Critical section operating system
Monitors
Ad

More from Vaibhav Khanna (20)

PPTX
Information and network security 47 authentication applications
PPTX
Information and network security 46 digital signature algorithm
PPTX
Information and network security 45 digital signature standard
PPTX
Information and network security 44 direct digital signatures
PPTX
Information and network security 43 digital signatures
PPTX
Information and network security 42 security of message authentication code
PPTX
Information and network security 41 message authentication code
PPTX
Information and network security 40 sha3 secure hash algorithm
PPTX
Information and network security 39 secure hash algorithm
PPTX
Information and network security 38 birthday attacks and security of hash fun...
PPTX
Information and network security 37 hash functions and message authentication
PPTX
Information and network security 35 the chinese remainder theorem
PPTX
Information and network security 34 primality
PPTX
Information and network security 33 rsa algorithm
PPTX
Information and network security 32 principles of public key cryptosystems
PPTX
Information and network security 31 public key cryptography
PPTX
Information and network security 30 random numbers
PPTX
Information and network security 29 international data encryption algorithm
PPTX
Information and network security 28 blowfish
PPTX
Information and network security 27 triple des
Information and network security 47 authentication applications
Information and network security 46 digital signature algorithm
Information and network security 45 digital signature standard
Information and network security 44 direct digital signatures
Information and network security 43 digital signatures
Information and network security 42 security of message authentication code
Information and network security 41 message authentication code
Information and network security 40 sha3 secure hash algorithm
Information and network security 39 secure hash algorithm
Information and network security 38 birthday attacks and security of hash fun...
Information and network security 37 hash functions and message authentication
Information and network security 35 the chinese remainder theorem
Information and network security 34 primality
Information and network security 33 rsa algorithm
Information and network security 32 principles of public key cryptosystems
Information and network security 31 public key cryptography
Information and network security 30 random numbers
Information and network security 29 international data encryption algorithm
Information and network security 28 blowfish
Information and network security 27 triple des

Recently uploaded (20)

PPTX
Introduction to Artificial Intelligence
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
System and Network Administraation Chapter 3
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Essential Infomation Tech presentation.pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Transform Your Business with a Software ERP System
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Introduction to Artificial Intelligence
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Design an Analysis of Algorithms I-SECS-1021-03
System and Network Administraation Chapter 3
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Essential Infomation Tech presentation.pptx
Design an Analysis of Algorithms II-SECS-1021-03
Wondershare Filmora 15 Crack With Activation Key [2025
L1 - Introduction to python Backend.pptx
Transform Your Business with a Software ERP System
2025 Textile ERP Trends: SAP, Odoo & Oracle
How to Migrate SBCGlobal Email to Yahoo Easily
Odoo POS Development Services by CandidRoot Solutions
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
How Creative Agencies Leverage Project Management Software.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises

Operating system 26 monitors

  • 1. Operating System 26 Monitors Prof Neeraj Bhargava Vaibhav Khanna Department of Computer Science School of Engineering and Systems Sciences Maharshi Dayanand Saraswati University Ajmer
  • 2. Monitors • A high-level abstraction that provides a convenient and effective mechanism for process synchronization • Abstract data type, internal variables only accessible by code within the procedure • Only one process may be active within the monitor at a time • But not powerful enough to model some synchronization schemes monitor monitor-name { // shared variable declarations procedure P1 (…) { …. } procedure Pn (…) {……} Initialization code (…) { … } } }
  • 3. Schematic view of a Monitor
  • 4. Condition Variables • condition x, y; • Two operations are allowed on a condition variable: – x.wait() – a process that invokes the operation is suspended until x.signal() – x.signal() – resumes one of processes (if any) that invoked x.wait() • If no x.wait() on the variable, then it has no effect on the variable
  • 6. Condition Variables Choices • If process P invokes x.signal(), andprocess Q is suspended in x.wait(), what should happen next? – Both Q and P cannot execute in paralel. If Q is resumed, then P must wait • Options include – Signal and wait – P waits until Q either leaves the monitor or it waits for another condition – Signal and continue – Q waits until P either leaves the monitor or it waits for another condition – Both have pros and cons – language implementer can decide – Monitors implemented in Concurrent Pascal compromise • P executing signal immediately leaves the monitor, Q is resumed – Implemented in other languages including Mesa, C#, Java
  • 7. Monitor Solution to Dining Philosophers monitor DiningPhilosophers { enum { THINKING; HUNGRY, EATING) state [5] ; condition self [5]; void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self[i].wait; } void putdown (int i) { state[i] = THINKING; // test left and right neighbors test((i + 4) % 5); test((i + 1) % 5); }
  • 8. Solution to Dining Philosophers (Cont.) void test (int i) { if ((state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING) ) { state[i] = EATING ; self[i].signal () ; } } initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING; } }
  • 9. • Each philosopher i invokes the operations pickup() and putdown() in the following sequence: DiningPhilosophers.pickup(i); EAT DiningPhilosophers.putdown(i); • No deadlock, but starvation is possible Solution to Dining Philosophers (Cont.)
  • 10. Monitor Implementation Using Semaphores • Variables semaphore mutex; // (initially = 1) semaphore next; // (initially = 0) int next_count = 0; • Each procedure F will be replaced by wait(mutex); … body of F; … if (next_count > 0) signal(next) else signal(mutex); • Mutual exclusion within a monitor is ensured
  • 11. Monitor Implementation – Condition Variables • For each condition variable x, we have: semaphore x_sem; // (initially = 0) int x_count = 0; • The operation x.wait can be implemented as: x_count++; if (next_count > 0) signal(next); else signal(mutex); wait(x_sem); x_count--;
  • 12. Monitor Implementation (Cont.) • The operation x.signal can be implemented as: if (x_count > 0) { next_count++; signal(x_sem); wait(next); next_count--; }
  • 13. Resuming Processes within a Monitor • If several processes queued on condition x, and x.signal() executed, which should be resumed? • FCFS frequently not adequate • conditional-wait construct of the form x.wait(c) – Where c is priority number – Process with lowest number (highest priority) is scheduled next
  • 14. • Allocate a single resource among competing processes using priority numbers that specify the maximum time a process plans to use the resource R.acquire(t); ... access the resurce; ... R.release; • Where R is an instance of type ResourceAllocator Single Resource allocation
  • 15. A Monitor to Allocate Single Resource monitor ResourceAllocator { boolean busy; condition x; void acquire(int time) { if (busy) x.wait(time); busy = TRUE; } void release() { busy = FALSE; x.signal(); } initialization code() { busy = FALSE; } }