Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Producer-Consumer Problem OR, The Bounded-Buffer Problem

Similar presentations


Presentation on theme: "The Producer-Consumer Problem OR, The Bounded-Buffer Problem"— Presentation transcript:

1 The Producer-Consumer Problem OR, The Bounded-Buffer Problem

2 count N 4 buffer Initial State while (TRUE) {
N 4 buffer while (TRUE) { int item = produce_item(); if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

3 count N 4 buffer First, the producer… while (TRUE) {
N 4 buffer while (TRUE) { int item = produce_item(); if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

4 count N 4 buffer ■ while (TRUE) { int item = produce_item();
N 4 buffer while (TRUE) { int item = produce_item(); if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

5 count N 4 buffer ■ while (TRUE) { int item = produce_item();
N 4 buffer while (TRUE) { int item = produce_item(); if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

6 count N 4 buffer ■ while (TRUE) { int item = produce_item();
N 4 buffer while (TRUE) { int item = produce_item(); if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

7 count 1 N 4 buffer ■ while (TRUE) { int item = produce_item();
if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

8 count 1 N 4 buffer ■ while (TRUE) { int item = produce_item();
if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

9 count 1 N 4 buffer ■ while (TRUE) { int item = produce_item();
if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

10 count 1 N 4 buffer ■ while (TRUE) { int item = produce_item();
if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

11 count 1 N 4 buffer ■ while (TRUE) { int item = produce_item();
if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

12 Similarly…

13 count 4 N 4 buffer ■ ■ ■ ■ ■ while (TRUE) { int item = produce_item();
if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

14 count 4 N 4 buffer ■ ■ ■ ■ ■ while (TRUE) { int item = produce_item();
if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

15 count 4 N 4 buffer ■ ■ ■ ■ ■ Now, the consumer… while (TRUE) {
int item = produce_item(); if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

16 count 4 N 4 buffer ■ ■ ■ ■ ■ while (TRUE) { int item = produce_item();
if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

17 count 3 N 4 buffer ■ ■ ■ ■ ■ while (TRUE) { int item = produce_item();
if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

18 count 3 N 4 buffer ■ ■ ■ ■ ■ while (TRUE) { int item = produce_item();
if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

19 count 3 N 4 buffer ■ ■ ■ ■ ■ while (TRUE) { int item = produce_item();
if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

20 count 3 N 4 buffer ■ ■ ■ ■ while (TRUE) { int item = produce_item();
if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer

21 Similarly…

22 count N 4 buffer while (TRUE) { int item = produce_item();
N 4 buffer while (TRUE) { int item = produce_item(); if (count == N) { sleep(); } insert_item(item); count = count + 1; if (count == 1) { wakeup(consumer); while (TRUE) { if (count == 0) { sleep(); } int item = remove_item(); count = count - 1; if (count == N - 1) { wakeup(producer); process_item(item); Producer Consumer


Download ppt "The Producer-Consumer Problem OR, The Bounded-Buffer Problem"

Similar presentations


Ads by Google