Slide 1 Too Much Milk With Locks Both threads struct lock l l ockacquire ampl if milk 0 buyMilk lockrelease amp l CS 140 Lecture Notes Locks ID: 793840
Download The PPT/PDF document "CS 140 Lecture Notes: Concurrency" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
CS 140 Lecture Notes: Concurrency
Slide 1
Too Much Milk With Locks
Both threads:
struct
lock l;
...
l
ock_acquire
(&l);
if (milk == 0)
{
buyMilk
();
}
lock_release
(&
l);
Slide2CS 140 Lecture Notes: Locks
Slide 2
Producer/Consumer v1
char 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;
}
Slide3CS 140 Lecture Notes: Locks
Slide 3
Producer/Consumer v1
char 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: 0
head: 0
tail: 0
SIZE: 8
a
count: 1
head: 1
tail: 0
SIZE: 8
put('a');
Slide4CS 140 Lecture Notes: Locks
Slide 4
Producer/Consumer v1
char 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
count: 0
head: 0
tail: 0
SIZE: 8
a
b
c
count: 3
head: 3
tail: 0
SIZE: 8
put('b');
put('c');
Slide5CS 140 Lecture Notes: Locks
Slide 5
Producer/Consumer v1
char 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
count: 3
head: 3
tail: 0
SIZE: 8
a
b
c
count: 2
head: 3
tail: 1
SIZE: 8
get() => 'a'
Slide6CS 140 Lecture Notes: Locks
Slide 6
Producer/Consumer v1
char 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
count: 2
head: 3
tail: 1
SIZE: 8
a
b
c
count: 1
head: 3
tail: 2
SIZE: 8
get() => 'b'
Slide7CS 140 Lecture Notes: Locks
Slide 7
Producer/Consumer v1
char 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
g
count: 5
head: 7
tail: 2
SIZE: 8
i
b
c
d
e
f
g
h
count: 7
head: 1
tail: 2
SIZE: 8
put('h');
put('i');
Slide8CS 140 Lecture Notes: Locks
Slide 8
Producer/Consumer v2
char 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;}
Slide9CS 140 Lecture Notes: Locks
Slide 9
Producer/Consumer v3
char buffer[SIZE];
int
count = 0;
int
head = 0, tail = 0;struct lock l;struct condition dataAvailable;struct condition spaceAvailable;lock_init(&l);
cond_init
(&
dataAvailable
);
cond_init
(&spaceAvailable); void put(char c) {
lock_acquire(&l);
while (count == SIZE) {
cond_wait(&spaceAvailable
, &l);
} count++;
buffer[head] = c; head++;
if (head == SIZE) {
head = 0; }
cond_signal(&dataAvailable, &l);
lock_release(&l);}
char get() {
char c;
lock_acquire(&l); while (count == 0) {
cond_wait(&
dataAvailable, &l);
} count--;
c = buffer[tail]; tail++;
if (tail == SIZE) { tail = 0; } cond_signal(&
spaceAvailable, &l); lock_release
(&l);
return c;
}
T1
T2
T3
Slide10CS 140 Lecture Notes: Locks
Slide 10