CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday,...
-
date post
22-Dec-2015 -
Category
Documents
-
view
217 -
download
0
Transcript of CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday,...
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
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
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
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
55
Synchronization EventSynchronization Event
Used for multiple resource Used for multiple resource synchronizationsynchronization
3 functions:3 functions:• Wait()Wait()• Signal()Signal()• Queue()Queue()
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
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
88
Sample MonitorSample Monitor
Data area
Conditional variable queues
Library of functions
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
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()
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
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
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
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