Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Synchronization.

Similar presentations


Presentation on theme: "Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Synchronization."— Presentation transcript:

1 Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Synchronization

2 Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 CS241 Administrative This week SMP4 is due today HW1 will be out next (practice for midterm)

3 Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 This lecture Goals: Introduce classic synchronization problems Topics Producer-Consumer Dining Philosophers

4 Copyright ©: Nahrstedt, Angrave, Abdelzaher 4 Producer-Consumer

5 Copyright ©: Nahrstedt, Angrave, Abdelzaher 5 Producer-Consumer

6 Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 Producer-Consumer

7 Copyright ©: Nahrstedt, Angrave, Abdelzaher 7 Producer-Consumer

8 Copyright ©: Nahrstedt, Angrave, Abdelzaher 8 Producer-Consumer

9 Copyright ©: Nahrstedt, Angrave, Abdelzaher 9 Producer-Consumer

10 Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 Producer-Consumer insertPtr removePtr

11 Copyright ©: Nahrstedt, Angrave, Abdelzaher 11 Producer-Consumer insertPtr removePtr

12 Copyright ©: Nahrstedt, Angrave, Abdelzaher 12 Producer-Consumer insertPtr removePtr

13 Copyright ©: Nahrstedt, Angrave, Abdelzaher 13 Producer-Consumer insertPtr removePtr

14 Copyright ©: Nahrstedt, Angrave, Abdelzaher 14 Producer-Consumer insertPtr removePtr

15 Copyright ©: Nahrstedt, Angrave, Abdelzaher 15 Producer-Consumer insertPtr removePtr

16 Copyright ©: Nahrstedt, Angrave, Abdelzaher 16 Producer-Consumer insertPtr removePtr

17 Copyright ©: Nahrstedt, Angrave, Abdelzaher 17 Producer-Consumer insertPtr removePtr BUFFER FULL: Producer must be blocked!

18 Copyright ©: Nahrstedt, Angrave, Abdelzaher 18 Producer-Consumer insertPtr removePtr

19 Copyright ©: Nahrstedt, Angrave, Abdelzaher 19 Producer-Consumer insertPtr removePtr

20 Copyright ©: Nahrstedt, Angrave, Abdelzaher 20 Producer-Consumer insertPtr removePtr

21 Copyright ©: Nahrstedt, Angrave, Abdelzaher 21 Producer-Consumer insertPtr removePtr

22 Copyright ©: Nahrstedt, Angrave, Abdelzaher 22 Producer-Consumer insertPtr removePtr

23 Copyright ©: Nahrstedt, Angrave, Abdelzaher 23 Producer-Consumer insertPtr removePtr

24 Copyright ©: Nahrstedt, Angrave, Abdelzaher 24 Producer-Consumer insertPtr removePtr

25 Copyright ©: Nahrstedt, Angrave, Abdelzaher 25 Producer-Consumer insertPtr removePtr BUFFER EMPTY: Consumer must be blocked!

26 Copyright ©: Nahrstedt, Angrave, Abdelzaher 26 Producer-Consumer Problem Producers insert items Consumers remove items Shared bounded buffer * * Efficient implementation is a circular buffer with an insert and a removal pointer.

27 Copyright ©: Nahrstedt, Angrave, Abdelzaher 27 Producer-Consumer Problem Producer inserts items. Updates insertion pointer. Consumer executes destructive reads on the buffer. Updates removal pointer Both update information about how full/empty the buffer is. Solution should allow multiple readers/writers

28 Copyright ©: Nahrstedt, Angrave, Abdelzaher 28 Challenge Prevent buffer overflow Prevent buffer underflow Proper synchronization

29 Copyright ©: Nahrstedt, Angrave, Abdelzaher 29 Solution Prevent overflow: block producer when full! Counting semaphore to count #free slots 0  block producer Prevent underflow: block consumer when empty!Counting semaphore to count #items in buffer 0  block consumer Mutex to protect accesses to shared buffer & pointers.

30 Copyright ©: Nahrstedt, Angrave, Abdelzaher 30 Assembling the solution sem_wait(slots), sem_post(slots) sem_wait(items), sem_post(items) mutex_lock(m) mutex_unlock(m) insertptr=(insertptr+1) % N removalptr=(removalptr+1) % N Initialize semaphore slots to size of buffer Initialize semaphore items to zero.

31 Copyright ©: Nahrstedt, Angrave, Abdelzaher 31 Pseudocode getItem() Error checking/EINTR handling not shown sem_wait(items) mutex_lock(mutex) result=buffer[ removePtr ]; removePtr=(removePtr +1 ) % N mutex_unlock(mutex) sem_post(slots)

32 Copyright ©: Nahrstedt, Angrave, Abdelzaher 32 Pseudocode putItem(data) Error checking/EINTR handling not shown sem_wait(slots) mutex_lock(mutex) buffer[ insertPtr]= data; insertPtr=(insertPtr + 1 ) % N mutex_unlock(mutex) sem_post(items)

33 Copyright ©: Nahrstedt, Angrave, Abdelzaher 33 Dining Philosophers

34 Copyright ©: Nahrstedt, Angrave, Abdelzaher 34 Dining Philosopher Challenge { Think | Eat } N Philosophers circular table with N chopsticks To eat the Philosopher must first pickup two chopsticks i th Philosopher needs i th & i+1 st chopstick Only put down chopstick when Philosopher has finished eating Devise a solution which satisfies mutual exclusion but avoids starvation and deadlock

35 Copyright ©: Nahrstedt, Angrave, Abdelzaher 35 Seems simple enough …? while(true) { think() lock(chopstick[i]) lock(chopstick[(i+1) % N]) eat() unlock(chopstick[(i+1) % N]) unlock(chopstick[i]) }

36 Copyright ©: Nahrstedt, Angrave, Abdelzaher 36 What if? Made picking up left and right chopsticks an atomic operation? or, N-1 Philosophers but N chopsticks?

37 Copyright ©: Nahrstedt, Angrave, Abdelzaher 37 What if? Made picking up left and right chopsticks an atomic operation? or, N-1 Philosophers but N chopsticks? … both changes prevent deadlock.

38 Copyright ©: Nahrstedt, Angrave, Abdelzaher 38 Formal Requirements for Deadlock Mutual exclusion Hold and wait condition No preemption condition Circular wait condition Original scenario & our proposed ritual had all four of these properties.

39 Copyright ©: Nahrstedt, Angrave, Abdelzaher 39 Formal Requirements for Deadlock Mutual exclusion Exclusive use of chopsticks Hold and wait condition Hold 1 chopstick, wait for next No preemption condition Cannot force another to undo their hold Circular wait condition Each waits for next neighbor to put down chopstick

40 Copyright ©: Nahrstedt, Angrave, Abdelzaher 40 Define the Input and Output of the Following time() Input Output ctime() Input Output gettimeofday() Input Output


Download ppt "Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Synchronization."

Similar presentations


Ads by Google