1 L50 Multithreading (2). 2 OBJECTIVES  What producer/consumer relationships are and how they are implemented with multithreading.

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.
Ch 7 B.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
 2005 Pearson Education, Inc. All rights reserved Multithreading.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
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.
Threading Part 3 CS221 – 4/24/09. Teacher Survey Fill out the survey in next week’s lab You will be asked to assess: – The Course – The Teacher – The.
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)
1 L49 Multithreading (1). 2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
1 Semaphores Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent.
 2007 Pearson Education, Inc. All rights reserved Multithreading.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
 2006 Pearson Education, Inc. All rights reserved Multithreading.
Chapter 6 – Concurrent Programming Outline 6.1 Introduction 6.2Monitors 6.2.1Condition Variables 6.2.2Simple Resource Allocation with Monitors 6.2.3Monitor.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
CS444/CS544 Operating Systems Classic Synchronization Problems 2/26/2007 Prof. Searleman
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
Threads in Java a tutorial introduction PdD Marco Antonio Ramos Corchado February 2009.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 16 – Multithreading Outline 16.1 Introduction 16.2 Thread States: Life Cycle of a Thread 16.3 Thread.
CSE 501N Fall ‘09 23: Advanced Multithreading: Synchronization and Thread-Safety December 1, 2009 Nick Leidenfrost.
Multi-Threaded Programming Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
1 G53SRP: Java Concurrency Control (1) - synchronisation Chris Greenhalgh School of Computer Science.
Synchronization Producer/Consumer Problem. Synchronization - PC with Semaphores2 Abstract The producer/consumer problem is a classical synchronization.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
Multithreading Chapter Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
Java Thread and Memory Model
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Advanced Programming, Based on LY Stefanus’s slides slide 7.1 Multithreading : Using the Runnable interface.
Multi-Threading in Java
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 16 – Multithreading Outline 16.1 Introduction 16.2 Thread States: Life Cycle of a Thread 16.3.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
T HREADS Lecture 5 1 L. Mohammad R.Alkafagee. H EAVYWEIGHT - PROCESSES The cooperation of traditional processes also known as heavyweight processes which.
Producer-Consumer Problem David Monismith cs550 Operating Systems.
1 5-High-Performance Embedded Systems using Concurrent Process (cont.)
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Software Design 13.1 From controller to threads l Threads are lightweight processes (what’s a process?)  Threads are part of a single program, share state.
pThread synchronization
Distributed and Parallel Processing George Wells.
Threaded Programming in Python
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
23 Multithreading.
Thread synchronization
5-High-Performance Embedded Systems using Concurrent Process (cont.)
Multithreading Chapter 23.
Synchronization Lecture 23 – Fall 2017.
Condition Variables and Producer/Consumer
Multithreading.
Condition Variables and Producer/Consumer
COT 5611 Operating Systems Design Principles Spring 2014
Dr. Mustafa Cem Kasapbaşı
NETWORK PROGRAMMING CNET 441
Presentation transcript:

1 L50 Multithreading (2)

2 OBJECTIVES  What producer/consumer relationships are and how they are implemented with multithreading.

Producer/Consumer Relationship without Synchronization Producer/consumer relationship – Producer generates data and stores it in shared memory – Consumer reads data from shared memory – Shared memory is called the buffer

4 Outline Buffer.java Fig | Buffer interface used in producer/consumer examples.

5 Outline Producer.java (1 of 2) Implement the runnable interface so that producer will run in a separate thread Declare run method to satisfy interface

6 Outline Producer.java (2 of 2) Sleep for up to 3 seconds

7 Outline Consumer.java (1 of 2) Implement the runnable interface so that producer will run in a separate thread Declare run method to satisfy interface

8 Outline Consumer.java (2 of 2) Sleep for up to 3 seconds

9 Outline Unsynchronized Buffer.java Shared variable to store data Set the value of the bufferRead the value of the buffer

10 Outline SharedBufferTest.java (1 of 4) Create shared UnsynchronizedBuffer for producer and consumer to use

11 Outline SharedBufferTest.java (2 of 4) Pass shared buffer to both producer and consumer

12 Outline SharedBufferTest.java (3 of 4)

13 Outline SharedBufferTest.java (4 of 4)

Producer/Consumer Relationship with Synchronization Producer/consumer relationship – This example uses Lock s and Condition s to implement synchronization

15 Outline SynchronizedBuffer.java (1 of 5) Create ReentrantLock for mutual exclusion Create two Condition variables; one for writing and one for reading Buffer shared by producer and consumer Try to obtain the lock before setting the value of the shared data

16 Outline SynchronizedBuffer.java (2 of 5) Producer waits until buffer is empty

17 Outline SynchronizedBuffer.java (3 of 5) Signal consumer that it may read a value Release lock on shared data Acquire lock before reading a value

18 Outline SynchronizedBuffer.java (4 of 5) Consumer waits until buffer contains data to read

19 Outline SynchronizedBuffer.java (5 of 5) Signal producer that it can write to buffer Release lock on shared data

20 Outline SharedBufferTest2.java (1 of 4) Create SynchronizedBuffer to be shared between producer and consumer

21 Outline SharedBufferTest2.java (2 of 4) Execute the producer and consumer in separate threads

22 Outline SharedBufferTest2.java (3 of 4)

23 Outline SharedBufferTest2.java (4 of 4)

Producer/Consumer Relationship Circular Buffer Circular buffer – Provides extra buffer space into which producer can place values and consumer can read values

25 Outline CircularBuffer.java (1 of 5) Lock to impose mutual exclusionCondition variables to control writing and reading Circular buffer; provides three spaces for data Obtain the lock before writing data to the circular buffer

26 Outline CircularBuffer.java (2 of 5) Wait until a buffer space is empty Update index; this statement imposes the circularity of the buffer Signal waiting thread it can now read data from buffer Release the lock

27 Outline CircularBuffer.java (3 of 5) Lock the object before attempting to read a value Wait for a value to be written to the buffer Update read index; this statement imposes the circularity of the buffer

28 Outline CircularBuffer.java (4 of 5) Signal thread waiting to write to the buffer Release the lock

29 Outline CircularBuffer.java (5 of 5)

30 Outline CircularBufferTest.java (1 of 4) Create CircularBuffer for use in both the producer and consumer Execute the producer and consumer in separate threads

31 Outline CircularBufferTest.java (2 of 4)

32 Outline CircularBufferTest.java (3 of 4)

33 Outline CircularBufferTest.java (4 of 4)

Producer/Consumer Relationship ArrayBlockingQueue ArrayBlockingQueue – Fully implemented version of the circular buffer – Implements the BlockingQueue interface – Declares methods put and take to write and read date from the buffer, respectively

35 Outline BlockingBuffer.java (1 of 2) Create instance of ArrayBlockingQueue to store data Place a value into the buffer; blocks if buffer is full

36 Outline BlockingBuffer.java (2 of 2) Remove value from buffer; blocks if buffer is empty

37 Outline BlockingBufferTest.java (1 of 2) Create BlockingBuffer for use in producer and consumer Execute the producer and consumer in separate threads

38 Outline BlockingBufferTest.java (2 of 2)