2012 08 07
play

2012-08-07 CSE 332 Data Abstractions: Data Races and Memory, - PDF document

2012-08-07 CSE 332 Data Abstractions: Data Races and Memory, Reordering, Deadlock, Readers/Writer Locks, and Condition Variables (oh my!) *ominous music* THE FINAL EXAM Kate Deibel Summer 2012 August 6, 2012 CSE 332 Data Abstractions,


  1. 2012-08-07 CSE 332 Data Abstractions: Data Races and Memory, Reordering, Deadlock, Readers/Writer Locks, and Condition Variables (oh my!) *ominous music* THE FINAL EXAM Kate Deibel Summer 2012 August 6, 2012 CSE 332 Data Abstractions, Summer 2012 1 August 6, 2012 CSE 332 Data Abstractions, Summer 2012 2 The Final Specific Topics It is next Wednesday, August 15 Although the final is by no means finalized, knowing the following would be good: It will take up the entire class period How to do Big-Oh (yes, again!)  Is it comprehensive? Yes and No  Best and worst case for all data structures and algorithms we covered  Will primarily call upon only what we covered since Sorting algorithm properties (in-place, stable)  the midterm (starting at sorting up through next  Graph representations Monday's lecture on minimum spanning trees) Topological sorting   Still, you will need to understand algorithmic  Dijkstra's shortest-path algorithm analysis, big-Oh, and best/worst-case for any data Parallel Maps and Reductions  structures we have discussed Parallel Prefix, Pack, and Sorting   You will NOT be doing tree or heap manipulations  ForkJoin Library code but you may (i.e., will) do some graph algorithms Key ideas / high-level notions of concurrency  July 11, 2012 CSE 332 Data Abstractions, Summer 2012 3 July 11, 2012 CSE 332 Data Abstractions, Summer 2012 4 Book, Calculator, and Notes The exam is closed book You can bring a calculator if you want You can bring a limited set of notes:  One 3x5 index card (both sides)  Must be handwritten (no typing!)  You must turn in the card with your exam Some horses like wet tracks or dry tracks or muddy tracks… MORE ON RACE CONDITIONS July 11, 2012 CSE 332 Data Abstractions, Summer 2012 5 August 6, 2012 CSE 332 Data Abstractions, Summer 2012 6 1

  2. 2012-08-07 Races Data Races A race condition occurs when the computation result depends on A data race is a type of race condition that can scheduling (how threads are interleaved on ≥1 processors) happen in two ways: Only occurs if T1 and T2 are scheduled in a particular way  Two threads potentially write a variable at the same time   As programmers, we cannot control the scheduling of threads  One thread potentially write a variable while another reads Program correctness must be independent of scheduling  Not a race: simultaneous reads provide no errors Race conditions are bugs that exist only due to concurrency Potentially is important  No interleaved scheduling with 1 thread  We claim that code itself has a data race independent of any Typically, the problem is some intermediate state that "messes particular actual execution up" a concurrent thread that "sees" that state Data races are bad, but they are not the only form of race conditions We will distinguish between data races and bad interleavings, both of which are types of race condition bugs  We can have a race, and bad behavior, without any data race August 6, 2012 CSE 332 Data Abstractions, Summer 2012 7 August 6, 2012 CSE 332 Data Abstractions, Summer 2012 8 Stack Example A Race Condition: But Not a Data Race In a sequential world, class Stack<E> { class Stack<E> { this code is of iffy, private E[] array = (E[])new Object[SIZE]; … ugly, and questionable synchronized boolean isEmpty() {…} int index = -1; synchronized void push(E val) {…} style , but correct synchronized boolean isEmpty() { synchronized E pop(E val) {…} return index==-1; } E peek() { synchronized void push(E val) { The "algorithm" is the E ans = pop(); array[++index] = val; only way to write a push(ans); } peek helper method if return ans; synchronized E pop() { } if(isEmpty()) this interface is all you throw new StackEmptyException(); have to work with return array[index--]; Note that peek() throws } the StackEmpty exception } via its call to pop() August 6, 2012 CSE 332 Data Abstractions, Summer 2012 9 August 6, 2012 CSE 332 Data Abstractions, Summer 2012 10 peek in a Concurrent Context Example 1: peek and isEmpty peek has no overall effect on the shared data Property we want:  It is a "reader" not a "writer" If there has been a push (and no pop) ,  State should be the same after it executes as before then isEmpty should return false This implementation creates an inconsistent With peek as written, property can be intermediate state violated – how?  Calls to push and pop are synchronized,so there are no data races on the underlying array Thread 1 ( peek ) Thread 2  But there is still a race condition E ans = pop(); push(x) Time  This intermediate state boolean b = isEmpty() E peek() { should not be exposed push(ans); E ans = pop();  Leads to several push(ans); return ans; bad interleavings return ans; } August 6, 2012 CSE 332 Data Abstractions, Summer 2012 11 August 6, 2012 CSE 332 Data Abstractions, Summer 2012 12 2

  3. 2012-08-07 Example 1: peek and isEmpty Example 2: peek and push Property we want: Property we want: If there has been a push (and no pop) , Values are returned from pop in LIFO order then isEmpty should return false With peek as written, property can be Race causes error with: With peek as written, property can be T2: push(x) violated – how? T1: pop() violated – how? T2: isEmpty() Thread 1 ( peek ) Thread 1 ( peek ) Thread 2 Thread 2 E ans = pop(); push(x) E ans = pop(); push(x) Time Time boolean b = isEmpty() push(y) push(ans); push(ans); E e = pop() return ans; return ans; August 6, 2012 CSE 332 Data Abstractions, Summer 2012 13 August 6, 2012 CSE 332 Data Abstractions, Summer 2012 14 Example 2: peek and push Example 3: peek and peek Property we want: Property we want: peek does not throw an exception unless Values are returned from pop in LIFO order the stack is empty Race causes error with: With peek as written, property can be T2: push(x) T1: pop() With peek as written, property can be violated – how? T2: push(x) violated – how? T1: push(x) Thread 1 ( peek ) Thread 1 ( peek ) Thread 2 Thread 2 E ans = pop(); push(x) E ans = pop(); E ans = pop(); Time Time push(y) push(ans); E e = pop() push(ans); push(ans); return ans; return ans; return ans; August 6, 2012 CSE 332 Data Abstractions, Summer 2012 15 August 6, 2012 CSE 332 Data Abstractions, Summer 2012 16 The Fix An Incorrect "Fix" peek needs synchronization to disallow interleavings So far we have focused on problems created when peek performs writes that lead to an incorrect  The key is to make a larger critical section intermediate state  This protects the intermediate state of peek  Use re-entrant locks; will allow calls to push and pop A tempting but incorrect perspective  Can be done in stack (left) or an external class (right)  If an implementation of peek does not write anything, class Stack<E> { class C { then maybe we can skip the synchronization? … <E> E myPeek(Stack<E> s){ synchronized E peek(){ synchronized (s) { Does not work due to data races with push and pop E ans = pop(); E ans = s.pop(); push(ans); s.push(ans);  Same issue applies with other readers, such as isEmpty return ans; return ans; } } } } } August 6, 2012 CSE 332 Data Abstractions, Summer 2012 17 August 6, 2012 CSE 332 Data Abstractions, Summer 2012 18 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend