1 / 5

Concurrency Management in Producer/Consumer Systems

This text discusses the use of locks and conditions in managing concurrency in Producer/Consumer systems. It presents three versions of implementation progress from basic lock usage to incorporating conditions for better synchronization. The examples show how to ensure proper data sharing and control access between producers and consumers effectively while preventing issues like race conditions. Developers can learn about the evolution of concurrency control mechanisms in the context of these systems.

elhamli
Télécharger la présentation

Concurrency Management in Producer/Consumer Systems

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Too Much Milk With Locks Both threads: struct lock l; ... lock_acquire(&l); if (milk == 0) { buy_milk(); } lock_release(&l); CS 140 Lecture Notes: Concurrency Slide 1

  2. Producer/Consumer, v1 char buffer[SIZE]; int count = 0; int head = 0, tail = 0; struct lock l; lock_init(&l); char get() { char c; lock_acquire(&l); count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } lock_release(&l); return c; } void put(char c) { lock_acquire(&l); count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } lock_release(&l); } CS 140 Lecture Notes: Locks Slide 2

  3. Producer/Consumer v2 char buffer[SIZE]; int count = 0; int head = 0, tail = 0; struct lock l; lock_init(&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; } 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); } CS 140 Lecture Notes: Locks Slide 3

  4. Producer/Consumer v3 char buffer[SIZE]; int count = 0; int head = 0, tail = 0; struct lock l; struct condition dataAvailable; struct condition spaceAvailable; 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; } 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); } T1 T2 T3 CS 140 Lecture Notes: Locks Slide 4

  5. CS 140 Lecture Notes: Locks Slide 5

More Related