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

synchroniza on review
SMART_READER_LITE
LIVE PREVIEW

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,


slide-1
SLIDE 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.

slide-2
SLIDE 2

Synchroniza+on: Topic 1

2

Synchroniza+on Mo+va+on & Basics

  • Race Condi+ons
  • Cri+cal Sec+ons
  • Example: Too Much Milk
  • Basic Hardware Primi+ves
  • Building a SpinLock
slide-3
SLIDE 3

Synchroniza+on: Topic 2

3

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
slide-4
SLIDE 4

Synchroniza+on: Topic 3

4

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)
slide-5
SLIDE 5

What is a Monitor?

5

ADT for shared resources:

  • 1. Shared Private Data
  • the resource
  • accessed only here
  • 2. Procedures
  • to access resource
  • only act on data local to

the monitor

  • 3. Synchroniza+on primi+ves
  • among threads that access

the procedures

[Hoare 1974]

Monitor stack { int top; void push(any_t *) { } any_t *pop() { } initialization_code() { } }

slide-6
SLIDE 6

Monitors can define Condi+on Variables

6

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.

slide-7
SLIDE 7

Types of Wait Queues

7

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

slide-8
SLIDE 8

Monitors in Python

8

class RWlock: def __init__(self): self.lock = Lock() self.canRead = Condition(self.lock) self.canWrite = Condition(self.lock) self.nReaders = 0 self.nWriters = 0 self.nWaitingReaders = 0 self.nWaitingWriters = 0 def end_read(self): with self.lock: self.nReaders -= 1 if self.nReaders == 0 and self.nWaitingWriters > 0: self.canWrite.notify() 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

Do not forget:

  • One lock for the monitor
  • CVs ini,alized with the lock
  • counters ini,alized
  • who waits? who no,fies?
  • post-wait updates
  • no,fy vs. no,fyAll
slide-9
SLIDE 9

Barbershop Problem

9

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

slide-10
SLIDE 10

Barbershop Problem

10

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
slide-11
SLIDE 11

More Specs

11

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

slide-12
SLIDE 12

ImplemenRng the Barbershop

12

(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

slide-13
SLIDE 13

ImplemenRng the Barbershop

13

(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

slide-14
SLIDE 14

ImplemenRng the Barbershop

14

(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