140 likes | 160 Views
CSI 400/500 Operating Systems Spring 2009. Lecture #16 – Synchronization with Monitors Wednesday, April 1 st. Multiple Synchronization. In modern operating systems, processes may share a set of resources. Each resource requires a semaphore Overhead gets overwhelming. AND Synchronization.
E N D
CSI 400/500 Operating Systems Spring 2009 Lecture #16 – Synchronization with Monitors Wednesday, April 1st
Multiple Synchronization • In modern operating systems, processes may share a set of resources. • Each resource requires a semaphore • Overhead gets overwhelming
AND Synchronization • Early attempts used a Wait function with multiple parameters • Obtained all resources at once • Required 2nd process to enqueue all resources
Event • Discrete operating system entity • Has unique id and descriptor like thread • Performs operating system activities • Accessed via function calls
Synchronization Event • Used for multiple resource synchronization • 3 functions: • Wait() • Signal() • Queue()
Monitors • Self-contained multiple resource synchronization event • Object containing storage, procedures, interface, and queues • Only one process can “enter” monitor at a time • Can access any data or function in monitor
Condition Variables • A variable on a crucial data area • Allows a process or thread to relinquish the monitor if it requires a resource elsewhere • Conditional variable has a queue
Sample Monitor Dataarea Library of functions Conditional variable queues
Interprocess Communication • Used by monitors to communicate among threads on their conditional variable queues • Necessary if data changes
IPC types • Pipes • Traditional UNIX method • FIFO buffer at kernel level • Write() and read() • Message Passing • Used by most OS and used by monitors • Monitor contains mailbox • Send() and receive()
Example #1 – Traffic signals • Think of one-lane crossing • Only allows one direction flow • Light changes if all cars pass or a timer goes off • Lights are traffic controlled, so cars don’t wait if no traffic in other direction
Monitor – Example #1 • Enter monitor – one arrival() for each direction • Depart(direction) • Internal function switch() • Data is count of cars in each direction, RED/GREEN signals in each direction, and timer • Code seen on p 350
Example #2 – Dining Philosophers • 5 place settings, bowl of spaghetti in middle • One fork per place setting • Philosopher requires two forks to eat
Monitor – Example #2 • Each philosopher has state: thinking, hungry, eating • Counter of forks in action • PickupFork(p) – Philosopher p picks up fork • If 1st, sets state to hungry • If 2nd, sets state to eating • Finish(p) – releases fork and sets state to thinking • Code on pg 351