Slide design: Dr. Mark L. Hornick

Slides:



Advertisements
Similar presentations
Chapter 6: Process Synchronization
Advertisements

Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Concurrency 101 Shared state. Part 1: General Concepts 2.
Week 9, Class 3: Model-View-Controller Final Project Worth 2 labs Happens-Before ( SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
“THREADS CANNOT BE IMPLEMENTED AS A LIBRARY” HANS-J. BOEHM, HP LABS Presented by Seema Saijpaul CS-510.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
Today Quiz Multithreading Options Qt socket buffers between threads Quiz today & Tuesday in lab SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Consider the program fragment below left. Assume that the program containing this fragment executes t1() and t2() on separate threads running on separate.
Multiprocessor Cache Consistency (or, what does volatile mean?) Andrew Whitaker CSE451.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Today Return Quiz First release of final project template Multithreading Options Qt socket buffers between threads Tomorrow: Quiz Interthread communication.
Today Final Project “Preview” Real-time concurrency Multithreading Options pthreads qthreads GPIO.h & GPIO.cpp Quiz tomorrow & Tuesday in lab SE-2811 Slide.
Today Return Quiz First true release of final project (Lab 8) Multithreading Options Qt socket buffers between threads SE-2811 Slide design: Dr. Mark L.
Week 9, Class 3: Java’s Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
Today Quiz not yet graded Final report requirements posted More on Multithreading Happens-Before in Java SE-2811 Slide design: Dr. Mark L. Hornick Content:
Today Signals Nyquist Theorem (revisited) Audio Sampling Latency and Bandwidth Scheduling Theory SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Today Quiz not yet graded (Sorry!) More on Multithreading Happens-Before in Java Other langauges with Happens-Before Happens-Before in C++ SE-2811 Slide.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Week 8, Class 3: Model-View-Controller Final Project Worth 2 labs Cleanup of Ducks Reducing coupling Finishing FactoryMethod Cleanup of Singleton SE-2811.
SE3910 Week 8, Class 3 Week 4 Lab: Please return your graded Lab 4 to me so I can enter it in my gradebook Week 9 Lab: Individual demos of working sub-modules.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Java Thread Programming
Slide design: Dr. Mark L. Hornick
CS703 – Advanced Operating Systems
Process Synchronization: Semaphores
Background on the need for Synchronization
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
Memory Consistency Models
Critical sections, locking, monitors, etc.
Memory Consistency Models
Other Important Synchronization Primitives
Lecture 5: GPU Compute Architecture
Threads and Memory Models Hal Perkins Autumn 2011
Lecture 5: GPU Compute Architecture for the last time
Synchronization Lecture 23 – Fall 2017.
Lecture 14: Pthreads Mutex and Condition Variables
Threads and Memory Models Hal Perkins Autumn 2009
Multithreading.
Lecture 2 Part 2 Process Synchronization
Dr. Mustafa Cem Kasapbaşı
Concurrency: Mutual Exclusion and Process Synchronization
CSCI1600: Embedded and Real Time Software
Monitors Chapter 7.
Lecture 14: Pthreads Mutex and Condition Variables
Slide design: Dr. Mark L. Hornick
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Slide design: Dr. Mark L. Hornick
Week 8, Class 3: Model-View-Controller
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
CSCI1600: Embedded and Real Time Software
Slide design: Dr. Mark L. Hornick
Problems with Locks Andrew Whitaker CSE451.
“The Little Book on Semaphores” Allen B. Downey
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

Slide design: Dr. Mark L. Hornick SE3910 5/5/2018 SE3910 Week 9, Class 1 Today Multithreading in Sequence Diagrams 17q3: 1-2,4,6-7,10-15,17,22,26-36 Print slides 20,21 on half sheets SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder Dr. Josiah Yoder

Multithreaded Prime App Revisited (See code) Dr.Yoder

Multitheading in Sequence Diagrams 5/5/2018 Multitheading in Sequence Diagrams Multithreaded Prime App SE-2811 Dr.Yoder Dr. Josiah Yoder

Multitheading in Sequence Diagrams 5/5/2018 Multitheading in Sequence Diagrams Multithreaded Prime App Can also use par tag SE-2811 Dr.Yoder Dr. Josiah Yoder

Slide design: Dr. Mark L. Hornick SE3910 5/5/2018 SE3910 Week 9, Class 2 Today Multithreading tools Multithreading styles 17q3: 1-2,4,6-7,10-15,17,22,26-36 Print slides 20,21 on half sheets SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder See Laplante&Ovaska 4th Ed, Sec 3.3, pp. 106-118 Dr. Josiah Yoder

Multithreading Tools Mailboxes Semaphores Monitors Single variable Queue – Qt’s signal-slots are queued mailboxes with an event loop on the receiving end Semaphores Mutex locks (binary semaphore) Counting semaphore Java synchronization – reentry mutexes Monitors Code region protected by semaphore See Laplante&Ovaska 4th Ed, Sec 3.3, pp. 106-118

Multithreading Tools (2) Signals signal() – OS event handling Hardware event handling Message passing Error handling Similar to event handling in Swing Create a handler Register it to the event Handler will be called if the event occurs Observer pattern! SE-2811 Dr.Yoder

Multithreading Tools (3) SE3910 5/5/2018 Multithreading Tools (3) Shared memory Busy-wait loops Data structures (queues, lists) Memory Barriers Instruction read/writes that avoid caching issues volatile in Java std::atomic<…> in C++ 11 C++11: volatile only for hardware access (Wikipedia) These are low-level techniques. These MUST be used by the higher-level techniques in their implementations But they can also be used alone … with much practice, analysis, and testing! SE-2811 Dr.Yoder Dr. Josiah Yoder

Semaphores Provides: Acquire – obtain a lock on a resource 5/5/2018 Semaphores Provides: Acquire – obtain a lock on a resource Release – release a lock on a resource Can allow multiple locks up to some fixed amount – e.g. to count how many processes have requested to use a socket SE-2811 Dr.Yoder Dr. Josiah Yoder

Monitors A monitor is a section of code protected by a lock. 5/5/2018 Monitors A monitor is a section of code protected by a lock. e.g. the synchronize statement in Java e.g. can implement with semaphore e.g. lock/unlock semantics in C/C++ pthreads, etc. SE-2811 Dr.Yoder Dr. Josiah Yoder

Signals Essentially a message between different parts of a program 5/5/2018 Signals Essentially a message between different parts of a program Implemented in Java: notify/wait Can implement with semaphores: Start with semaphore locked To wait, try to lock on the semaphore To release, unlock the semaphore SE-2811 Dr.Yoder Dr. Josiah Yoder

Mailboxes A mailbox provides: send: Send a message to another thread 5/5/2018 Mailboxes A mailbox provides: send: Send a message to another thread receive: Get the message. Block until a message is ready Rather like TCP. SE-2811 Dr.Yoder Dr. Josiah Yoder

Multithreading risks (See also Dr. Schilling’s slide) Basic risk: Race Conditions Locking risks: Deadlock Starvation Priority Inversion Queuing risk: Timing guarantees SE-2811 Dr.Yoder

Java Multithreading synchronized(this) { …} notify/wait volatile Mutex Lock / Binary semaphore notify/wait signal where receiver locked on mutex lock volatile memory barrier SE-2811 Dr.Yoder

Slide design: Dr. Mark L. Hornick SE3910 5/5/2018 SE3910 Week 9, Class 3 Today Project note volatile keyword (continued) 17q3: 1-2,4,6-7,10-15,17,22,26-36 Print slides 20,21 on half sheets SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder See Laplante&Ovaska 4th Ed, Sec 3.3, pp. 106-118 Dr. Josiah Yoder

Sound card note When playing sound in the VM, configure the VM to use Windows DirectSound (the default) rather than the hardware. Windows pays attention to the sampling frequency you set. The hardware just assumes some fixed frequency (44KHz if I remember what Dr. Schilling said correctly) speaker-test will play white-noise to test if you can hear anything

Slide design: Dr. Mark L. Hornick SE3910 5/5/2018 Volatile http://www.brandsoasis.com/bo3/en/volatile http://link-designs.deviantart.com/art/Warning-Extremely-Volatile-403315705 http://www.emcdda.europa.eu/publications/drug-profiles/volatile http://www.stormthetest.com/word-of-the-day-volatile/ http://world-worst-disasters.blogspot.com/2010/11/volcano-death-toll-rises-past-320.html https://eagleman6788.wordpress.com/tag/explosive-anger/ SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder Dr. Josiah Yoder

Volatile readily vaporizable at a relatively low temperature SE3910 5/5/2018 Volatile readily vaporizable at a relatively low temperature flying or having the power to fly lighthearted, lively tending to erupt into violence : explosive unable to hold the attention fixed because of an inherent lightness or fickleness of disposition difficult to capture or hold permanently http://www.stormthetest.com/word-of-the-day-volatile/ SE-2811 Dr.Yoder Dr. Josiah Yoder

SE3910 5/5/2018 Based on these definitions, which do you think is a closer fit for volatile? A variable which is cached – at any time, value could be incorrect because other threads change it. A variable which is not cached. Must always go to main memory to read value, because other threads may change it at any time. Note: I am NOT asking which is volatile in Java or other languages. SE-2811 Dr.Yoder Dr. Josiah Yoder

SE3910 5/5/2018 Volatile (in Java) A variable which is not cached. Must always go to main memory to read or write the value Why might these variables be called volatile? Why might we want a volatile variable? SE-2811 Dr.Yoder Dr. Josiah Yoder

SE3910 5/5/2018 Volatile (in C) https://en.wikipedia.org/wiki/Volatile_(computer_programming)#Optimization_comparison_in_C Volatile prevents certain compiler optimizations that prevent writes to memory from actually occurring Historic keyword – predates multicore SE-2811 Dr.Yoder Dr. Josiah Yoder

Volatile in C++11 volatile keyword exists, but does not guarantee multithread cache consistency Instead, use std::atomic<…> See example on next slide SE-2811 Dr.Yoder

Volatile in C++11 http://en.cppreference.com/w/cpp/atomic/memory_order SE3910 5/5/2018 Volatile in C++11 #include <thread> #include <atomic> #include <cassert> #include <string> std::atomic<std::string*> ptr; int data; void producer() { std::string* p = new std::string("Hello"); data = 42; ptr.store(p, std::memory_order_release); } void consumer() { std::string* p2; while (!(p2 = ptr.load(std::memory_order_acquire))) /* do nothing */; assert(*p2 == "Hello"); // never fires assert(data == 42); // never fires } int main() std::thread t1(producer); std::thread t2(consumer); t1.join(); t2.join(); http://en.cppreference.com/w/cpp/atomic/memory_order SE-2811 Dr.Yoder Dr. Josiah Yoder

We need "Sequential Consistency" (Review) 5/5/2018 We need "Sequential Consistency" (Review) A program is sequentially consistent if it behaves as if every read is immediately visible on every other thread, in the program order. The “hop-back-and-forth between threads” model https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.4 SE-2811 Dr.Yoder Dr. Josiah Yoder

Total and Partial Order SE2811 5/5/2018 Total and Partial Order Each thread has a Total Order Without synchronize or volatile, actions are concurrent ? In a partial order, some “happens-before” relationships are specified, but not all. In a total order, all “happens-before” relationships are specified. (So there is only one way through the program) For any given multithreaded program, different total orders are possible. But from a synchronization perspective, each thread has only one intra-thread total order. This is the “Program order” of that thread. SE-2811 Dr.Yoder Dr. Josiah Yoder

Total and Partial Order SE2811 5/5/2018 Total and Partial Order Partial Order One Total Order In a partial order, some “happens-before” relationships are specified, but not all. In a total order, all “happens-before” relationships are specified. (So there is only one way through the program) SE-2811 Dr.Yoder Dr. Josiah Yoder

Total and Partial Order SE2811 5/5/2018 Total and Partial Order Partial Order One Total Order In a partial order, some “happens-before” relationships are specified, but not all. In a total order, all “happens-before” relationships are specified. (So there is only one way through the program) SE-2811 Dr.Yoder Dr. Josiah Yoder

Total and Partial Order SE2811 5/5/2018 Total and Partial Order Partial Order Another Total Order In a partial order, some “happens-before” relationships are specified, but not all. In a total order, all “happens-before” relationships are specified. (So there is only one way through the program) For any given multithreaded program, different total orders are possible when the program runs. These are called the “Execution order” But from a synchronization perspective, each thread has only one intra-thread total order. SE-2811 Dr.Yoder Dr. Josiah Yoder

Total and Partial Order SE2811 5/5/2018 Total and Partial Order Partial Order Another Total Order In a partial order, some “happens-before” relationships are specified, but not all. In a total order, all “happens-before” relationships are specified. (So there is only one way through the program) For any given multithreaded program, different total orders are possible when the program runs. These are called the “Execution order” But from a synchronization perspective, each thread has only one intra-thread total order. SE-2811 Dr.Yoder Dr. Josiah Yoder

Happens-Before A guarantee that code is executed in a logical order despite Instruction reordering Cache consistency issues SE-2811 Dr. Yoder

Languages using Happens-Before SE3910 5/5/2018 Languages using Happens-Before Java C++11 Golang LLVM (Technically not a language, but a compiler infrastructure, but it does include an intermediate representation language) http://preshing.com/20130702/the-happens-before-relation/ SE-2811 Dr.Yoder Dr. Josiah Yoder

Happens-Before in C++11 Some are transitive SE3910 5/5/2018 Happens-Before in C++11 Some are transitive “If we ignore consume operations and the dependency-ordered-before relation in C++11, the remaining forms of happens-before end up being transitive.” (Preshing) http://preshing.com/20130702/the-happens-before-relation/ http://en.cppreference.com/w/cpp/atomic/memory_order http://en.cppreference.com/w/cpp/language/eval_order Specifications are notoriously difficult to understand, even the most precise ones. This question clarifies exactly where the C++11 defines what happens-before means. http://stackoverflow.com/questions/34244647/what-does-happens-before-mean-in-the-c11-spec The asker is confused because they say A is true if B is true instead of A is true if and only if B is true. Important to our current discussion: I don’t have access to the C++11 specification. It is not an open standard. You have to purchase a copy, which I have not done. (or at least it doesn’t pop up on Google) SE-2811 Dr.Yoder Dr. Josiah Yoder

Queues require mutex locks What if both of these are called at the same time on different threads? queue.push(); queue.pop(); … but not always… SE-2811 Dr.Yoder

An alternative: java.util.concurrent SE2811 5/5/2018 An alternative: java.util.concurrent Lock-free multi-threaded data structures: ConcurrentHashMap Like HashMap, only “concurrent” ConcurrentSkipListMap Like TreeMap, only “concurrent” Discussed in Dean & Dean SE1011 book recommended by a student for studying data structures Instead of Collections.synchronizedMap (or something like that_ use http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListMap.html or http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html SE-2811 Dr. Yoder Dr. Josiah Yoder

Week 10, Class 2 Final report due Friday, 11pm SE-2811 Dr.Yoder

Slide design: Dr. Mark L. Hornick References EB: Derek Malloy, Exploring Beaglebone, Wiley, 2015 Laplante and Ovaska, Real-Time Systems Design and Analysis, Fourth Edition, Wiley, 2012 SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder