Professional Documents
Culture Documents
Monitors: An Operating System Structuring Concept: Paper by C. A. R. Hoare Presentation by Emerson Murphy-Hill
Monitors: An Operating System Structuring Concept: Paper by C. A. R. Hoare Presentation by Emerson Murphy-Hill
Operating System
Structuring
Concept
Paper by C. A. R. Hoare
Presentation by
Emerson Murphy-Hill
The Problem
An OSs main task is to control
access to a machines resources
(data, devices, time, space)
If resources are not tightly
controlled, chaos will ensue
- race conditions
The Solution
Monitors provide control by
allowing only one process to
access a critical resource at a
time
A class/module/package
Contains procedures and data
An Abstract Monitor
name : monitor
some local declarations
initialize local data
procedure name(arguments)
do some work
other procedures
Monitor Rules
Any process can access any
monitor procedure at any time
Only one process may enter a
monitor procedure
No process may directly access a
monitors local variables
A monitor may only access its
local variables
Things Needed to Enforce Monitor
wait operation
Forces running process to sleep
signal operation
Wakes up a sleeping process
A condition
Something to store whos waiting
for a particular reason
Implemented as a queue
A Running Example Emersons Kitchen
procedure exitKitchen
occupied = false;
nonOccupied.signal; Procedure
Multiple Conditions
Sometimes desirable to be able to
wait on multiple things
Can be implemented with multiple
conditions
Example: Two reasons to enter
kitchen - cook (remove clean dishes)
or clean (add clean dishes)
Two reasons to wait:
Going to cook, but no clean dishes
Going to clean, no dirty dishes
Emersons Kitchen
kitchen : monitor
procedure cook
if dishes.isEmpty then cleanDishes.wait
sink.push ( dishes.pop );
dirtyDishes.signal;
procedure cleanDish
if sink.isEmpty then dirtyDishes.wait
dishes.push (sink.pop)
cleanDishes.signal
Condition Queue
Checking if any process is waiting
on a condition:
condition.queue returns true if a
process is waiting on condition
Example: Doing dishes only if
someone is waiting for them
Emersons Kitchen
kitchen : monitor
procedure cook
if dishes.isEmpty then cleanDishes.wait
sink.push ( dishes.pop );
dirtyDishes.signal;
procedure cleanDishes
if sink.isEmpty then dirtyDishes.wait
if cleanDishes.queue
dishes.push (sink.pop);
cleanDishes.signal;
Scheduled Waits
Gives a waiting thread a number
Lowest number gets woken up
first (inverse priority)
Example: People who want to
cook are prioritized:
Highest: Me!
Medium: Family
Lowest: Vagrants/Friends
Emersons Kitchen
kitchen : monitor
procedure cleanDishes
if sink.isEmpty then dirtyDishes.wait;
while(cleanDishes.queue)
dishes.push (sink.pop);
cleanDishes.signal;
Summary
Advantages
Data access synchronization simplified
(vs. semaphores or locks)
Better encapsulation
Disadvantages:
Deadlock still possible (in monitor code)
Programmer can still botch use of
monitors
No provision for information exchange
between machines
Other Issues Discussed
Power of Monitors
SemaphoreMonitor
MonitorSemaphore
Proof Rules
I (b.wait) I & B
I & B (b.signal) I
OS Examples
Bounded Buffer
Alarm clock
Buffer allocation
Disk-head Scheduler
Reader/Writer
Mutual Exclusion:
Implementation
Disabling Interrupts Mutex Insertion
kitchen : monitor kitchen : monitor