COMP 111 Threads and concurrency Sept 28, 2005. Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Synchronization NOTE to instructors: it is helpful to walk through an example such as readers/writers locks for illustrating the use of condition variables.
Operating Systems Part III: Process Management (Process Synchronization)
Process Synchronization A set of concurrent/parallel processes/tasks can be disjoint or cooperating (or competing) With cooperating and competing processes.
PROCESS SYNCHRONIZATION READINGS: CHAPTER 5. ISSUES IN COOPERING PROCESSES AND THREADS – DATA SHARING Shared Memory Two or more processes share a part.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
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.
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.
1 Mutual Exclusion: Primitives and Implementation Considerations.
Race Conditions. Isolated & Non-Isolated Processes Isolated: Do not share state with other processes –The output of process is unaffected by run of other.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Synchronization Andy Wang Operating Systems COP 4610 / CGS 5765.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
9/8/2015cse synchronization-p1 © Perkins DW Johnson and University of Washington1 Synchronization Part 1 CSE 410, Spring 2008 Computer Systems.
1 Thread Synchronization: Too Much Milk. 2 Implementing Critical Sections in Software Hard The following example will demonstrate the difficulty of providing.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
CSCI-375 Operating Systems Lecture Note: Many slides and/or pictures in the following are adapted from: slides ©2005 Silberschatz, Galvin, and Gagne Some.
1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CPS110: Thread cooperation Landon Cox. Constraining concurrency  Synchronization  Controlling thread interleavings  Some events are independent  No.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Concurrency: Locks and synchronization Slides by Prof. Cox.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
6/27/20161 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam King,
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Atomic Operations in Hardware
Atomic Operations in Hardware
Chapter 5: Process Synchronization
Andy Wang Operating Systems COP 4610 / CGS 5765
Jonathan Walpole Computer Science Portland State University
Background and Motivation
Implementing Mutual Exclusion
Concurrency: Mutual Exclusion and Process Synchronization
CSCI1600: Embedded and Real Time Software
Implementing Mutual Exclusion
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
CSCI1600: Embedded and Real Time Software
CSE 542: Operating Systems
CSE 542: Operating Systems
CPS110: Thread cooperation
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

COMP 111 Threads and concurrency Sept 28, 2005

Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant professor in CS Complete Ph.D. in 2003 at University of Texas Grew up in Boston Research area: Compilers Mostly, new applications and algorithms

Tufts University Computer Science3 My research Compilers are powerful tools Performance improvement Through program transformation Better cooperation with run-time system, OS Error checking Analyze program behavior Try to predict errors – eg, null pointer dereference Try to identify security holes – eg, buffer overrun This problem is wicked hard

Tufts University Computer Science4 Today: Threads What are they? Why use ‘em? Problems – synchronization Wait, why are we using them? Off to Dr. Couch’s OneNote presentation…

Tufts University Computer Science5 Why use threads? Hide latency Don’t wait for disk access, network transfers, etc Less overhead than processes Easier for threads to cooperate than processes Schedule on multiprocessors True parallelism Near future: all processors will have multiple cores Great, threads are easy, right? Not so fast…

Tufts University Computer Science6 Synchronization – Motivation “The too much milk problem” Example of need to synchronize activities Thanks to Emery Berger UMass Amherst

