/
CS 140 Lecture Notes: Concurrency CS 140 Lecture Notes: Concurrency

CS 140 Lecture Notes: Concurrency - PowerPoint Presentation

bikersjoker
bikersjoker . @bikersjoker
Follow
342 views
Uploaded On 2020-07-03

CS 140 Lecture Notes: Concurrency - PPT Presentation

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

lock amp head tail amp lock tail head count size char buffer release acquire int put struct locks init

Share:

Link:

Embed:

Download Presentation from below link

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.


Presentation Transcript

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);

Slide2

CS 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;

}

Slide3

CS 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');

Slide4

CS 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');

Slide5

CS 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'

Slide6

CS 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'

Slide7

CS 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');

Slide8

CS 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;}

Slide9

CS 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

Slide10

CS 140 Lecture Notes: Locks

Slide 10