Concurrency: shared objects & mutual exclusion1 ©Magee/Kramer 2 nd Edition Chapter 4 Shared Objects & Mutual Exclusion.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Ch 7 B.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
CSC321 §6 Modelling Processes using FSP 1 Section 6 Modelling Processes using FSP.
Concurrency: monitors & condition synchronization1 ©Magee/Kramer 2 nd Edition COMP60611 Fundamentals of Parallel and Distributed Systems Lecture 8b Monitors.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
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.
/ PSWLAB Atomizer: A Dynamic Atomicity Checker For Multithreaded Programs By Cormac Flanagan, Stephen N. Freund 24 th April, 2008 Hong,Shin.
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture.
Concurrency: concurrent execution ©Magee/Kramer Claus Brabrand University of Aarhus Concurrent Execution Concurrency.
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Concurrency: Deadlock1 ©Magee/Kramer 2 nd Edition Chapter 6 Deadlock.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
University of Sunderland Java Threading, Mutex and Synchronisation Lecture 02 COMM86 Concurrent and Distributed Software Systems.
Atomic Operations David Monismith cs550 Operating Systems.
Concurrency: processes & threads1 ©Magee/Kramer Chapter 2 Processes & Threads.
Definitions & Scenarios. Acknowledgements  This tutorial is based in part on Concurrency: State Models & Java Programming by Jeff Magee and Jeff Kramer.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
Concurrency: dynamic systems1 ©Magee/Kramer 2 nd Edition Chapter 9 Dynamic Systems.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
2015 Concurrency: processes & threads 1 ©Magee/Kramer 2 nd Edition Chapter 2 Processes & Threads.
CSC321 §6 Modelling Processes using FSP 1 Chapter 6 Modelling Processes using FSP.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
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.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer September 7th, 2010 The University of Georgia.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
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.
Threading and Concurrency COM379T John Murray –
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
2015 Concurrency: logical properties 1 ©Magee/Kramer 2 nd Edition Chapter 14 Logical Properties Satisfied? Not satisfied?
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 24 Critical Regions.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
CMSC 330: Organization of Programming Languages Threads.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
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.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Concurrency: processes & threads1 ©Magee/Kramer 2 nd Edition Chapter 2 Processes & Threads.
CS499 – Mobile Application Development Fall 2013 Threads & Android – Part 1.
Java Thread Programming
Shared Objects & Mutual Exclusion
Multithreading / Concurrency
Background on the need for Synchronization
Monitors Chapter 7.
Module 7a: Classic Synchronization
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
Monitors Chapter 7.
Monitors Chapter 7.
Chapter 6: Synchronization Tools
Presentation transcript:

Concurrency: shared objects & mutual exclusion1 ©Magee/Kramer 2 nd Edition Chapter 4 Shared Objects & Mutual Exclusion

Concurrency: shared objects & mutual exclusion2 ©Magee/Kramer 2 nd Edition Shared Objects & Mutual Exclusion Concepts : process interference. mutual exclusion. Models :model checking for interference modeling mutual exclusion Practice :thread interference in shared Java objects mutual exclusion in Java (synchronized objects/methods).

Concurrency: shared objects & mutual exclusion3 ©Magee/Kramer 2 nd Edition 4.1 Interference People enter an ornamental garden through either of two turnstiles. Management wish to know how many are in the garden at any time. The concurrent program consists of two concurrent threads and a shared counter object. Ornamental garden problem:

Concurrency: shared objects & mutual exclusion4 ©Magee/Kramer 2 nd Edition ornamental garden Program - class diagram The Turnstile thread simulates the periodic arrival of a visitor to the garden every second by sleeping for a second and then invoking the increment() method of the counter object.

Concurrency: shared objects & mutual exclusion5 ©Magee/Kramer 2 nd Edition ornamental garden program private void go() { counter = new Counter(counterD); west = new Turnstile(westD,counter); east = new Turnstile(eastD,counter); west.start(); east.start(); } The Counter object and Turnstile threads are created by the go() method of the Garden applet: Note that counterD, westD and eastD are objects of NumberCanvas used in chapter 2.

Concurrency: shared objects & mutual exclusion6 ©Magee/Kramer 2 nd Edition Turnstile class class Turnstile extends Thread { NumberCanvas display; Counter people; Turnstile(NumberCanvas n,Counter c) { display = n; people = c; } public void run() { try{ display.setvalue(0); for (int i=1;i<=Garden.MAX;i++){ Thread.sleep(500); //0.5 second between arrivals display.setvalue(i); people.increment(); } } catch (InterruptedException e) {} } The run() method exits and the thread terminates after Garden.MAX visitors have entered.

Concurrency: shared objects & mutual exclusion7 ©Magee/Kramer 2 nd Edition Counter class class Counter { int value=0; NumberCanvas display; Counter(NumberCanvas n) { display=n; display.setvalue(value); } void increment() { int temp = value; //read value Simulate.HWinterrupt(); value=temp+1; //write value display.setvalue(value); } Hardware interrupts can occur at arbitrary times. The counter simulates a hardware interrupt during an increment(), between reading and writing to the shared counter value. Interrupt randomly calls Thread.sleep() to force a thread switch.

Concurrency: shared objects & mutual exclusion8 ©Magee/Kramer 2 nd Edition ornamental garden program - display After the East and West turnstile threads have each incremented its counter 20 times, the garden people counter is not the sum of the counts displayed. Counter increments have been lost. Why?

Concurrency: shared objects & mutual exclusion9 ©Magee/Kramer 2 nd Edition concurrent method activation Java method activations are not atomic - thread objects east and west may be executing the code for the increment method at the same time. east west increment: read value write value + 1 program counter program counter PC shared code

Concurrency: shared objects & mutual exclusion10 ©Magee/Kramer 2 nd Edition ornamental garden Model Process VAR models read and write access to the shared counter value. Increment is modeled inside TURNSTILE since Java method activations are not atomic i.e. thread objects east and west may interleave their read and write actions.

Concurrency: shared objects & mutual exclusion11 ©Magee/Kramer 2 nd Edition ornamental garden model const N = 4 range T = 0..N set VarAlpha = { value.{read[T],write[T]} } VAR = VAR[0], VAR[u:T] = (read[u] ->VAR[u] |write[v:T]->VAR[v]). TURNSTILE = (go -> RUN), RUN = (arrive-> INCREMENT |end -> TURNSTILE), INCREMENT = (value.read[x:T] -> value.write[x+1]->RUN )+VarAlpha. ||GARDEN = (east:TURNSTILE || west:TURNSTILE || { east,west,display}::value:VAR) /{ go /{ east,west}.go, end/{ east,west}.end}. The alphabet of shared process VAR is declared explicitly as a set constant, VarAlpha. The TURNSTILE alphabet is extended with VarAlpha to ensure no unintended free (autonomous) actions in VAR eg. value.write[0]. All actions in the shared VAR must be controlled (shared) by a TURNSTILE.

Concurrency: shared objects & mutual exclusion12 ©Magee/Kramer 2 nd Edition checking for errors - animation Scenario checking - use animation to produce a trace. Is this trace correct?

Concurrency: shared objects & mutual exclusion13 ©Magee/Kramer 2 nd Edition checking for errors - exhaustive analysis TEST = TEST[0], TEST[v:T] = (when (v TEST[v+1] |end->CHECK[v] ), CHECK[v:T] = (display.value.read[u:T] -> (when (u==v) right -> TEST[v] |when (u!=v) wrong -> ERROR ) )+{display.VarAlpha}. Exhaustive checking - compose the model with a TEST process which sums the arrivals and checks against the display value: Like STOP, ERROR is a predefined FSP local process (state), numbered -1 in the equivalent LTS.

Concurrency: shared objects & mutual exclusion14 ©Magee/Kramer 2 nd Edition ornamental garden model - checking for errors ||TESTGARDEN = (GARDEN || TEST). Use LTSA to perform an exhaustive search for ERROR. Trace to property violation in TEST: go east.arrive east.value.read.0 west.arrive west.value.read.0 east.value.write.1 west.value.write.1 end display.value.read.1 wrong LTSA produces the shortest path to reach ERROR.

Concurrency: shared objects & mutual exclusion15 ©Magee/Kramer 2 nd Edition Interference and Mutual Exclusion Destructive update, caused by the arbitrary interleaving of read and write actions, is termed interference. Interference bugs are extremely difficult to locate. The general solution is to give methods mutually exclusive access to shared objects. Mutual exclusion can be modeled as atomic actions.

Concurrency: shared objects & mutual exclusion16 ©Magee/Kramer 2 nd Edition 4.2 Mutual exclusion in Java class SynchronizedCounter extends Counter { SynchronizedCounter(NumberCanvas n) {super(n);} synchronized void increment() { super.increment(); } We correct COUNTER class by deriving a class from it and making the increment method synchronized: Concurrent activations of a method in Java can be made mutually exclusive by prefixing the method with the keyword synchronized, which uses a lock on the object. acquire lock release lock

Concurrency: shared objects & mutual exclusion17 ©Magee/Kramer 2 nd Edition mutual exclusion - the ornamental garden Java associates a lock with every object. The Java compiler inserts code to acquire the lock before executing the body of the synchronized method and code to release the lock before the method returns. Concurrent threads are blocked until the lock is released.

Concurrency: shared objects & mutual exclusion18 ©Magee/Kramer 2 nd Edition Java synchronized statement Access to an object may also be made mutually exclusive by using the synchronized statement: synchronized (object) { statements } A less elegant way to correct the example would be to modify the Turnstile.run() method: synchronized(people) {people.increment();} Why is this “less elegant”? To ensure mutually exclusive access to an object, all object methods should be synchronized.

Concurrency: shared objects & mutual exclusion19 ©Magee/Kramer 2 nd Edition To add locking to our model, define a LOCK, compose it with the shared VAR in the garden, and modify the alphabet set : 4.3 Modeling mutual exclusion LOCK = (acquire->release->LOCK). ||LOCKVAR = (LOCK || VAR). set VarAlpha = {value.{read[T],write[T], acquire, release}} TURNSTILE = (go -> RUN), RUN = (arrive-> INCREMENT |end -> TURNSTILE), INCREMENT = (value.acquire -> value.read[x:T]->value.write[x+1] -> value.release->RUN )+VarAlpha. Modify TURNSTILE to acquire and release the lock:

Concurrency: shared objects & mutual exclusion20 ©Magee/Kramer 2 nd Edition Revised ornamental garden model - checking for errors Use TEST and LTSA to perform an exhaustive check. Is TEST satisfied? go east.arrive east.value.acquire east.value.read.0 east.value.write.1 east.value.release west.arrive west.value.acquire west.value.read.1 west.value.write.2 west.value.release end display.value.read.2 right A sample animation execution trace

Concurrency: shared objects & mutual exclusion21 ©Magee/Kramer 2 nd Edition COUNTER: Abstraction using action hiding To model shared objects directly in terms of their synchronized methods, we can abstract the details by hiding. For SynchronizedCounter we hide read, write, acquire, release actions. const N = 4 range T = 0..N VAR = VAR[0], VAR[u:T] = ( read[u]->VAR[u] | write[v:T]->VAR[v]). LOCK = (acquire->release->LOCK). INCREMENT = (acquire->read[x:T] -> (when (x<N) write[x+1] ->release->increment->INCREMENT ) )+{read[T],write[T]}. ||COUNTER =

Concurrency: shared objects & mutual exclusion22 ©Magee/Kramer 2 nd Edition COUNTER: Abstraction using action hiding Minimized LTS: We can give a more abstract, simpler description of a COUNTER which generates the same LTS: This therefore exhibits “equivalent” behavior i.e. has the same observable behavior. COUNTER = COUNTER[0] COUNTER[v:T] = (when (v COUNTER[v+1]).

Concurrency: shared objects & mutual exclusion23 ©Magee/Kramer 2 nd Edition Summary  Concepts process interference mutual exclusion  Models model checking for interference modeling mutual exclusion  Practice thread interference in shared Java objects mutual exclusion in Java (synchronized objects/methods).