Академический Документы
Профессиональный Документы
Культура Документы
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
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
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
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
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
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
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
signal(wrt);
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
A non-solution to the dining philosophers problem Deadlock: everyone picks up their left fork first, then waits for right fork => Starvation!
32
33
Can prove that this solution is deadlock-free and starvation-free Try the devils advocate approach to see if this really works
34
35