Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.

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.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Mutual Exclusion.
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
1 Mutual Exclusion: Primitives and Implementation Considerations.
COS 461 Fall 1997 Concurrent Programming u Traditional programs do one thing at a time. u Concurrent programs do several things at once. u Why do this?
CS 162 Discussion Section Week 3. Who am I? Mosharaf Chowdhury Office 651 Soda 4-5PM.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
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.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Threads and Critical Sections Vivek Pai / Kai Li Princeton University.
Synchronization Andy Wang Operating Systems COP 4610 / CGS 5765.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
9/8/2015cse synchronization-p1 © Perkins DW Johnson and University of Washington1 Synchronization Part 1 CSE 410, Spring 2008 Computer Systems.
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.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Games Development 2 Concurrent Programming CO3301 Week 9.
CSE 501N Fall ‘09 23: Advanced Multithreading: Synchronization and Thread-Safety December 1, 2009 Nick Leidenfrost.
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.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
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.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
CPS110: Thread cooperation Landon Cox. Constraining concurrency  Synchronization  Controlling thread interleavings  Some events are independent  No.
CS162 Operating Systems and Systems Programming Lecture 4 Synchronization, Atomic operations, Locks September 10, 2012 Ion Stoica
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
1 Ivan Marsic Rutgers University LECTURE 19: Concurrent Programming.
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: Locks and synchronization Slides by Prof. Cox.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
6/27/20161 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam King,
December 1, 2006©2006 Craig Zilles1 Threads & Atomic Operations in Hardware  Previously, we introduced multi-core parallelism & cache coherence —Today.
29 June 2015 Charles Reiss CS162: Operating Systems and Systems Programming Lecture 5: Basic Scheduling + Synchronization.
CS703 – Advanced Operating Systems
Background on the need for Synchronization
COMPSCI210 Recitation 12 Oct 2012 Vamsi Thummala
CSCI 511 Operating Systems Chapter 5 (Part A) Process Synchronization
CSCI 511 Operating Systems Chapter 5 (Part A) Process Synchronization
Andy Wang Operating Systems COP 4610 / CGS 5765
Chapter 26 Concurrency and Thread
Background and Motivation
Dr. Mustafa Cem Kasapbaşı
Implementing Mutual Exclusion
Slides by Prof. Landon Cox and Vamsi Thummala
Implementing Mutual Exclusion
CSE 451: Operating Systems Autumn 2004 Module 6 Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2004 Module 6 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
CPS110: Thread cooperation
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox

Threads  Light weight  Easy and Fast  Each thread has its own  SP, PC, Registers  But they share  Heap, Code, Library  Slow devices, interactive tasks, prioritization  Server addressing multiple clients

Example  Two threads accessing account balance  Thread 1 deposit money  Thread 2 withdraw money  Initial balance is 100  What is the final balance?

Debugging is Hard  Worst case reasoning  Think of all ways threads can be scheduled  Difficult to test all possible interleavings  Bugs may appear only sometimes  Rerunning the program may make them disappear!

Constraining concurrency  Synchronization  Controlling thread interleavings  Some events are independent  No shared state  Relative order of these events don’t matter  Other events are dependent  Output of one can be input to another  Their order can affect program results

Goals of synchronization 1.All interleavings must give correct result  Correct concurrent program  Works no matter how fast threads run  Important for lab3! 2.Constrain program as little as possible  Why?  Constraints slow program down  Constraints create complexity

“Too much milk” principals

“Too much milk” rules  The fridge must be stocked with milk  Milk expires quickly, so never > 1 milk  Landon and Melissa  Can come home at any time  If either sees an empty fridge, must buy milk  Code (no synchronization) if (noMilk){ buy milk; }

Time 3:00Look in fridge (no milk) 3:05Go to grocery store 3:10Look in fridge (no milk) 3:15Buy milk 3:20Go to grocery store 3:25Arrive home, stock fridge 3:30Buy milk 3:35Arrive home, stock fridge Too much milk! Unsynchronized code will break

What broke?  Code worked sometimes, but not always  Code contained a race condition  Processor speed caused incorrect result  First type of synchronization  Mutual exclusion  Critical sections

Synchronization concepts  Mutual exclusion  Ensure 1 thread doing something at a time  E.g. 1 person shops at a time  Code blocks are atomic w/re to each other  Threads can’t run code blocks at same time

