Title: Semaphores
1Semaphores
- The producer-consumer problem using semaphores
2Producer/Consumer
Producer down(empty) down(mutex) insert_item(i
tem) up(mutex) up(full)
N 6 Semaphore mutex 1 Semaphore full
0 Semaphore empty N
producer1
consumer1
consumer2
producer2
Run queue
Mutex queue
Full queue
Empty queue
3Producer/Consumer
Producer down(empty) down(mutex) insert_item(i
tem) up(mutex) up(full)
N 6 Semaphore mutex 1 Semaphore full
0 Semaphore empty 5
down (empty)
producer1
consumer1
producer2
consumer2
Run queue
Mutex queue
Full queue
Empty queue
4Producer/Consumer
Producer down(empty) down(mutex) insert_item(i
tem) up(mutex) up(full)
N 6 Semaphore mutex 0 Semaphore full
0 Semaphore empty 5
down (mutex)
producer1
consumer1
producer2
consumer2
Run queue
Mutex queue
Full queue
Empty queue
5Producer/Consumer
Producer down(empty) down(mutex) insert_item(i
tem) up(mutex) up(full)
N 6 Semaphore mutex 0 Semaphore full
0 Semaphore empty 5
insert_item(item)
producer1
consumer1
producer2
consumer2
Run queue
Mutex queue
X
Full queue
Empty queue
6Producer/Consumer
N 6 Semaphore mutex 0 Semaphore full
0 Semaphore empty 5
producer1
consumer1
producer2
consumer2
Run queue
Quantum ends
Mutex queue
X
Full queue
Empty queue
7Producer/Consumer
Consumer down(full) down(mutex) consume_item()
up(mutex) up(empty)
N 6 Semaphore mutex 0 Semaphore full
0 Semaphore empty 5
down (full)
producer2
consumer2
producer1
Run queue
Mutex queue
X
consumer1
Full queue
Empty queue
8Producer/Consumer
N 6 Semaphore mutex 0 Semaphore full
0 Semaphore empty 5
producer2
consumer2
producer1
Run queue
Mutex queue
X
consumer1
Full queue
Empty queue
9Producer/Consumer
Producer down(empty) down(mutex) insert_item(i
tem) up(mutex) up(full)
N 6 Semaphore mutex 0 Semaphore full
0 Semaphore empty 4
down (empty)
producer2
consumer2
producer1
Run queue
Mutex queue
X
consumer1
Full queue
Empty queue
10Producer/Consumer
Producer down(empty) down(mutex) insert_item(i
tem) up(mutex) up(full)
N 6 Semaphore mutex 0 Semaphore full
0 Semaphore empty 4
down (mutex)
consumer2
producer1
Run queue
producer2
Mutex queue
X
consumer1
Full queue
Empty queue
11Producer/Consumer
N 6 Semaphore mutex 0 Semaphore full
0 Semaphore empty 4
consumer2
producer1
Run queue
producer2
Mutex queue
X
consumer1
Full queue
Empty queue
12Producer/Consumer
Consumer down(full) down(mutex) consume_item()
up(mutex) up(empty)
N 6 Semaphore mutex 0 Semaphore full
0 Semaphore empty 4
down (full)
producer1
Run queue
producer2
Mutex queue
X
consumer1
consumer2
Full queue
Empty queue
13Producer/Consumer
N 6 Semaphore mutex 0 Semaphore full
0 Semaphore empty 4
producer1
Run queue
producer2
Mutex queue
X
consumer1
consumer2
Full queue
Empty queue
14Producer/Consumer
Producer down(empty) down(mutex) insert_item(i
tem) up(mutex) up(full)
N 6 Semaphore mutex 1 Semaphore full
0 Semaphore empty 4
up (mutex)
producer2
producer1
Run queue
Mutex queue
X
consumer1
consumer2
Full queue
Empty queue
15Producer/Consumer
Producer down(empty) down(mutex) insert_item(i
tem) up(mutex) up(full)
N 6 Semaphore mutex 1 Semaphore full
1 Semaphore empty 4
up (full)
consumer1
producer2
producer1
Run queue
Mutex queue
X
Full queue
consumer2
Empty queue
16Producer/Consumer
N 6 Semaphore mutex 1 Semaphore full
1 Semaphore empty 4
consumer1
producer2
producer1
Run queue
Quantum ends
Mutex queue
X
Full queue
consumer2
Empty queue
17Producer/Consumer
N 6 Semaphore mutex 0 Semaphore full
1 Semaphore empty 4
down (mutex)
consumer1
producer2
producer1
Run queue
Mutex queue
X
Full queue
consumer2
Empty queue
18Producer/Consumer
N 6 Semaphore mutex 0 Semaphore full
1 Semaphore empty 4
insert_item (item)
consumer1
producer2
producer1
Run queue
Mutex queue
X
Full queue
consumer2
X
Empty queue
19Producer/Consumer
N 6 Semaphore mutex 1 Semaphore full
1 Semaphore empty 4
up (mutex)
consumer1
producer2
producer1
Run queue
Mutex queue
X
Full queue
consumer2
X
Empty queue
20Producer/Consumer
N 6 Semaphore mutex 1 Semaphore full
2 Semaphore empty 4
up (full)
consumer1
producer2
consumer2
producer1
Run queue
Mutex queue
X
Full queue
X
Empty queue
21Producer/Consumer
N 6 Semaphore mutex 1 Semaphore full
2 Semaphore empty 4
consumer1
producer2
consumer2
producer1
Run queue
Quantum ends
Mutex queue
X
Full queue
X
Empty queue
22Producer/Consumer
N 6 Semaphore mutex 1 Semaphore full
1 Semaphore empty 4
down (full)
consumer1
producer2
consumer2
producer1
Run queue
Mutex queue
X
Full queue
X
Empty queue
23Producer/Consumer
N 6 Semaphore mutex 0 Semaphore full
1 Semaphore empty 4
down (mutex)
consumer1
producer2
consumer2
producer1
Run queue
Mutex queue
X
Full queue
X
Empty queue
24Producer/Consumer
N 6 Semaphore mutex 0 Semaphore full
1 Semaphore empty 4
consume_item()
consumer1
producer2
consumer2
producer1
Run queue
Mutex queue
X
Full queue
Empty queue
25Producer/Consumer
N 6 Semaphore mutex 1 Semaphore full
1 Semaphore empty 4
up(mutex)
consumer1
producer2
consumer2
producer1
Run queue
Mutex queue
X
Full queue
Empty queue
26Producer/Consumer
N 6 Semaphore mutex 1 Semaphore full
1 Semaphore empty 5
up(empty)
consumer1
producer2
consumer2
producer1
Run queue
Mutex queue
X
Full queue
Empty queue