Software Engineering and Architecture

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

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.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Ch 7 B.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Chapter 6: Process Synchronization
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Practice Session 7 Synchronization Liveness Guarded Methods Model
Concurrency (3) CSE 132. iClicker/WUTexter Question The following four numbers are in 8-bit 2’s complement form: Which.
CS 11 java track: lecture 7 This week: Web tutorial:
Threading Part 4 CS221 – 4/27/09. The Final Date: 5/7 Time: 6pm Duration: 1hr 50mins Location: EPS 103 Bring: 1 sheet of paper, filled both sides with.
Definitions Process – An executing program
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Internet Software Development Controlling Threads Paul J Krause.
Threading Eriq Muhammad Adams J
Producer-Consumer Problem The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.bufferqueue.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Java Thread and Memory Model
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
CS 151: Object-Oriented Design November 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.
Semaphores Chapter 6. Semaphores are a simple, but successful and widely used, construct.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
Distributed and Parallel Processing George Wells.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Tutorial 2: Homework 1 and Project 1
Multithreading / Concurrency
Background on the need for Synchronization
Lecture 24 Concurrency 2 (D&D 23) Date.
Threads and Concurrency in Java: Part 2
Threads Chate Patanothai.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Threads II IS
Synchronization Lecture 23 – Fall 2017.
Condition Variables and Producer/Consumer
Multithreading.
Condition Variables and Producer/Consumer
Race Conditions & Synchronization
Producer-Consumer Problem
Multithreading.
Lecture 2 Part 2 Process Synchronization
Dr. Mustafa Cem Kasapbaşı
Semaphores Chapter 6.
Monitor Giving credit where it is due:
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Threads and Multithreading
Software Engineering and Architecture
CSE 542: Operating Systems
More concurrency issues
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Synchronization and liveness
Presentation transcript:

Software Engineering and Architecture Producer Consumer

Henrik Bærbak Christensen Recap The three categories of concurrent programs Independent threads Like running your Media player program while coding in IntelliJ Shared resources Like two threads reading/writing to the same account object Typical case: web servers handling resources You cannot book the same train seat twice! Collaborating processes Like one thread inserting into a buffer and assuming some other thread will remove those items from the buffer CS@AU Henrik Bærbak Christensen

Collaborating Threads The last, and most challenging, class of concurrent programs is collaborating processes The typical example is the Producer Consumer For instance Printer queue; high volume web traffic; disk read/write; … Producer Queue Consumer CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Buffer Producer(s) and Consumer(s) are threads, and Our Queue (Buffer) class has methods Synchronized void put(Object o) Insert ‘o’ into buffer Synchronized Object take() Retrieve ‘o’ from buffer Both must be critical regions - guarded by Lock or Synchronized Consider the ‘consumer’ that wants to take the next item ‘o’ to process it But if there is no item in the queue, it of course has to wait until there is an item available; that is, a producer has put something into the queue… CS@AU Henrik Bærbak Christensen

Waiting for item to be available Waiting for item ‘o’ to become available ? Queue Consumer CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Waiting for ‘o’ So, how do we arrange to wait for the item? In the consumer code Call ‘take()’ repeatedly until it returns a non-null value This is called polling (busy waiting), and wastes a lot of CPU cycles on nothing … And there is a waiting time from item available to processing Similar to picking up the phone every 1 minute to see if any has called you … ? Queue Consumer CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Waiting for ‘o’ So, how do we arrange to wait for the item? In the queue code’s take() method Wait inside – but hey! It is a critical region and thus no producer can ever enter the ‘put()’ method Deadlock A thread waits infinitely for an event that will never happen Queue Consumer CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Deadlock Repeat (as it is important!) We would like to wait in the queue code Take() is called and then just returns when there is item available But we cannot because Take() will take the ‘lock’ on the object and thus no other thread will ever be able to call the method put() Waiting for the lock outside… CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen So We Want… … a mechanism that awaits that a condition becomes true Let other threads acquire the lock so they can make that condition true Not Java code! class Queue { Object p; boolean empty = true; public synchronized void put(Object p) { await (empty); this.p = p; empty = false; } public synchronized Object take() { await (!empty); empty = true; return p; CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Example Scenario C call take() Takes lock Empty == true Release lock and Enter Waiting state P calls put() No wait (empty) Finish and release lock Later: Scheduler force P into ready state (not running) C enters ‘running’ state Empty = false! class Queue { Object p; boolean empty = true; public synchronized void put(Object p) { await (empty); this.p = p; empty = false; } public synchronized Object take() { await (!empty); empty = true; return p; CS@AU Henrik Bærbak Christensen

Java Primitives (Java 1.4) Java objects maintain a wait-set in addition to the lock a.wait() does atomically Force current thread into waiting state, Add current thread in object’s wait-set Release the lock on the object, a a.notify() does Choose one random thread, T, in a’s wait-set T must take the lock on a May fail if another thread has already taken the lock! T resumes execution (becomes runnable) from the wait() statement a.notifyAll() does The same except all threads in a’s wait-set become ‘runnable’… a CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Java 1.4 Code class Channel { Object p; boolean empty = true; synchronized public void store(Object p) { while (!empty) { try { wait(); } catch (InterruptedException ie) {} } this.p = p; empty = false; notifyAll(); synchronized public Object retrieve() { while (empty) { empty = true; notifyAll(); return p; CS@AU Henrik Bærbak Christensen

Java 1.4 Code Why a loop around wait() ??? class Channel { Object p; boolean empty = true; synchronized public void store(Object p) { while (!empty) { try { wait(); } catch (InterruptedException ie) {} } this.p = p; empty = false; notifyAll(); synchronized public Object retrieve() { while (empty) { empty = true; notifyAll(); return p; Why a loop around wait() ??? Hint: What if 10 threads are waiting in Wait()? CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Note The wait-set only makes sense inside a critical region You cannot call ‘wait()’ or ‘notify()’ if you are not in a synchronize method / critical region Will throw exceptions at your if you try… CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Demo CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Java Code CS@AU Henrik Bærbak Christensen

Java 5 Onwards

Henrik Bærbak Christensen Critique Java was the first mainstream language to have internal threading Brink Hansen should have said that all his whole lifelong research into concurrency was a complete waste  Morale: It had to be improved… Package: java.util.concurrent Much more fine-grained concurrency control A lot of default implementations without bugs! CS@AU Henrik Bærbak Christensen

Java 1.5 Code The Lock Two different wait-sets associated… Now, producers are waiting in one wait-set; while consumers are in another! We are sure to signal the right one! CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen And Even More Easy! It is already implemented ! CS@AU Henrik Bærbak Christensen

Moving On…

Henrik Bærbak Christensen Vast Subject Area Lots of properties of concurrent programs Liveliness Fairness Starvation Deadlocks Performance / blocked threads Thread priority And library support Java Collection classes are not thread safe  But Decorators exists List newList = Collections.synchronizedList(oldList); CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Vast Subject Area And Parallelism – the other side of concurrency Java Stream processing Runs concurrently if you use parallelStream() Map-Reduce Why not use 1.000 machines to compute ‘f’? And on, and on, and on… CS@AU Henrik Bærbak Christensen