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

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Tutorial 3 Sync or sink! presented by: Antonio Maiorano Paul Di Marco.
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Intertask Communication and Synchronization In this context, the terms “task” and “process” are used interchangeably.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
1 Synchronization Coordinating the Activity of Mostly Independent Entities.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
1 L50 Multithreading (2). 2 OBJECTIVES  What producer/consumer relationships are and how they are implemented with multithreading.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
1 Outline Processes Threads Inter-process communication (IPC) Classical IPC problems Scheduling.
CS444/CS544 Operating Systems Classic Synchronization Problems 2/26/2007 Prof. Searleman
1 Interprocess Communication Race Conditions Two processes want to access shared memory at same time.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Monitor  Giving credit where it is due:  The lecture notes are borrowed from Dr. I-Ling Yen at University of Texas at Dallas  I have modified them and.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Synchronization II: CPE Operating Systems
1 Using Semaphores CS 241 March 14, 2012 University of Illinois Slides adapted in part from material accompanying Bryant & O’Hallaron, “Computer Systems:
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
Synchronizing Threads with Semaphores
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Today’s Agenda  Quick Review  Semaphore and Lock Advanced Topics in Software Engineering 1.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-2: Process Synchronization Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
Consider the Java code snippet below. Is it a legal use of Java synchronization? What happens if two threads A and B call get() on an object supporting.
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
Operating System Concepts and Techniques Lecture 14 Interprocess communication-3 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
pThread synchronization
Chien-Chung Shen CIS/UD
CS703 - Advanced Operating Systems
Background on the need for Synchronization
Process Synchronization
Process Synchronization
Classical Synchronization Problems
Monitors.
CSE451 Basic Synchronization Spring 2001
HW1 and Synchronization & Queuing
Monitors Chapter 7.
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Monitors Chapter 7.
Monitors Chapter 7.
Monitor Giving credit where it is due:
Chapter 6 Synchronization Principles
Chapter 7: Synchronization Examples
CSE 153 Design of Operating Systems Winter 2019
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
Presentation transcript:

Synchronization Producer/Consumer Problem

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

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

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

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

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() * ; 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; }

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

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

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

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

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