Presentation is loading. Please wait.

Presentation is loading. Please wait.

Synchronization Producer/Consumer Problem. Synchronization - PC with Semaphores2 Abstract The producer/consumer problem is a classical synchronization.

Similar presentations


Presentation on theme: "Synchronization Producer/Consumer Problem. Synchronization - PC with Semaphores2 Abstract The producer/consumer problem is a classical synchronization."— Presentation transcript:

1 Synchronization Producer/Consumer Problem

2 Synchronization - PC with Semaphores2 Abstract The producer/consumer problem is a classical synchronization exercise. The problem defines an arbitrary number of so-called producers which create goods and consumers which consume these goods. A bounded buffer of size n mediates among producers and consumers and adapts to the different execution speeds of all the involved processes resp. threads. Besides mutual exclusion issues, semaphores are required to block consumers until consumable goods are inside the buffer and to block producers until there is free space available inside the buffer to deposit additional goods. It is noteworthy that for these additional semaphores, the semaphore functions are called by opposite processes or threads (asymmetrical solution).

3 Synchronization - PC with Semaphores3 Producers Motivation n threads produce some good m threads consume some good Bounded buffer between producers and consumers –adapt to varying execution speeds –minimize waiting time How to synchronize the n+m threads? Consumers Buffer

4 Synchronization - PC with Semaphores4 Example Producer Consumer Buffer with 1 slot

5 Synchronization - PC with Semaphores5 Outline of Buffer public class Buffer { public Buffer ( int n ) { } /// Inserts another good into the buffer. /// May block until free space is available. public void Produce ( int good ) { } /// Consumes a good stored inside the buffer. /// May signal blocked producer threads. public int Consume () { return 42; }

6 Synchronization - PC with Semaphores6 Producer (C#) public class Producer { public Producer ( Buffer b ) { buffer = b; my_id = this.GetHashCode(); ThreadStart ts = new ThreadStart(Run); my_thread = new Thread(ts); my_thread.Start(); } private void Run () { Console.WriteLine("Producer {0}: started...",my_id); int good = this.GetHashCode() * 1000000; while (true) { buffer.Produce(good); Console.WriteLine("Producer {0}: good {1} stored",my_id,good); good++; } private Buffer buffer; private Thread my_thread; private int my_id; }

7 Synchronization - PC with Semaphores7 Consumer (C#) public class Consumer { public Consumer ( Buffer b ) { buffer = b; my_id = this.GetHashCode(); ThreadStart ts = new ThreadStart(Run); my_thread = new Thread(ts); my_thread.Start(); } private void Run () { Console.WriteLine("Consumer {0}: started...",my_id); while (true) { int good = buffer.Consume(); Console.WriteLine("Consumer {0}: good {1} retrieved",my_id,good); } private Buffer buffer; private Thread my_thread; private int my_id; }

8 Synchronization - PC with Semaphores8 Exercise Implement the buffer –Producers must block if buffer is full –Consumers must block if buffer is empty –Access to shared data structures should be mutally exclusive Examine the running solution

9 Synchronization - PC with Semaphores9 The Buffer (C#) public class Buffer { public Buffer ( int n ) { this.n = n; slots = new int[n]; mutex = new Semaphore(1); slots_available = new Semaphore(n); goods_available = new Semaphore(0); } … private int n; private int [] slots; private int free = 0; private int used = 0; private Semaphore mutex; private Semaphore slots_available; private Semaphore goods_available; } public void Produce ( int good ) { slots_available.P(); mutex.P(); slots[free] = good; free = (free+1) % n; mutex.V(); goods_available.V(); } public int Consume () { goods_available.P(); mutex.P(); int good = slots[used]; used = (used+1) % n; mutex.V(); slots_available.V(); return good; }

10 Synchronization - PC with Semaphores10 Program Visualization Producers Consumer Program Behavior Program State

11 Synchronization - PC with Semaphores11 Resources Every textbook on operating systems has a section on solving the producer/consumer problem with semaphores The "Little Book on Semaphores" by Allen B. Downey


Download ppt "Synchronization Producer/Consumer Problem. Synchronization - PC with Semaphores2 Abstract The producer/consumer problem is a classical synchronization."

Similar presentations


Ads by Google