The Joy of Synchronization Lecture 19, Nov 5. Administrivia Reminder: Schedule P3M1 with us Only 3 groups scheduled so far… Time slots running out...

Slides:



Advertisements
Similar presentations
Tutorial 3 Sync or sink! presented by: Antonio Maiorano Paul Di Marco.
Advertisements

Class 18: Concurrency on Mars Fall 2010 UVa David Evans cs2220: Engineering Software Image: Michael Dewey-Vogt.
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.
Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Chapter 6: Process Synchronization
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
The Zen of Threads. 3 Types of Execution Single, lone process doing all the work No problems with multiple-access Can be inconvenient/inefficient -- blocking,
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
CS444/CS544 Operating Systems Synchronization 2/19/2007 Prof. Searleman
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
The Zen of Threads When you can snatch the lock from the object, grasshopper, then you are ready to spawn the thread...
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Oh what a tangled web we weave when first to thread we do conceive.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
The Zen of Threads When you can snatch the lock from the object, grasshopper, then you are ready to spawn the thread... Lecture 18, Nov 03.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Synchronization Andy Wang Operating Systems COP 4610 / CGS 5765.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
Atomic Operations David Monismith cs550 Operating Systems.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
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.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Thread Synchronization including Mutual Exclusion In Java synchronized keyword Monitor or Lock with conditions Semaphore.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Lecture 3 Concurrency and Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
CSE 153 Design of Operating Systems Winter 2015 Lecture 5: Synchronization.
CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization.
CSC 480 Software Engineering Lab 2 – Multi-Threading Oct 18, 2002.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
Java Thread Programming
CSE 120 Principles of Operating
Background on the need for Synchronization
COEN346 Tutorial Monitor Objects.
Monitors.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Multithreading.
Synchronization Issues
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
CSE 451: Operating Systems Winter 2007 Module 6 Synchronization
Thread Synchronization including Mutual Exclusion
Lecture 11: Mutual Exclusion
CSE 451: Operating Systems Autumn 2004 Module 6 Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2004 Module 6 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
CSE 451: Operating Systems Winter 2007 Module 6 Synchronization
CSE 451: Operating Systems Autumn 2009 Module 7 Synchronization
Monitors and Inter-Process Communication
Sarah Diesburg Operating Systems CS 3430
Presentation transcript:

The Joy of Synchronization Lecture 19, Nov 5

Administrivia Reminder: Schedule P3M1 with us Only 3 groups scheduled so far… Time slots running out...

Wheel of Time (god I hope not...) Last time: Multiprocessing intro: thread/process machine models Intro to Graphics2D: the basic graphics pipeline Geometry and rendering intro Today: Threading reprised More on geometry and affine transforms

The joy of synchronization

Synchronization Can’t control when/how long each thread runs Can ensure that threads don’t work on same data at same time

Synchronization Definition: critical section Segment of code that should not be invoked by >1 thread at a time

Synchronization Definition: Mutual exclusion Only 1 thread can be executing block (critical section) at a time

Synchronization Definition: Lock (monitor) Data struct used to ensure mutual exclusion w/in critical section

Kinds of exclusion Definition: enforced mutual exclusion You don’t have any choice. The system (OS, DB, etc.) forces mutex on the threads Definition: advisory mutual exclusion Everybody has to play nice together. It is up to the code to ensure that mutex is met.

Live Action Multithreading Demo

public class BoxObject { private int[] DataArray=new int[4]; public void twiddle(int c) { for (i=0;i<DataArray.length;++i) { DataArray[i]=c*i; } BoxObject X=new BoxObject(); BoxObject Y=new BoxObject();

public class BoxObject { private int[] DataArray=new int[4]; public void twiddle(int c) { for (i=0;i<DataArray.length;++i) { DataArray[i]=c*i; } BoxObject X=new BoxObject(); BoxObject Y=new BoxObject(); Thread A:Thread B: X.twiddle(3)Y.twiddle(5)

X.DataArray={ 0, 3, 6, 9 } Y.DataArray={ 0, 5, 10, 15 }

public class BoxObject { private int[] DataArray=new int[4]; public void twiddle(int c) { for (i=0;i<DataArray.length;++i) { DataArray[i]=c*i; } BoxObject X=new BoxObject(); BoxObject Y=new BoxObject(); Thread A:Thread B: X.twiddle(3)X.twiddle(5)

X.DataArray={ 0, 3, 6, 9 } or, maybe... X.DataArray={ 0, 5, 10, 15 } or, maybe... X.DataArray={ 0, 5, 6, 9 } or even... X.DataArray={ 0, 3, 10, 9 }

public class BoxObject { private int[] DataArray=new int[4]; private Lock _l=new Lock(); }

public class BoxObject { private int[] DataArray=new int[4]; private Lock _l=new Lock(); public void twiddle(int c) { _l.retrieve(); for (i=0;i<DataArray.length;++i) { DataArray[i]=c*i; } _l.release(); }

public class BoxObject { private int[] DataArray=new int[4]; private Lock _l=new Lock(); public void twiddle(int c) { _l.retrieve(); for (i=0;i<DataArray.length;++i) { DataArray[i]=c*i; } _l.release(); } Thread A:Thread B: X.twiddle(3)Y.twiddle(5)

public class BoxObject { private int[] DataArray=new int[4]; private Lock _l=new Lock(); public void twiddle(int c) { _l.retrieve(); for (i=0;i<DataArray.length;++i) { DataArray[i]=c*i; } _l.release(); } Thread A:Thread B: X.twiddle(3)X.twiddle(5)

public class BoxObject { private int[] DataArray=new int[4]; private static int[] StaticData= new int[4]; private Lock _l=new Lock(); }

public class BoxObject { private int[] DataArray=new int[4]; private static int[] StaticData= new int[4]; private Lock _l=new Lock(); public void twiddle(int c) { _l.retrieve(); for (i=0;i<DataArray.length;++i) { DataArray[i]=c*i; StaticData[i]=-c*c*i*i; } _l.release(); }

Back to Java: Synchronization

The (dirty) truth of synchronized Java synchronized keyword only means: Get the lock before entering block Release lock when leaving block Trick is: which lock do you mean?

Context Switch

Back to Graphics