too much milk with locks n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Too Much Milk With Locks PowerPoint Presentation
Download Presentation
Too Much Milk With Locks

Loading in 2 Seconds...

play fullscreen
1 / 5

Too Much Milk With Locks - PowerPoint PPT Presentation


  • 100 Views
  • Uploaded on

Both threads: struct lock l; ... l ock_acquire (&l); if (milk == 0) { buy_milk (); } lock_release (& l);. Too Much Milk With Locks. char buffer[SIZE]; int count = 0; int putIndex = 0, getIndex = 0; struct lock l; lock_init (&l); void put(char c) { lock_acquire (&l); count++;

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Too Much Milk With Locks' - taite


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
too much milk with locks
Both threads:

struct lock l;

...

lock_acquire(&l);

if (milk == 0) {

buy_milk();

}

lock_release(&l);

Too Much Milk With Locks

CS 140 Lecture Notes: Concurrency

producer consumer v1
char buffer[SIZE];

int count = 0;

intputIndex= 0, getIndex= 0;

struct lock l;

lock_init(&l);

void put(char c) {

lock_acquire(&l);

count++;

buffer[putIndex] = c;

putIndex++;

if (putIndex== SIZE) {

putIndex= 0;

}

lock_release(&l);

}

char get() {

char c;

lock_acquire(&l);

count--;

c = buffer[getIndex];

getIndex++;

if (getIndex== SIZE) {

getIndex= 0;

}

lock_release(&l);

return c;

}

Producer/Consumer, v1

CS 140 Lecture Notes: Locks

producer consumer v2
char buffer[SIZE];

int count = 0;

intputIndex= 0, getIndex= 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[putIndex] = c;

putIndex++;

if (putIndex== SIZE) {

putIndex= 0;

}

lock_release(&l);

}

char get() {

char c;

lock_acquire(&l);

while (count == 0) {

lock_release(&l);

lock_acquire(&l);

}

count--;

c = buffer[getIndex];

getIndex++;

if (getIndex== SIZE) {

getIndex= 0;

}

lock_release(&l);

return c;

}

Producer/Consumer v2

CS 140 Lecture Notes: Locks

producer consumer v3
char buffer[SIZE];

int count = 0;

intputIndex= 0, getIndex= 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[putIndex] = c;

putIndex++;

if (putIndex== SIZE) {

putIndex= 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[getIndex];

getIndex++;

if (getIndex== SIZE) {

getIndex= 0;

}

cond_signal(&spaceAvailable, &l);

lock_release(&l);

return c;

}

Producer/Consumer v3

T1

T2

T3

CS 140 Lecture Notes: Locks