Professional Documents
Culture Documents
Lecture 25
Lecture 25
Operating System Concepts – 9th Edition 5.1 Silberschatz, Galvin and Gagne
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 (…) { …. }
Operating System Concepts – 9th Edition 5.2 Silberschatz, Galvin and Gagne
Schematic view of a Monitor
Operating System Concepts – 9th Edition 5.3 Silberschatz, Galvin and Gagne
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
Operating System Concepts – 9th Edition 5.4 Silberschatz, Galvin and Gagne
Monitor with Condition Variables
Operating System Concepts – 9th Edition 5.5 Silberschatz, Galvin and Gagne
Condition Variables Choices
If process P invokes x.signal(), and process Q is
suspended in x.wait(), what should happen next?
Both Q and P cannot execute in parallel. 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
Operating System Concepts – 9th Edition 5.6 Silberschatz, Galvin and Gagne
Monitor Solution to Dining Philosophers
monitor DiningPhilosophers
{
enum { THINKING, HUNGRY, EATING } state [5] ;
condition self [5];
Operating System Concepts – 9th Edition 5.7 Silberschatz, Galvin and Gagne
Solution to Dining Philosophers (Cont.)
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
Operating System Concepts – 9th Edition 5.8 Silberschatz, Galvin and Gagne
Solution to Dining Philosophers (Cont.)
DiningPhilosophers.pickup(i);
EAT
DiningPhilosophers.putdown(i);
Operating System Concepts – 9th Edition 5.9 Silberschatz, Galvin and Gagne