Docsity

Operating Systems: Monitors and Message Passing - Prof. Jonathan Walpole, Study notes of Operating Systems

Partial preview of the text

Download Operating Systems: Monitors and Message Passing - Prof. Jonathan Walpole and more Study notes Operating Systems in PDF only on Docsity!

CS 333

Introduction to Operating Systems

Class 6 – Monitors and Message Passing

Jonathan Walpole Computer Science

Portland State University

But first …^ ‰

Continuation of Class 5 – ClassicalSynchronization Problems

The sleeping barber problem^ ‰

Barber:^ ™

While there are people waiting for a hair cut,put one in the barber chair, and cut their hair

™

When done, move to the next customer

™

Else go to sleep, until someone comes in

‰

Customer:^ ™

If barber is asleep wake him up for a haircut

™

If someone is getting a haircut wait for thebarber to become free by sitting in a chair

™

If all chairs are all full, leave the barbershop

Designing a solution^ ‰

How will we model the barber and customers?

‰

What state variables do we need?^ ™

.. and which ones are shared?

™

…. and how will we protect them?

‰

How will the barber sleep?

‰

How will the barber wake up?

‰

How will customers wait?

‰

What problems do we need to look out for?

The readers and writers problem^ ‰

Multiple readers and writers want to access adatabase (each one is a thread)

‰

Multiple readers can proceed concurrently

‰

Writers must synchronize with readers andother writers^ ™

only one writer at a time!

™

when someone is writing, there must be no readers!

Goals:

™

Maximize concurrency.

™

Prevent starvation.

Designing a solution^ ‰

How will we model the readers and writers?

‰

What state variables do we need?^ ™

.. and which ones are shared?

™

…. and how will we protect them?

‰

How will the writers wait?

‰

How will the writers wake up?

‰

How will readers wait?

‰

How will the readers wake up?

‰

What problems do we need to look out for?

Readers and writers solution^ ‰

Does the previous solution have any problems?^ ™

is it “fair”?

™

can any threads be starved? If so, how could thisbe fixed?

™

… and how much confidence would you have in yoursolution?

Monitors

Monitors^ ‰

Related shared objects are collected together

‰

Compiler enforces encapsulation/mutual exclusion^ ™

Encapsulation:

•^

Local data variables are accessible only via themonitor’s entry procedures (like methods)

™

Mutual exclusion

•^

A monitor has an associated mutex lock

•^

Threads must acquire the monitor’s mutex lockbefore invoking one of its procedures

Monitors and condition variables^ ‰

But we need two flavors of synchronization^ ™

Mutual exclusion

•^

Only one at a time in the critical section

•^

Handled by the monitor’s mutex

™

Condition synchronization

•^

Wait until a certain condition holds

•^

Signal waiting threads when the condition holds

Monitor structures

initialization

code

“entry” methods

y

x

shared data

condition variables

monitor entry queue

List of threads

waiting to

enter the monitor

Can be called fromoutside the monitor.Only one active at

any moment.

Local to monitor

(Each has an associatedlist of waiting threads)

local methods

Monitor example for mutual exclusion

process

Producer

begin

loop

<produce

char

“c”>

BoundedBuffer.deposit(c) end

loop

end

Producer

process

Consumer

begin

loop

BoundedBuffer.remove(c) <consume

char

“c”>

end

loop

end

Consumer

monitor:

BoundedBuffer

var

buffer

:^

nextIn, nextOut

entry

deposit(c:

char)

begin

end entry

remove(var

c:

char)

begin

end

end BoundedBuffer

Monitor example with condition variables

monitor

BoundedBuffer

var

buffer

:^

array[0..n-1]

of

char

nextIn,nextOut

:^

0..n-

fullCount

:^

0..n

notEmpty,

notFull

:^

condition

entry

deposit(c:char)

entry

remove(var c:

char)

begin

begin

if

(fullCount

=^

n)

then

if

(fullCount

n)

then

wait(notFull)

wait(notEmpty)

end

if

end

if

buffer[nextIn]

c

c

buffer[nextOut]

nextIn

nextIn+

mod

n

nextOut

nextOut+

mod

n

fullCount :=

fullCount+

fullCount

fullCount-

signal(notEmpty)

signal(notFull)

end

deposit

end

remove

end BoundedBuffer

Condition variables

“Condition variables allow processes tosynchronize based on some state of themonitor variables.”