6/27/20161 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam King,

Slides:



Advertisements
Similar presentations
Dependence Precedence. Precedence & Dependence Can we execute a 1000 line program with 1000 processors in one step? What are the issues to deal with in.
Advertisements

CIS 540 Principles of Embedded Computation Spring Instructor: Rajeev Alur
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes.
CSC321 Concurrent Programming: §3 The Mutual Exclusion Problem 1 Section 3 The Mutual Exclusion Problem.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Concurrent Programming Problems OS Spring Concurrency pros and cons Concurrency is good for users –One of the reasons for multiprogramming Working.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Mutual Exclusion.
Critical Section chapter3.
Race Conditions. Isolated & Non-Isolated Processes Isolated: Do not share state with other processes –The output of process is unaffected by run of other.
1 Friday, June 16, 2006 "In order to maintain secrecy, this posting will self-destruct in five seconds. Memorize it, then eat your computer." - Anonymous.
Concurrency.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Process Synchronization Ch. 4.4 – Cooperating Processes Ch. 7 – Concurrency.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Memory Consistency Models Some material borrowed from Sarita Adve’s (UIUC) tutorial on memory consistency models.
Synchronization Andy Wang Operating Systems COP 4610 / CGS 5765.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
1 Lecture 9: Synchronization  concurrency examples and the need for synchronization  definition of mutual exclusion (MX)  programming solutions for.
1 Thread Synchronization: Too Much Milk. 2 Implementing Critical Sections in Software Hard The following example will demonstrate the difficulty of providing.
CPS110: Intro to processes, threads and concurrency Author: Landon Cox.
28/10/1999POS-A1 The Synchronization Problem Synchronization problems occur because –multiple processes or threads want to share data; –the executions.
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.
Internet Software Development Controlling Threads Paul J Krause.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
11/21/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
Memory Consistency Models. Outline Review of multi-threaded program execution on uniprocessor Need for memory consistency models Sequential consistency.
12/5/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam King,
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Concurrency Properties. Correctness In sequential programs, rerunning a program with the same input will always give the same result, so it makes sense.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
CS533 Concepts of Operating Systems Jonathan Walpole.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Lecture 3 Concurrency and Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
CPS110: Thread cooperation Landon Cox. Constraining concurrency  Synchronization  Controlling thread interleavings  Some events are independent  No.
CPS110: Intro to processes Landon Cox. OS Complexity  Lines of code  XP: 40 million  Linux 2.6: 6 million  (mostly driver code)  Sources of complexity.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Concurrency and Performance Based on slides by Henri Casanova.
Concurrency: Locks and synchronization Slides by Prof. Cox.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Background on the need for Synchronization
Memory Consistency Models
COMPSCI210 Recitation 12 Oct 2012 Vamsi Thummala
Memory Consistency Models
143a discussion session week 3
Andy Wang Operating Systems COP 4610 / CGS 5765
Background and Motivation
Implementing Mutual Exclusion
Implementing Mutual Exclusion
Andy Wang Operating Systems COP 4610 / CGF 5765
Problems with Locks Andrew Whitaker CSE451.
Andy Wang Operating Systems COP 4610 / CGF 5765
Sarah Diesburg Operating Systems COP 4610
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
CPS110: Thread cooperation
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

6/27/20161 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam King, and Andrew S Tanenbaum

Cooperating threads How multiple threads can cooperate on a single task Assume for now that we have enough physical processors for each thread Later we’ll discuss how to give illusion of infinite processors on single processor 6/27/20162

Ordering of events Ordering of events from different threads is non-deterministic Processor speeds may vary E.g., after 10 seconds, different thread have different amounts of work done Thread A > Thread B > Thread C > 6/27/20163

Nondeterminisim Non deterministic ordering produces non deterministic results Printing example Thread A: print ABC Thread B: print 123 Possible outputs? Impossible outputs? Why or why not? What is being shared? 6/27/20164

Arithmetic example Initially y=10 Thread A: x = y+1; Thread B: y = y*2; Possible results? 6/27/20165

Atomic operations Example: Thread A: x=1; Thread B: x=2; Possible results? Is 3 a possible output? 6/27/20166

Non-interference and Atomic operations Non-interference assumption: Before we can reason at all about cooperating threads, we must know that some operations will execute to completion without interference from any other source Non-interference: operation will always return result as if it were the only operation running Operation that in all circumstances will execute to completion without interference is atomic 6/27/20167

