synchroniza on review
play

Synchroniza+on Review CS 4410, Opera+ng Systems Fall 2016 Cornell - PowerPoint PPT Presentation

Synchroniza+on Review CS 4410, Opera+ng Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 5&6 in OSPP textbook The slides are the product of many rounds of teaching CS 4410 by Professors Sirer, Bracy, Agarwal,


  1. Synchroniza+on Review CS 4410, Opera+ng Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 5&6 in OSPP textbook The slides are the product of many rounds of teaching CS 4410 by Professors Sirer, Bracy, Agarwal, George, and Van Renesse.

  2. Synchroniza+on: Topic 1 Synchroniza+on Mo+va+on & Basics • Race Condi+ons • Cri+cal Sec+ons • Example: Too Much Milk • Basic Hardware Primi+ves • Building a SpinLock 2

  3. Synchroniza+on: Topic 2 Semaphores • Defini+on • Binary Semaphores • Coun+ng Semaphores • Implemen+ng Semaphores • Classic Synchroniza+on Problems (w/Semaphores) • Producer-Consumer (w/ a bounded buffer) • Readers/Writers Problems • Classic Semaphore Mistakes • Semaphores Considered Harmful 3

  4. Synchroniza+on: Topic 3 Monitors & Condi,on Variables • Defini+on • Seman+cs • Simple Monitor Example • vs. Semaphores • Classic Synchroniza+on Problems (w/Monitors) • Bounded Buffer Producer-Consumer • Readers/Writers Problems • Classic Synchroniza+on Mistakes (w/Monitors) 4

  5. [Hoare 1974] What is a Monitor? ADT for shared resources: Monitor stack 1. Shared Private Data { int top; • the resource • accessed only here void push(any_t *) { } 2. Procedures any_t *pop() { • to access resource } • only act on data local to initialization_code() { } the monitor } 3. Synchroniza+on primi+ves • among threads that access the procedures 5

  6. Monitors can define Condi+on Variables A mechanism to wait for events 3 opera+ons on Condi+on Variable Condition x; • x.wait() : release monitor lock, relinquish processor, sleep until woken up (or wake up on your own ), reacquire on return • x.signal() : wake at least one process waiting on condition (if there is one). No history associated with signal. • x.broadcast() : wake all processes wai+ng on condition (useful for resource manager) You must hold the monitor lock to call these operations. 6

  7. Types of Wait Queues Monitors have two kinds of “wait” queues • Entry to the monitor: has a queue of threads wai+ng to obtain mutual exclusion & enter • Condi,on variables: each condi+on variable has a queue of threads wai+ng on the associated condi+on 7

  8. Monitors in Python Do not forget: class RWlock: def __init__(self): • One lock for the monitor self.lock = Lock() self.canRead = Condition(self.lock) • CVs ini,alized with the lock self.canWrite = Condition(self.lock) • counters ini,alized self.nReaders = 0 self.nWriters = 0 • who waits? who no,fies? self.nWaitingReaders = 0 self.nWaitingWriters = 0 • post-wait updates • no,fy vs. no,fyAll def begin_read(self): with self.lock: self.nWaitingReaders += 1 while self.nWriters > 0 or self.nWaitingWriters > 0: self.canRead. wait() self.nWaitingReaders -= 1 self.nActiveReaders += 1 def end_read(self): with self.lock: self.nReaders -= 1 if self.nReaders == 0 and self.nWaitingWriters > 0: self.canWrite.notify() 8

  9. Barbershop Problem One possible version: • A barbershop holds up to k clients • N barbers work on clients • M clients total want their hair cut • Each client will have their hair cut by the first barber available 9

  10. Barbershop Problem Another possible version: • Barbershop has an exit door • customer cannot leave un+l door is open • barber cannot take on a new client un+l customer has lee & closed the door • Customer takes a seat only when a barber ready • Barber cut hair only when customer is seated 10

  11. More Specs Need to implement three monitor func+ons: getHaircut: • called by client • returns when haircut is done getClient: • called by barber to serve a customer letClientLeave: • called by barber to let a customer out of the barbershop 11

  12. ImplemenRng the Barbershop (1) Iden+fy the waits Customer wait: • un+l barber is available • un+l barber opens exit door Barber waits: • un+l customer sits in a chair • un+l customer leaves (2) Create condi+on variables for each (3) Create counters to trigger the wai+ng (4) Create signals for the waits 12

  13. ImplemenRng the Barbershop (1) Iden+fy the waits (2) Create condi+on variables for each • waitForBarber • waitForDoor • waitForClient • waitForClientToLeave (3) Create counters to trigger the wai+ng (4) Create signals for the waits 13

  14. ImplemenRng the Barbershop (1) Iden+fy the waits (2) Create condi+on variables for each (3) Create counters to trigger the wai+ng • nSeatedCustomers • nBarbersAvail • nDoorsOpened (4) Create signals for the waits 14

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend