CSE 332: Locks and Deadlocks

Slides:



Advertisements
Similar presentations
CS492B Analysis of Concurrent Programs Lock Basics Jaehyuk Huh Computer Science, KAIST.
Advertisements

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.
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.
Concurrency 101 Shared state. Part 1: General Concepts 2.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 4 Shared-Memory Concurrency & Mutual Exclusion Dan Grossman Last Updated:
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections Dan Grossman Spring 2010.
A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 6 Data Races and Memory Reordering Deadlock Readers/Writer Locks Condition.
CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion Tyler Robison Summer
CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion Dan Grossman Spring 2010.
A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 5 Programming with Locks and Critical Sections Dan Grossman Last Updated:
CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections Tyler Robison Summer
CSE332: Data Abstractions Lecture 24: Remaining Topics in Shared-Memory Concurrency Dan Grossman Spring 2010.
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.
A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 4 Shared-Memory Concurrency & Mutual Exclusion Dan Grossman Last Updated:
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
CSE 332 Data Abstractions: Data Races and Memory, Reordering, Deadlock, Readers/Writer Locks, and Condition Variables (oh my!) Kate Deibel Summer 2012.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 5 Programming with Locks and Critical Sections Original Work by: Dan Grossman.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Optimistic Design 1. Guarded Methods Do something based on the fact that one or more objects have particular states  Make a set of purchases assuming.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Internet Software Development Controlling Threads Paul J Krause.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
CSE332: Data Abstractions Lecture 25: Deadlocks and Additional Concurrency Issues Tyler Robison Summer
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
CPE779: More on OpenMP Based on slides by Laxmikant V. Kale and David Padua of the University of Illinois.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
CSE 332 Data Abstractions: Parallel Sorting & Introduction to Concurrency Kate Deibel Summer 2012 August 6, 2012CSE 332 Data Abstractions, Summer
Mergesort example: Merge as we return from recursive calls Merge Divide 1 element 829.
CSCD 330 Network Programming
Shared-Memory Concurrency & Mutual Exclusion
CSE332: Data Abstractions Lecture 23: Data Races and Memory Reordering Deadlock Readers/Writer Locks Condition Variables Dan Grossman Spring 2012.
Synchronization Lecture 23 – Fall 2017.
CSE332: Data Abstractions Lecture 21: Shared-Memory Concurrency & Mutual Exclusion Dan Grossman Spring 2012.
More on Thread Safety CSE451 Andrew Whitaker.
Process Synchronization
Java Concurrency 17-Jan-19.
CSE 451: Operating Systems Winter 2007 Module 6 Synchronization
Kernel Synchronization II
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
Java Concurrency.
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
Java Concurrency.
CSE 451: Operating Systems Spring 2008 Module 7 Synchronization
CSE 451: Operating Systems Winter 2007 Module 6 Synchronization
CSE 451: Operating Systems Autumn 2009 Module 7 Synchronization
CSE 451 Section 1/27/2000.
Java Concurrency 29-May-19.
CSE 332: Concurrency and Locks
CSE 451: Operating Systems Spring 2007 Module 7 Synchronization
EECE.4810/EECE.5730 Operating Systems
Software Engineering and Architecture
CSE 542: Operating Systems
CSE 542: Operating Systems
A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 4 Shared-Memory Concurrency & Mutual Exclusion Dan Grossman Last Updated:
Presentation transcript:

CSE 332: Locks and Deadlocks Richard Anderson Spring 2016 1

Announcements 2

Recall Bank Account Problem class BankAccount { private int balance = 0; synchronized int getBalance() { return balance; } synchronized void setBalance(int x) { balance = x; } synchronized void withdraw(int amount) { int b = getBalance(); if(amount > b) throw … setBalance(b – amount); } // deposit would also use synchronized Call to setBalance in withdraw tries to lock this 3

Re-Entrant Lock A re-entrant lock (a.k.a. recursive lock) If a thread holds a lock, subsequent attempts to acquire the same lock in the same thread won’t block withdraw can acquire the lock and setBalance can also acquire it implemented by maintaining a count of how many times each lock is acquired in each thread, and decrementing the count on each release. Java synchronize locks are re-entrant 4

Lock everything? No. For every memory location (e.g., object field), obey at least one of the following: Thread-local: only one thread sees it Immutable: read-only Shared-and-mutable: control access via a lock need synchronization ask the class what goes wrong in each case thread-local memory all memory immutable memory 5

Thread local Whenever possible, do not share resources easier to give each thread its own local copy only works if threads don’t need to communicate via resource In typical concurrent programs, the vast majority of objects should be thread local: shared memory should be rare—minimize it ask the class what goes wrong in each case 6

Immutable If location is read-only, no synchronizatin is necessary Whenever possible, do not update objects make new objects instead! one of the key tenets of functional programming (CSE 341) In practice, programmers usually over-use mutation – minimize it ask the class what goes wrong in each case 7

The rest: keep it synchronized ask the class what goes wrong in each case 8

Other Forms of Locking in Java Java provides many other features and details. See, for example: Chapter 14 of CoreJava, Volume 1 by Horstmann/Cornell Java Concurrency in Practice by Goetz et al 9

Locking Guidelines Correctness Consistency: make it well-defined Granularity: coarse to fine Critical Sections: make them small, atomic Leverage libraries 10

Consistent Locking Clear mapping of locks to resources followed by all methods clearly documented same lock can guard multiple resources what’s a resource? Conceptual: object field data structure (e.g., linked list, hash table) 11

Lock Granularity Coarse grained: fewer locks, more objects per lock e.g., one lock for entire data structure (e.g., linked list) advantage: disadvantage: Fine grained: more locks, fewer objects per lock e.g., one lock for each item in the linked list … … 12

Lock Granularity Example: hashtable with separate chaining coarse grained: one lock for whole table fine grained: one lock for each bucket Which supports more concurrency for insert and lookup? Which makes implementing resize easier? Suppose hashtable maintains a numElements field. Which locking approach is better? 13

Critical Sections Critical sections: how much code executes while you hold the lock? want critical sections to be short make them “atomic”: think about smallest sequence of operations that have to occur at once (without data races, interleavings) 14

Critical Sections Suppose we want to change a value in a hash table assume one lock for the entire table computing the new value takes a long time (“expensive”) synchronized(lock) { v1 = table.lookup(k); v2 = expensive(v1); table.remove(k); table.insert(k,v2); } 15

Critical Sections Suppose we want to change a value in the hash table assume one lock for the entire table computing the new value takes a long time (“expensive”) will this work? synchronized(lock) { v1 = table.lookup(k); } v2 = expensive(v1); table.remove(k); table.insert(k,v2); 16

Critical Sections Suppose we want to change a value in the hash table assume one lock for the entire table computing the new value takes a long time (“expensive”) convoluted fix: done = false; while(!done) { synchronized(lock) { v1 = table.lookup(k); } v2 = expensive(v1); if(table.lookup(k)==v1) { done = true; // I can exit the loop! table.remove(k); table.insert(k,v2); }}} 17

Leverage Libraries Use built-in libraries whenever possible In “real life”, it is unusual to have to write your own data structure from scratch Implementations provided in standard libraries Point of CSE332 is to understand the key trade-offs, abstractions, and analysis of such implementations Especially true for concurrent data structures Very difficult to provide fine-grained synchronization without race conditions Standard thread-safe libraries like ConcurrentHashMap written by world experts 18

Another Bank Operation Consider transferring money: What can go wrong? class BankAccount { … synchronized void withdraw(int amt) {…} synchronized void deposit(int amt) {…} synchronized void transferTo(int amt, BankAccount a) { this.withdraw(amt); a.deposit(amt); } 19

Deadlock x and y are two different accounts acquire lock for x withdraw from x block on lock for y acquire lock for y withdraw from y block on lock for x Time Thread 1: x.transferTo(1,y) Thread 2: y.transferTo(1,x) 20

Dining Philosopher’s Problem 5 Philosopher’s eating rice around a table one chopstick to the left and right of each first grab the one on your left, then on your right… 21

Deadlock = Cycles Multiple threads depending on each other in a cycle T2 has lock that T1 needs T3 has lock that T2 needs T1 has lock that T3 needs Solution? T1 T2 T3 Solution is to avoid creating cycles in the first place! 22

How to Fix Deadlock? In Banking example class BankAccount { … synchronized void withdraw(int amt) {…} synchronized void deposit(int amt) {…} synchronized void transferTo(int amt, BankAccount a) { this.withdraw(amt); a.deposit(amt); } 23

How to Fix Deadlock? Separate withdraw from deposit Problems? class BankAccount { … synchronized void withdraw(int amt) {…} synchronized void deposit(int amt) {…} synchronized void transferTo(int amt, BankAccount a) { this.withdraw(amt); a.deposit(amt); } 24

Possible Solutions transferTo not synchronized exposes intermediate state after withdraw before deposit may be okay here, but exposes wrong total amount in bank Coarsen lock granularity: one lock for each pair of accounts allowing transfers between them works, but sacrifices concurrent deposits/withdrawals Give every bank-account a unique ID and always acquire locks in the same ID order Entire program should obey this order to avoid cycles 25

Ordering Accounts Transfer from bank account 5 to account 9 A5 A9 lock A5 lock A9 withdraw from A5 deposit to A9 A5 A9 26

Ordering Accounts Transfer from bank account 5 to account 9 lock A5 lock A9 withdraw from A5 deposit to A9 Transfer from bank account 9 to account 5 lock withdraw from deposit to A5 A9 A5 A9 27

Ordering Accounts Transfer from bank account 5 to account 9 lock A5 lock A9 withdraw from A5 deposit to A9 Transfer from bank account 9 to account 5 lock withdraw from deposit to A5 A9 A5 A9 No interleavings will produce deadlock! T1 cannot block on A9 until it has A5 T2 cannot acquire A9 until it has A5 28

Banking Without Deadlocks class BankAccount { … private int acctNumber; // must be unique void transferTo(int amt, BankAccount a) { if(this.acctNumber < a.acctNumber) synchronized(this) { synchronized(a) { this.withdraw(amt); a.deposit(amt); }} else } 29

Lock Ordering Useful in many situations Doesn’t always work e.g., when moving an item from work queue A to B, need to acquire locks in a particular order Doesn’t always work not all objects can be naturally ordered Java StringBuffer append is subject to deadlocks thread 1: append string A onto string B thread 2: append string B onto string A 30

Locking a Hashtable Consider a hashtable with many simultaneous lookup operations rare insert operations What’s the right locking strategy? 31

Read vs. Write Locks Recall race conditions two simultaneous write to same location one write, one simultaneous read But two simultaneous reads OK Synchronize is too strict blocks simultaneous reads 32

Readers/Writer Locks 0  writers  1 0  readers writers*readers==0 A new synchronization ADT: The readers/writer lock A lock’s states fall into three categories: “not held” “held for writing” by one thread “held for reading” by one or more threads new: make a new lock, initially “not held” acquire_write: block if currently “held for reading” or “held for writing”, else make “held for writing” release_write: make “not held” acquire_read: block if currently “held for writing”, else make/keep “held for reading” and increment readers count release_read: decrement readers count, if 0, make “not held” 0  writers  1 0  readers writers*readers==0 33

In Java Java’s synchronized statement does not support readers/writer Instead, library java.util.concurrent.locks.ReentrantReadWriteLock Different interface: methods readLock and writeLock return objects that themselves have lock and unlock methods 34

Concurrency Summary Parallelism is powerful, but introduces new concurrency issues: Data races Interleaving Deadlocks Requires synchronization Locks for mutual exclusion Guidelines for correct use help avoid common pitfalls 35