DOC PREVIEW
Stanford CS 140 - Lecture Notes

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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(&notEmpty);condition_init(&notFull); void put(char c) { lock_acquire(&l); while (count == SIZE) { condition_wait(&notFull, &l); } count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } condition_signal(&notEmpty, &l); lock_release(&l);}char get() { char c; lock_acquire(&l); while (count == 0) { condition_wait(&notEmpty, &l); } count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } condition_signal(&notFull, &l); lock_release(&l); return c;}CS 140 Lecture Notes: Locks Slide


View Full Document

Stanford CS 140 - Lecture Notes

Documents in this Course
Homework

Homework

25 pages

Notes

Notes

8 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?