Вы находитесь на странице: 1из 36

Classical Synchronization Problems

This lecture

Goals:

Introduce classical synchronization problems Producer-Consumer Problem Reader-Writer Problem Dining Philosophers Problem

Topics

I. Producer-Consumer
Imagine a chef cooking items and putting them onto a conveyor belt

Producer-Consumer
Imagine a chef cooking items and putting them onto a conveyor belt

Now imagine many such chefs!

Producer-Consumer
Imagine a chef cooking items and putting them onto a conveyor belt

Now imagine many such chefs!

Producer-Consumer
Imagine a chef cooking items and putting them onto a conveyor belt

Now imagine many such chefs!


Now imagine a customer picking items off the conveyor belt

Producer-Consumer
Imagine a chef cooking items and putting them onto a conveyor belt Now imagine many such chefs! Now imagine many such customers picking items off the conveyor belt!

Producer-Consumer
Imagine a chef cooking items and putting them onto a conveyor belt

Now imagine many such chefs!


Now imagine many such customers picking items off the conveyor belt!

Producer-Consumer
Chef Customer = Producer = Consumer

insertPtr

removePtr

Producer-Consumer
Chef Customer = Producer = Consumer

insertPtr

removePtr

10

Producer-Consumer
Chef Customer insertPtr = Producer = Consumer

removePtr

11

Producer-Consumer
Chef Customer = Producer = Consumer

insertPtr

removePtr

12

Producer-Consumer
Chef Customer = Producer = Consumer

insertPtr

removePtr
13

Producer-Consumer
Chef Customer = Producer = Consumer

insertPtr

removePtr
14

Producer-Consumer
Chef Customer = Producer = Consumer

insertPtr

removePtr
15

Producer-Consumer
Chef Customer = Producer = Consumer BUFFER FULL: Producer must be blocked!

insertPtr removePtr
16

Producer-Consumer
Chef Customer = Producer = Consumer

removePtr

insertPtr

17

Producer-Consumer
Chef Customer = Producer = Consumer

removePtr insertPtr

18

Producer-Consumer
Chef Customer = Producer = Consumer

removePtr

insertPtr

19

Producer-Consumer
Chef Customer removePtr = Producer = Consumer

insertPtr

20

Producer-Consumer
Chef Customer = Producer = Consumer removePtr

insertPtr

21

Producer-Consumer
Chef Customer = Producer = Consumer

removePtr

insertPtr

22

Producer-Consumer
Chef Customer = Producer = Consumer

removePtr

insertPtr

23

Producer-Consumer
Chef Customer = Producer = Consumer BUFFER EMPTY: Consumer must be blocked!

removePtr insertPtr

24

Producer-Consumer Problem
Chef Customer = Producer = Consumer

Producers insert items Consumers remove items Shared bounded buffer *

* Efficient implementation is a circular buffer with an insert and a removal pointer.


25

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 producers and consumers
26

Challenges

Prevent buffer overflow Prevent buffer underflow Proper synchronization

27

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.


28

II. Reader-Writer Problem


A reader: read data A writer: write data Rules:


Multiple readers may read the data simultaneously Only one writer can write the data at any time A reader and a writer cannot access data simultaneously

Locking table: whether any two can be in the critical section simultaneously

Reader Writer
Reader Writer OK No No No

29

Reader-Writer Solution

Does it work? Why? Work through an example. Ask Systematic questions, and look at What if ? scenarios Play the devils advocate!

Semaphore mutex, wrt; // shared and initialized to 1; int readcount; // shared and initialized to 0
// Writer // Reader wait(mutex); readcount:=readcount+1; if readcount == 1 then wait(wrt); signal(mutex); .... reading performed wait(mutex); readcount:=readcount-1; if readcount == 0 then signal(wrt); signal(mutex);
30

wait(wrt); ...... writing performed .....

signal(wrt);

III. Dining Philosophers: an intellectual game


0

N philosophers and N forks Philosophers eat/think Eating needs 2 forks Pick one fork at a time

1 1

0 4

3 N=5

31

Does this solve the Dining Philosophers Problem?

A non-solution to the dining philosophers problem Deadlock: everyone picks up their left fork first, then waits for right fork => Starvation!

32

Dining Philosophers Solution

(s[n] inited to 0s)

33

Dining Philosophers Solution

Can prove that this solution is deadlock-free and starvation-free Try the devils advocate approach to see if this really works

34

More What if?s

Made picking up both left and right chopsticks an atomic operation?


or,

N-1 Philosophers but N chopsticks?

35

More What if?s

Made picking up left and right chopsticks an atomic operation?


or,

N-1 Philosophers but N chopsticks?


both changes prevent deadlock.
36

Вам также может понравиться