Producer/Consumer v1Slide 2Slide 3Slide 4Slide 5Slide 6Producer/Consumer v2Producer/Consumer v3Slide 9CS 140 Lecture Notes: Locks Slide 1Producer/Consumer v1char buffer[SIZE];int count = 0;int head = 0, tail = 0;struct lock l;lock_init(&l); void put(char c) { lock_acquire(&l); count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } lock_release(&l);}char get() { char c; lock_acquire(&l); count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } lock_release(&l); return c;}CS 140 Lecture Notes: Locks Slide 2Producer/Consumer v1char buffer[SIZE];int count = 0;int head = 0, tail = 0;struct lock l;lock_init(&l); void put(char c) { lock_acquire(&l); count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } lock_release(&l);}char get() { char c; lock_acquire(&l); count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } lock_release(&l); return c;}count: 0head: 0tail: 0SIZE: 8acount: 1head: 1tail: 0SIZE: 8put('a');CS 140 Lecture Notes: Locks Slide 3Producer/Consumer v1char buffer[SIZE];int count = 0;int head = 0, tail = 0;struct lock l;lock_init(&l); void put(char c) { lock_acquire(&l); count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } lock_release(&l);}char get() { char c; lock_acquire(&l); count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } lock_release(&l); return c;}acount: 0head: 0tail: 0SIZE: 8a b ccount: 3head: 3tail: 0SIZE: 8put('b');put('c');CS 140 Lecture Notes: Locks Slide 4Producer/Consumer v1char buffer[SIZE];int count = 0;int head = 0, tail = 0;struct lock l;lock_init(&l); void put(char c) { lock_acquire(&l); count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } lock_release(&l);}char get() { char c; lock_acquire(&l); count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } lock_release(&l); return c;}a b ccount: 3head: 3tail: 0SIZE: 8a b ccount: 2head: 3tail: 1SIZE: 8get() => 'a'CS 140 Lecture Notes: Locks Slide 5Producer/Consumer v1char buffer[SIZE];int count = 0;int head = 0, tail = 0;struct lock l;lock_init(&l); void put(char c) { lock_acquire(&l); count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } lock_release(&l);}char get() { char c; lock_acquire(&l); count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } lock_release(&l); return c;}a b ccount: 2head: 3tail: 1SIZE: 8a b ccount: 1head: 3tail: 2SIZE: 8get() => 'b'CS 140 Lecture Notes: Locks Slide 6Producer/Consumer v1char buffer[SIZE];int count = 0;int head = 0, tail = 0;struct lock l;lock_init(&l); void put(char c) { lock_acquire(&l); count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } lock_release(&l);}char get() { char c; lock_acquire(&l); count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } lock_release(&l); return c;}a b c d e f gcount: 5head: 7tail: 2SIZE: 8i b c d e f g hcount: 7head: 1tail: 2SIZE: 8put('h');put('i');CS 140 Lecture Notes: Locks Slide 7Producer/Consumer v2char buffer[SIZE];int count = 0;int head = 0, tail = 0;struct lock l;lock_init(&l); void put(char c) { lock_acquire(&l); while (count == SIZE) { lock_release(&l); lock_acquire(&l); } count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } lock_release(&l);}char get() { char c; lock_acquire(&l); while (count == 0) { lock_release(&l); lock_acquire(&l); } count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } lock_release(&l); return c;}CS 140 Lecture Notes: Locks Slide 8Producer/Consumer v3char buffer[SIZE];int count = 0;int head = 0, tail = 0;struct lock l;struct condition notEmpty;struct condition notFull;lock_init(&l);condition_init(¬Empty);condition_init(¬Full); void put(char c) { lock_acquire(&l); while (count == SIZE) { condition_wait(¬Full, &l); } count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } condition_signal(¬Empty, &l); lock_release(&l);}char get() { char c; lock_acquire(&l); while (count == 0) { condition_wait(¬Empty, &l); } count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } condition_signal(¬Full, &l); lock_release(&l); return c;}CS 140 Lecture Notes: Locks Slide
View Full Document