csi 400/500 operating systems spring 2009 lecture #16 – synchronization with monitors wednesday,...

14
CSI 400/500 CSI 400/500 Operating Systems Operating Systems Spring 2009 Spring 2009 Lecture #16 – Lecture #16 – Synchronization with Synchronization with Monitors Monitors Wednesday, April 1 Wednesday, April 1 st st

Post on 22-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday, April 1 st

CSI 400/500 Operating CSI 400/500 Operating Systems Spring 2009Systems Spring 2009

Lecture #16 – Lecture #16 – Synchronization with MonitorsSynchronization with Monitors

Wednesday, April 1Wednesday, April 1stst

Page 2: CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday, April 1 st

22

Multiple SynchronizationMultiple Synchronization

In modern operating systems, In modern operating systems, processes may share a set of processes may share a set of resources. resources.

Each resource requires a semaphoreEach resource requires a semaphore Overhead gets overwhelmingOverhead gets overwhelming

Page 3: CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday, April 1 st

33

AND SynchronizationAND Synchronization

Early attempts used a Wait function Early attempts used a Wait function with multiple parameterswith multiple parameters

Obtained all resources at onceObtained all resources at once Required 2Required 2ndnd process to enqueue all process to enqueue all

resourcesresources

Page 4: CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday, April 1 st

44

EventEvent

Discrete operating system entityDiscrete operating system entity Has unique id and descriptor like Has unique id and descriptor like

threadthread Performs operating system activitiesPerforms operating system activities Accessed via function callsAccessed via function calls

Page 5: CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday, April 1 st

55

Synchronization EventSynchronization Event

Used for multiple resource Used for multiple resource synchronizationsynchronization

3 functions:3 functions:• Wait()Wait()• Signal()Signal()• Queue()Queue()

Page 6: CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday, April 1 st

66

MonitorsMonitors

Self-contained multiple resource Self-contained multiple resource synchronization eventsynchronization event

Object containing storage, Object containing storage, procedures, interface, and queuesprocedures, interface, and queues

Only one process can “enter” Only one process can “enter” monitor at a timemonitor at a time

Can access any data or function in Can access any data or function in monitormonitor

Page 7: CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday, April 1 st

77

Condition VariablesCondition Variables

A variable on a crucial data areaA variable on a crucial data area Allows a process or thread to Allows a process or thread to

relinquish the monitor if it requires a relinquish the monitor if it requires a resource elsewhereresource elsewhere

Conditional variable has a queueConditional variable has a queue

Page 8: CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday, April 1 st

88

Sample MonitorSample Monitor

Data area

Conditional variable queues

Library of functions

Page 9: CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday, April 1 st

99

Interprocess CommunicationInterprocess Communication

Used by monitors to communicate Used by monitors to communicate among threads on their conditional among threads on their conditional variable queuesvariable queues• Necessary if data changesNecessary if data changes

Page 10: CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday, April 1 st

1010

IPC typesIPC types

PipesPipes• Traditional UNIX methodTraditional UNIX method• FIFO buffer at kernel levelFIFO buffer at kernel level• Write() and read()Write() and read()

Message PassingMessage Passing• Used by most OS and used by monitorsUsed by most OS and used by monitors• Monitor contains mailboxMonitor contains mailbox• Send() and receive()Send() and receive()

Page 11: CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday, April 1 st

1111

Example #1 – Traffic signalsExample #1 – Traffic signals

Think of one-lane crossingThink of one-lane crossing Only allows one direction flowOnly allows one direction flow Light changes if all cars pass or a Light changes if all cars pass or a

timer goes offtimer goes off Lights are traffic controlled, so cars Lights are traffic controlled, so cars

don’t wait if no traffic in other don’t wait if no traffic in other directiondirection

Page 12: CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday, April 1 st

1212

Monitor – Example #1Monitor – Example #1

Enter monitor – one arrival() for each Enter monitor – one arrival() for each directiondirection

Depart(direction)Depart(direction) Internal function switch()Internal function switch() Data is count of cars in each Data is count of cars in each

direction, RED/GREEN signals in each direction, RED/GREEN signals in each direction, and timerdirection, and timer

Code seen on p 350Code seen on p 350

Page 13: CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday, April 1 st

1313

Example #2 – Dining PhilosophersExample #2 – Dining Philosophers

5 place settings, bowl of spaghetti in 5 place settings, bowl of spaghetti in middlemiddle

One fork per place settingOne fork per place setting Philosopher requires two forks to eatPhilosopher requires two forks to eat

Page 14: CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday, April 1 st

1414

Monitor – Example #2Monitor – Example #2 Each philosopher has state: thinking, Each philosopher has state: thinking,

hungry, eatinghungry, eating Counter of forks in actionCounter of forks in action PickupFork(p) – Philosopher p picks PickupFork(p) – Philosopher p picks

up forkup fork• If 1If 1stst, sets state to hungry, sets state to hungry• If 2If 2ndnd, sets state to eating, sets state to eating

Finish(p) – releases fork and sets Finish(p) – releases fork and sets state to thinkingstate to thinking

Code on pg 351Code on pg 351