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.

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.
Prepared By Sarath S Menon S6 CSE.  Imagine a scenario in which there exists two Distinct processes both operating on a single shared data area.  One.
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)
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
Concurrency 101 Shared state. Part 1: General Concepts 2.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Intro to Threading CS221 – 4/20/09. What we’ll cover today Finish the DOTS program Introduction to threads and multi-threading.
Threading Wrapup CS221 – 4/29/09. Concurrent Collections Java supplies a set of concurrent collections you can use manage sets of data in a multi- threaded.
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.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
1 L50 Multithreading (2). 2 OBJECTIVES  What producer/consumer relationships are and how they are implemented with multithreading.
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.
Week 9 Building blocks.
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.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Race condition The scourge of parallel and distributed computing...
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
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.
Kernel Locking Techniques by Robert Love presented by Scott Price.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Lecture 5 Page 1 CS 111 Summer 2013 Bounded Buffers A higher level abstraction than shared domains or simple messages But not quite as high level as RPC.
Distributed and Parallel Processing George Wells.
Java.util.concurrent package. concurrency utilities packages provide a powerful, extensible framework of high-performance threading utilities such as.
Race Conditions & Synchronization
CS703 - Advanced Operating Systems
Multithreading / Concurrency
CS703 – Advanced Operating Systems
COT 4600 Operating Systems Fall 2009
Background on the need for Synchronization
Chapter 5: Process Synchronization – Part II
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Synchronization Lecture 23 – Fall 2017.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Chapter 7: Synchronization Examples
CSE 153 Design of Operating Systems Winter 19
CSE 451 Section 1/27/2000.
Software Engineering and Architecture
Synchronization and liveness
Presentation transcript:

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 typed and/or handwritten notes Pick up: In the CS office starting 5/11

Test Review Wednesday: Mindmap exercise Friday: Review anything you need more time on. Answer questions.

Where We Left Off Data integrity options: – Synchronized methods – Synchronized statements using locks – Atomic data access – Immutable objects Order of operations options: – Guarded blocks – Locks

Liveness Problems Can be broken into three categories: – Deadlock – Starvation – Livelock

Deadlock Two or more threads are blocked on each other, waiting forever.

Example

Starvation A thread needs access to a resource but cannot get it for long periods of time Caused by a greedy thread which blocks other threads access to the resource For instance – Imagine a synchronized method that is very slow to return – Thread 1 calls this method often – Thread 2, when calling the method, will often be blocked

Livelock Thread 1 takes action in response to Thread 2 Thread 2 takes action in response to Thread 1 Threads aren’t blocked but they are in an endless loop of responses and won’t do other work.

Livelock Example Alphonse moves to his left to let Gaston pass, while Gaston moves to his right to let Alphonse pass. Seeing that they are still blocking each other, Alphone moves to his right, while Gaston moves to his left. They're still blocking each other, so...

Guarded Blocks In order to coordinate activity between two threads we can use Guarded Blocks Wait() – puts a thread to sleep until it is notified Notify() – wakes up one thread that is waiting on this object NotifyAll() – wakes up all threads waiting on this object

Guarded Blocks Example

Producer-Consumer Example Remember the producer-consumer scenario? Let’s look at a possible implementation – Recall, the previous pseudocode had a thread synchronization problem that could lead to deadlock.

Producer-Consumer Example

Our implementation is inefficient. Why? How do we improve it?

Producer-Consumer Example Things to Notice: – Sleep vs. Wait and Notify – Use of finally – Use of interrupt – What is the impact if we change the buffer size? – What is the impact if we modify the random timeouts?

Lock Objects Recall that every object is associated with an intrinsic lock In order for a thread to get exclusive access to an object, it must: – Acquire the lock before access – Release the lock when it is done

Lock Objects Remember that synchronized methods and statements use locks implicitly: – Thread 1 acquires lock for the Counter object – Thread 1 calls increment method() – Thread 2 tries to acquire the lock – Thread 2 blocks – Thread 1 releases the lock – Thread 2 acquires lock for the Counter object – Thread 2 calls decrement() method

Lock Objects Lock objects give you more sophisticated, explicit access to lock behavior Java.util.concurrent.locks – urrent/locks/package-summary.html urrent/locks/package-summary.html “The framework permits much greater flexibility in the use of locks and conditions, at the expense of more awkward syntax.

Lock Objects Like Intrinsic locks: – Only one thread can hold a lock object at a time – Lock objects support wait() and notify() Lock objects add flexibility: – You can back out of an attempt to acquire a lock Immediately On a timeout On an interrupt – There are a number of lock objects to choose from Read/Write Lock – separate locks for read vs. write access Condition – separate notification mechanisms

Example Alphonse and Gaston are in a loop, continually bowing to each other We want to acquire a lock for each friend before starting a bow Let’s look at how lock objects can help

Example

Things to Notice: – ReentrantLock Object: similar to an intrinsic lock – Use of tryLock() – Use of unlock() – The conditions under which both locks cannot be acquired

Concurrent Collections Java supplies a set of concurrent collections you can use manage sets of data in a multi- threaded program Examples – ArrayBlockingQueue – ConcurrentHashMap – ConcurrentNavigableMap

ArrayBlockingQueue Queue data structure with some added benefits – Blocks when you attempt to add to a full queue – Blocks when you attempt to remove from an empty queue – Wakes up the blocked thread when the queue is ready once more Safe to use with as many threads as you wish

Example Let’s convert producer-consumer to use this datastructure!

ConcurrentHashMap Provides all the functionality of a hash table, plus it is thread-safe. Allows you to add items to the data structure based on a key Supports fast retrieval of the items based on the same key Hashing is much faster than a search, but takes up more space

MultiThreaded Dots Let’s convert our dot moving program to use multiple threads. Steps – Decide what should be in each thread – Move MouseMotionListener to new thread – Move mouse event handlers to the new thread – Start the new thread from MoveComponent constructor

MultiThreaded Dots To be safe: Convert Dots class to use Vector instead of ArrayList – Tip: Vector is thread-safe and ArrayList is not Note: If we’d designed Dots class better we could have replaced ArrayList with Vector without having to modify the calling code. – What would have been a better way to design this class?

Semaphore Semaphore is a generalized version of a mutex. – A mutex is mutually exclusive, only one thread can access at a time. – A semaphore can allow 1..n threads access at a time. – If you reduce semaphore to 1 thread, it is a mutex. oncurrent/Semaphore.html

Thread Priorities You can use thread priority to give some of your threads higher priority than others. Higher priority threads will get more CPU cycles than lower priority threads

Example Create an applet with two counting threads See how the thread priority changes counting speed