Synchronization concepts  Critical section  Code block that must run atomically  “with respect to some other pieces of code”  If A and B are critical w/re to each other  Threads mustn’t interleave code from A and B  A and B mutually exclude each other  Conflicting code is often same block  But executed by different threads  Reads/writes shared data (e.g. screen, fridge)

Back to “Too much milk”  What is the critical section?  Landon and Melissa’s critical sections  Must be atomic w/re to each other if (noMilk){ buy milk; }

“Too much milk” solution 1  Assume only atomic load/store  Build larger atomic section from load/store  Idea: 1.Leave notes to say you’re taking care of it 2.Don’t check milk if there is a note

Solution 1 code  Atomic operations  Atomic load: check note  Atomic store: leave note if (noMilk) { if (noNote){ leave note; buy milk; remove note; }

Does it work? if (noMilk) { if (noNote){ leave note; buy milk; remove note; } } if (noMilk) { if (noNote){ leave note; buy milk; remove note; } } Is this better than no synchronization at all? What if “if” sections are switched?

What broke?  Melissa’s events can happen  After Landon checks for a note  Before Landon leaves a note if (noMilk) { if (noNote){ leave note; buy milk; remove note; }

Next solution  Idea:  Change the order of “leave note”, “check note”  Requires labeled notes (else you’ll see your note)

Does it work? leave noteLandon if (no noteMelissa){ if (noMilk){ buy milk; } remove noteLandon leave noteMelissa if (no noteLandon){ if (noMilk){ buy milk; } remove noteMelissa Nope. (Illustration of “starvation.”)

What about now? while (noMilk){ leave noteLandon if(no noteMelissa){ if(noMilk){ buy milk; } remove noteLandon } while (noMilk){ leave noteMelissa if(no noteLandon){ if(noMilk){ buy milk; } remove noteMelissa } Nope. (Same starvation problem as before)

Next solution  We’re getting closer  Problem  Who buys milk if both leave notes?  Solution  Let Landon hang around to make sure job is done

Too much milk solution leave noteLandon while (noteMelissa){ do nothing } if (noMilk){ buy milk; } remove noteLandon leave noteMelissa if (no noteLandon){ if (noMilk){ buy milk; } remove noteMelissa

Downside of solution  Complexity  Hard to convince yourself it works  Asymmetric  Landon and Melissa run different code  Not clear if this scales to > 2 people  Landon consumes CPU while waiting  Busy-waiting  Note: only needed atomic load/store

Raising the level of abstraction  Locks  Also called mutexes  Provide mutual exclusion  Prevent threads from entering a critical section  Lock operations  Lock (aka Lock::acquire )  Unlock (aka Lock::release )

Lock operations  Lock: wait until lock is free, then acquire it  This is a busy-waiting implementation  We’ll improve on this in a few lectures  Unlock: atomic release lock do { if (lock is free) { acquire lock break } } while (1) Must be atomic with respect to other threads calling this code

Too much milk, solution 2 Why doesn’t the note work as a lock? if (noMilk) { if (noNote){ leave note; buy milk; remove note; } } Block is not atomic. Must atomically check if lock is free grab it

Elements of locking 1.The lock is initially free 2.Threads acquire lock before an action 3.Threads release lock when action completes 4.Lock() must wait if someone else has lock  Key idea  All synchronization involves waiting  Threads are either running or blocked

Too much milk with locks?  Problem?  Waiting for lock while other buys milk lock () if (noMilk) { buy milk } unlock () lock () if (noMilk) { buy milk } unlock ()

Too much milk “w/o waiting”? lock () if (noNote && noMilk){ leave note “at store” unlock () buy milk lock () remove note unlock () } else { unlock () } Only hold lock while handling shared resource. lock () if (noNote && noMilk){ leave note “at store” unlock () buy milk lock () remove note unlock () } else { unlock () } Not holding lock

What about this? lock () if (noMilk && noNote){ leave note “at store” unlock () buy milk stock fridge remove note } else { unlock () } lock () if (noMilk && noNote){ leave note “at store” unlock () buy milk stock fridge remove note } else { unlock () } lock () if (noMilk

What about Java? Too much milk  Every object is a lock  Use synchronized key word  Lock : “{“, unlock: “}” synchronized (obj){ if (noMilk) { buy milk } synchronized (obj){ if (noMilk) { buy milk }

Concurrency in Java  Implement a Runnable Interface  public class MyThread implements Runnable{ public void run(){} }  Subclass Thread  public class MyThread extends Thread{ public void run(){} }

Example