Previous Examples In assignment example above, if assignment to x is atomic, then only possible results are 1 and 2. In print example above, what are the possible output if each print statement is atomic? In print example, assuming printing a char was atomic. What if printing a single char were not atomic? 6/27/20168

Atomicity On most machines, memory load and store are atomic But, many instructions are not atomic Floating point store on 32-bit machine If you don’t have any atomic operations, difficult to make one (bakery algorithm Fortunately, H/W designers have helped us out. 6/27/20169

Another example Thread A Thread B i=0 while(i -10){ i++ i-- } Print “A finished” Print “B finished” Who will win? Is it guaranteed that someone will win? What if threads run at exactly the same speed and start close together? What if i++ and i-- are not atomic? 6/27/201610

I++ I-- not atomic Tmp (private) = I + 1; I = Tmp; (A) TmpA = I + 1 (I.e. 1) (B) TmpB = I - 1 (I.e. -1) (A) I = TmpA (B) I = TmpB 6/27/201611

Another example Should you worry about this happening? Non-deterministic interleaving makes debugging challenging 6/27/201612

Synchronizing multiple threads Must control interleaving between threads Order of some operations irrelevant Independent Other operations are dependent and order does matter All possible interleaving must yield a correct answer A correct concurrent program will work no matter how fast the processors are that execute the various threads 6/27/201613

Synchronizing multiple threads All interleavings result in correct answer Try to constrain the thread executions as little as possible Controlling the execution and order of threads is called “synchronization” 6/27/201614

Too much milk The Gunter household drinks a lot of milk, but has a small fridge Problem: Carl and Elsa want there to always at least one gallon of milk in the fridge for dinner; fridge holds at most two gallons If either sees there is less than one gallon, goes to buy milk Specification: Someone buys milk if running low Never more than two gallons of milk milk 6/27/201615

Solution #0 – no sync Carl Elsa 5:30 Comes home 5:35 Checks milk 5:40 Goes to store 5:45Comes home 5:50 Buys milkChecks milk 5:55 Goes homeGoes to store 6:00 Puts milk in Fridge Buys milk 6:05Comes home 6:10Too much milk! 6/27/201616

Mutual Exclusion Ensure that only 1 thread is doing a certain thing at one time Only one person goes shopping at one time Critical section A section of code that needs to run atomically w.r.t. other code If code A and code B are critical sections w.r.t. each other threads cannot interleave events from A and B Critical sections must be atomic w.r.t. each other Share data (or other resourced, e.g., screen, fridge) What is the critical section in solution #0? 6/27/201617

Too much milk (solution #1) Assume only atomic operations are load and store Idea: leave note that going to check on milk status Carl: if (no note) {if (milk low) {leave note; buy milk; remove note;} Elsa: if (no note) {if (milk low) {leave note; buy milk; remove note;} What can go wrong? Is this better than before? 6/27/201618

Too much milk (solution #2) Idea: Change order of leave note and check milk Carl: if (milk low) {if (no note) {leave note; buy milk; remove note;} Elsa: if (milk low) {if (no note) {leave note; buy milk; remove note;} What can go wrong? Is this better than before? 6/27/201619

Too much milk (solution #3) Idea: Protect more actions with note Carl: if (no note) {leave note; if (milk low) {buy milk}; remove note;} Elsa: if (no note) {leave note; if (milk low) {buy milk}; remove note;} What can go wrong? Is this better than before? 6/27/201620

Too much milk (solution #4) Idea: Change order of leaving note and checking note Carl: leave noteCarl; if (no noteElsa) { if (milk low) {buy milk};} ; remove noteCarl Elsa: leave noteElsa; if (no noteCarl) { if (milk low) {buy milk};} ; remove noteElsa What can go wrong? Is this better than before? 6/27/201621

Too much milk (solution #5) Idea: When both leave note, always give priority to fixed one to buy milk Carl: leave noteCarl; while (noteElsa) {do nothing}; if (milk low) {buy milk}; remove noteCarl Elsa: leave noteElsa; if (no noteCarl) { if (milk low) {buy milk};} ; remove noteElsa Simplified instance of Bakery Algorithm 6/27/201622

6/27/201623

6/27/201624