discussion week 3 - ucsb computer science departmentkyledewey/cs170/week3/week_3_discussi… ·...
TRANSCRIPT
![Page 1: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/1.jpg)
Discussion Week 3TA: Kyle Dewey
Monday, October 10, 11
![Page 2: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/2.jpg)
Overview
• Concurrency overview
• Synchronization primitives
• Semaphores
• Locks
• Conditions
• Project #1
Monday, October 10, 11
![Page 3: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/3.jpg)
Concurrency
• Looks easy
• Really hard to get right
• Really hard
• No seriously, borderline impossible
Monday, October 10, 11
![Page 4: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/4.jpg)
Race Condition
• Different results are possible based on different process/thread orderings
• Ordering may be correct 99.999% of the time
Monday, October 10, 11
![Page 5: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/5.jpg)
Deadlock
• Two processes/threads wait for each other to do something
• While they wait, they do not do whatever it is they are waiting for
• Potential outcome of a race condition
Monday, October 10, 11
![Page 6: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/6.jpg)
(Sort of) Real Deadlock Example
Monday, October 10, 11
![Page 7: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/7.jpg)
Critical Region
• A point in code where the ordering matters
• Almost always this is some state that is shared between processes/threads
Clientconnect to server:port1connect to server:port2do something with both
Serveraccept from port1accept from port2
do something with both
Monday, October 10, 11
![Page 8: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/8.jpg)
Fixing the Problem
• Do not share state
• Only share read-only state
• Carefully regulate write access to shared state
Monday, October 10, 11
![Page 9: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/9.jpg)
Regulation
• A critical region can be manipulated by only one thread at a time
• Need a way to enforce that at most one thread at any time point is in such a region
Monday, October 10, 11
![Page 10: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/10.jpg)
Solving in Java
• Java provides the synchronized keyword for blocks
• Only one thread at a time may access a block marked with the synchronized keyword
int x = 0;public synchronized void set( int y ) {x = y;}public int get() {return x;}
Monday, October 10, 11
![Page 11: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/11.jpg)
Who cares about Java?
• Many concurrency primitives work exactly like this, just with a little more work
• One call upon entrance to critical region, another upon exit
• The entrance and exit are implicit through blocks with Java
Monday, October 10, 11
![Page 12: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/12.jpg)
Semaphores
• Simply a shared integer
• One call decrements, another increments
• By convention, 0 is locked, and values > 0 are unlocked
• Values < 0 mean?
Monday, October 10, 11
![Page 13: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/13.jpg)
Semaphores
• Increment/decrement are atomic - they are uninterruptible
• The highest possible number it can hold is equal to the max number of callers to the region it protects
Monday, October 10, 11
![Page 14: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/14.jpg)
Usage Example
Monday, October 10, 11
![Page 15: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/15.jpg)
Fix the Notebook Problem
Monday, October 10, 11
![Page 16: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/16.jpg)
NACHOS Semaphore Methods
• P(): wait until the value is > 0, then decrement
• V(): increment the value, waking up any waiting threads
Monday, October 10, 11
![Page 17: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/17.jpg)
NACHOS Semaphore Implementation
Monday, October 10, 11
![Page 18: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/18.jpg)
Spinlock
• Alternative to blocking
• A.K.A. busy waiting
• “Spin” in a tight loop
• More efficient for short critical regions
Monday, October 10, 11
![Page 19: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/19.jpg)
Everything In Between
• May spinlock under certain conditions
• May schedule differently if in a locked state
• Implementation can do whatever it wants
Monday, October 10, 11
![Page 20: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/20.jpg)
Project 1 Task 1
• Experiment according to instructions
• Explain the execution of multithreaded code
• Add semaphores and contrast the difference
Monday, October 10, 11
![Page 21: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/21.jpg)
Project 1 Task 2
• Implement locks - essentially semaphores with a maximum of one caller at a time
• Given all the semaphore code to look at
• Hint hint it is a special case of a semaphore
Monday, October 10, 11
![Page 22: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/22.jpg)
Lock Methods
• Acquire(): calling thread waits until lock is available, then grabs the lock
• Release(): calling threads gives up the lock
Monday, October 10, 11
![Page 23: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/23.jpg)
Lock vs. Semaphore
• Locks permit at most one thread in a region, not n
• Locks make sure that only the thread that grabs the lock can release the lock
Monday, October 10, 11
![Page 24: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/24.jpg)
Lock Example
Monday, October 10, 11
![Page 25: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/25.jpg)
Project 1 Task 3
• Implement conditions
• Requires a correct Lock implementation
Monday, October 10, 11
![Page 26: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/26.jpg)
Conditions
• Allow a group of threads to synchronize on a given condition
• Until the condition is true, they wait
Monday, October 10, 11
![Page 27: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/27.jpg)
Condition Methods
• Wait( lock ): release the given lock, wait until signaled, and acquire the lock
• Signal( lock ): wake up any single thread waiting on the condition
• Broadcast( lock ): wake up all threads waiting on the condition
Monday, October 10, 11
![Page 28: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/28.jpg)
Condition Semantics
• The lock should be owned by the calling thread
• Only reason why the reference implementation’s Signal() and Broadcast() needs the lock
• Signal() and Broadcast() require that the lock is currently held
Monday, October 10, 11
![Page 29: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/29.jpg)
Condition Example - Broadcast
Monday, October 10, 11
![Page 30: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/30.jpg)
Condition Example - Signal
Monday, October 10, 11
![Page 31: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/31.jpg)
Project 1 Task 4
• Identify and describe a race condition in a given section of code
• Fix the race condition using semaphores
• Fix it another way using locks and/or conditions
Monday, October 10, 11
![Page 32: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/32.jpg)
Identifying Race Conditions
• NACHOS is more or less deterministic
• Some of the hardest errors to find
Monday, October 10, 11
![Page 33: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/33.jpg)
Project Tips
• Start early
• Use the given implementation as a guide
• Overcomplicated
• Buggy
• Ugly
• The Print() method is a lifesaver
Monday, October 10, 11
![Page 34: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/34.jpg)
FAQ
Monday, October 10, 11
![Page 35: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/35.jpg)
“What’s the difference?”
• Not much
• Possible to implement some in terms of others
• Some may be more natural in different contexts
Monday, October 10, 11
![Page 36: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/36.jpg)
“Are these even working?”
• If everything is done correctly, the output remains the same for first task
• NACHOS thread scheduler is simple
• No interrupts
• All threads are part of the same program
Monday, October 10, 11
![Page 37: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/37.jpg)
“Why bother?”
• Change any of the aforementioned things, and it will matter big time
• Later projects will need this for correctness
• Gentle introduction to concurrency and synchronization primitives
Monday, October 10, 11
![Page 38: Discussion Week 3 - UCSB Computer Science Departmentkyledewey/cs170/week3/week_3_discussi… · Race Condition • Different results are possible based on different process/thread](https://reader031.vdocuments.us/reader031/viewer/2022022512/5ae60ea97f8b9acc268ce31d/html5/thumbnails/38.jpg)
“Conditions make no sense!”
• Name most people are used to: monitors
• http://www.java-samples.com/showtutorial.php?tutorialid=306 has an excellent example of usage (Java standpoint. Examples were adapted from this.)
Monday, October 10, 11