Tufts University Computer Science7 Example Consider following routine: int x = 0; void *threaded_routine (void * v) { const int *n = (int *)v; int i; for (i=0; i<10; i++) { int y=x; y++; printf("%d: y=%d\n",*n,y); sleep(1); x=y; printf("%d: x=%d\n",*n,x); } Increment the value of x by one

Tufts University Computer Science8 Multithreaded Run three instances Should produce “30” – three threads increment the value 10 times How can we fix this?

Tufts University Computer Science9 Solving the Too Much Milk Problem Correctness properties Only one person buys milk Safety: “nothing bad happens” Someone buys milk if you need to Progress: “something good eventually happens” Add some synchronization protocol: “Leave a note” (lock) “Remove a note” (unlock) “Don’t buy milk if there’s a note” (wait)

Tufts University Computer Science10 Too Much Milk: Solution 1 thread A if (no milk && no note) leave note buy milk remove note thread B if (no milk && no note) leave note buy milk remove note Does this work? too much milk

Tufts University Computer Science11 Too Much Milk: Solution 2 thread A leave note A if (no note B) if (no milk) buy milk remove note A thread B leave note B if (no note A) if (no milk) buy milk remove note B Idea: use labeled notes oops – no milk

Tufts University Computer Science12 Too Much Milk: Solution 3 thread A leave note A while (note B) do nothing if (no milk) buy milk remove note A thread B leave note B if (no note A) if (no milk) buy milk remove note B Idea: wait for the right note Must try all possibilities to verify

Tufts University Computer Science13 Too Much Milk: Solution 3 thread A leave note A while (note B) do nothing if (no milk) buy milk remove note A thread B leave note B if (no note A) if (no milk) buy milk remove note B Possibility 1: A first, then B OK

Tufts University Computer Science14 Too Much Milk: Solution 3 thread A leave note A while (note B) do nothing if (no milk) buy milk remove note A thread B leave note B if (no note A) if (no milk) buy milk remove note B Possibility 2: B first, then A OK

Tufts University Computer Science15 Too Much Milk: Solution 3 thread A leave note A while (note B) do nothing if (no milk) buy milk remove note A thread B leave note B if (no note A) if (no milk) buy milk remove note B Possibility 3: Interleaved – A waits & buys OK

Tufts University Computer Science16 Too Much Milk: Solution 3 thread A leave note A while (note B) do nothing if (no milk) buy milk remove note A thread B leave note B if (no note A) if (no milk) buy milk remove note B Possibility 4: Interleaved – A waits, B buys OK

Tufts University Computer Science17 Too Much Milk: Solution 3 Solution 3: “Thread A waits for B, otherwise buys” Correct – preserves desired properties Safety: we only buy one milk Progress: we always buy milk But…

Tufts University Computer Science18 Problems with this Solution Complicated Difficult to convince ourselves that it works Asymmetrical Threads A & B are different Adding more threads = different code for each thread Poor utilization Busy waiting – consumes CPU resources, no useful work Possibly non-portable Relies on atomicity of loads & stores

Tufts University Computer Science19 Synchronization Terminology Mutual exclusion (“mutex”) – prevents multiple threads from entering Critical section – code only one thread can execute at a time Race condition – outcome of some code is non-deterministic Lock – mechanism for mutual exclusion Atomic or atomicity – either all completes, or none completes

Tufts University Computer Science20 Locks Provide mutual exclusion to shared data via two atomic routines Lock::Acquire – wait for lock, then take it Lock::Release – unlock, wake up waiters Rules: Acquire lock before accessing shared data Release lock afterwards Lock – initially released

Tufts University Computer Science21 Too Much Milk: Locks thread A Lock.acquire() if (no milk) buy milk Lock.release() thread B Lock.acquire() if (no milk) buy milk Lock.release()

Tufts University Computer Science22 With locks… int x=0; void *threaded_routine (void * v) { const int *n = (int *)v; int i; for (i=0; i<10; i++) { pthread_mutex_lock(&locker); int y=x; y++; printf("%d: y=%d\n",*n,y); sleep(1); x=y; printf("%d: x=%d\n",*n,x); pthread_mutex_unlock(&locker); }

Tufts University Computer Science23 Locks Now run three threads… What have we accomplished? Made the update of x atomic Clean, symmetric - but how do we implement it?

Tufts University Computer Science24 Implementing Locks Requires hardware support (in general) Can build on atomic operations: Load/Store Disable interrupts Uniprocessors only Test & Set, Compare & Swap

Tufts University Computer Science25 Race conditions In practice: These are the most difficult bugs you will ever see Often occur intermittently Debugging changes the timing, hides the bug What to do? Two neat-o approaches Run-time race detection Compile-time race detection

Tufts University Computer Science26 Summary Communication between threads: via shared variables Critical sections = regions of code that modify or access shared variables Must be protected by synchronization primitives that ensure mutual exclusion Loads & stores: tricky, error-prone Solution: high-level primitives (e.g., locks)