Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Similar documents
CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Operating Systems. Synchronization

CS 153 Design of Operating Systems Winter 2016

Lecture 5: Synchronization w/locks

Synchronization I. Jo, Heeseung

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

CSE 153 Design of Operating Systems

Dealing with Issues for Interprocess Communication

CSE 153 Design of Operating Systems Fall 2018

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

Review: Easy Piece 1

CSE 451: Operating Systems Winter Synchronization. Gary Kimura

Synchronization Principles

CS533 Concepts of Operating Systems. Jonathan Walpole

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

CMSC421: Principles of Operating Systems

Operating Systems. Sina Meraji U of T

Systèmes d Exploitation Avancés

CS 153 Design of Operating Systems Winter 2016

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

Concurrency: Mutual Exclusion (Locks)

Advanced Operating Systems (CS 202) Memory Consistency, Cache Coherence and Synchronization

Concurrency: Locks. Announcements

Synchronization Spinlocks - Semaphores

Chapter 6: Process Synchronization

Problem Set 2. CS347: Operating Systems

Advanced Operating Systems (CS 202) Memory Consistency, Cache Coherence and Synchronization

Chapter 6: Process Synchronization

EECS 482 Introduction to Operating Systems

CS510 Advanced Topics in Concurrency. Jonathan Walpole

Goals. Processes and Threads. Concurrency Issues. Concurrency. Interlacing Processes. Abstracting a Process

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

CSE 374 Programming Concepts & Tools

CS3733: Operating Systems

Synchronization. Dr. Yingwu Zhu

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

Locks. Dongkun Shin, SKKU

Today: Synchronization. Recap: Synchronization

Page 1. Challenges" Concurrency" CS162 Operating Systems and Systems Programming Lecture 4. Synchronization, Atomic operations, Locks"

Lesson 6: Process Synchronization

Lecture 6 (cont.): Semaphores and Monitors

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

! Why is synchronization needed? ! Synchronization Language/Definitions: ! How are locks implemented? Maria Hybinette, UGA

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

CS 537 Lecture 11 Locks

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization?

POSIX / System Programming

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading

Synchronization 1. Synchronization

Last Class: Synchronization

Synchronization COMPSCI 386

THREADS: (abstract CPUs)

Advance Operating Systems (CS202) Locks Discussion

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

CS420: Operating Systems. Process Synchronization

IV. Process Synchronisation

[537] Locks. Tyler Harter

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5

Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

5. Synchronization. Operating System Concepts with Java 8th Edition Silberschatz, Galvin and Gagn

Lecture 10: Multi-Object Synchronization

Programming Languages

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources

Introduction to PThreads and Basic Synchronization

Advanced Topic: Efficient Synchronization

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Part II Process Management Chapter 6: Process Synchronization

Threads Tuesday, September 28, :37 AM

Synchronising Threads

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Chapter 6: Process Synchronization

Administrivia. Review: Thread package API. Program B. Program A. Program C. Correct answers. Please ask questions on Google group

CHAPTER 6: PROCESS SYNCHRONIZATION

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Programming in Parallel COMP755

Concurrency Control. Synchronization. Brief Preview of Scheduling. Motivating Example. Motivating Example (Cont d) Interleaved Schedules

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

Lecture #7: Implementing Mutual Exclusion

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Advanced Threads & Monitor-Style Programming 1/24

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Process Synchronization

Process/Thread Synchronization

Mutex Implementation

Interprocess Communication By: Kaushik Vaghani

Synchronization I q Race condition, critical regions q Locks and concurrent data structures q Next time: Condition variables, semaphores and monitors

Chapter 6 Process Synchronization

Synchronization. Before We Begin. Synchronization. Credit/Debit Problem: Race Condition. CSE 120: Principles of Operating Systems.

CS370 Operating Systems

Process Management And Synchronization

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.

CMSC 330: Organization of Programming Languages

Transcription:

Synchronization CS61, Lecture 18 Prof. Stephen Chong November 3, 2011

Announcements Assignment 5 Tell us your group by Sunday Nov 6 Due Thursday Nov 17 Talks of interest in next two days Towards Predictable, Heisenbug-Free Parallel Software Environments Bryan Ford, Yale Thursday 4pm MD G-125 Engineering Storage for the Data Age Friday 10am MD 319 Steve Swanson, UCSD Stephen Chong, Harvard University 2

Topics for today Why to synchronize multiple threads Race conditions Concurrent access to shared resource without synch. Mutual exclusion and critical sections Locks A way to to prevent races. A simple mechanism to synchronize threads. Efficiently implementing locks Stephen Chong, Harvard University 3

Interleaved Execution The execution of the two threads can be interleaved Assume preemptive scheduling i.e., Thread may be context switched arbitrarily, without cooperation from the thread Each thread may context switch after each assembly instruction (or, in some cases, part of an assembly instruction!) We need to worry about the worst-case scenario! Execution sequence as seen by CPU balance = get_balance(account); balance -= amount; local balance = $1400 balance = get_balance(account); balance -= amount; local balance = $1400 put_balance(account, balance); account.bal = $1500 account.bal = $1400 put_balance(account, balance); account.bal = $1400 What s the account balance after this sequence? And who's happier, the bank or you??? Stephen Chong, Harvard University 4

Little white lie... Sleeping does not help! Earlier I showed some examples to highlight which locations were shared between threads int i = 0; // global variable void bar() { i++; sleep(1); printf("i is %d.\n", i); } int i = 0; // global variable void bar() { i++; sleep(1); printf("i is %d.\n", i); } Possible outputs: 1 2, 1 2, 2 2, 2 2 All are possible, not all equally likely. Stephen Chong, Harvard University 5

Stephen Chong, Harvard University It s gets worse... Most programmers assume that memory is sequentially consistent state of memory is due to some interleaving of threads, with instructions in each thread executed in order E.g., Given and, memory is result of some ordering such as This is not true in most systems! 6 A B C D E A D E B C A D E B C A D E B C A D E B C A D E B C A D E B C A D E B C A D E B C A D E B C etc.

Example Suppose we have two threads (x and y are global, a and b are thread-local, all variables initially 0) x=1; y=2; What are the possible outputs? 0 a=y b=x x=1 y=2 a = y; b = x; printf( %d, a+b); 1 a=y x=1 b=x y=2 and others 3 2 x=1 y=2 a=y b=x and others Requires a=2 and b=0. Is possible, but no such order! Stephen Chong, Harvard University 7

What the...? What s going on? Several things, including: Processor Regs Core 1 L1 d-cache L1 i-cache Unified L2 Cache With multiple processors, multiple caches A cache may not write values from cache to memory in same order as updates Regs Core 2 Regs Core 3 L1 d-cache L1 i-cache L1 d-cache L1 i-cache Unified L2 Cache Unified L2 Cache L3 Cache Main Memory Processor may have cache hits for some locations and not others Regs Core 4 L1 d-cache L1 i-cache Unified L2 Cache Compiler optimizations Compiler may change order of instructions Stephen Chong, Harvard University 8

Relaxed memory models A model of how memory behaves provides 1) programmers with a way to think about memory 2) compiler writers with limits on what optimizations they can do 3) hardware designers with limits on what optimizations they can do Relaxed memory models provide a weaker model than sequential consistency Can be complicated! Stephen Chong, Harvard University 9

Race Conditions The problem: concurrent threads accessing a shared resource without any synchronization This is called a race condition The result of the concurrent access is non-deterministic, depends on Timing When context switches occurred Which thread ran at which context switch What the threads were doing A solution: mechanisms for controlling concurrent access to shared resources Allows us to reason about the operation of programs We want to re-introduce some determinism into the execution of multiple threads Stephen Chong, Harvard University 10

Race conditions in real life Race conditions are bugs, and difficult to detect Northeast Blackout of 2003 About 55 million people in North America affected Race condition in monitoring code in part responsible: alarm system failed Code had been running since 1990, over 3 million hours of operation, without manifesting bug Vincent Laforet Stephen Chong, Harvard University 11

Race conditions in real life Race conditions are bugs, and difficult to detect Therac-25 radiation therapy machine Designed to give non-lethal doses of radiation to cancer patients Race conditions contributed to incorrect lethal doses Several fatalities in mid-80s. Stephen Chong, Harvard University 12

Which resources are shared? Local variables in a function are not shared They exist on the stack, and each thread has its own stack Cannot safely pass a pointer from a local variable to another thread Why? Global variables are shared Stored in static data portion of the address space Accessible by any thread Dynamically-allocated data is shared Stored in the heap, accessible by any thread 0xffffffff 0xc0000000 0x40000000 0x08048000 0x00000000 OS memory Stack for thread 0 Stack for thread 1 Stack for thread 2 Shared libraries Heap (used by malloc) Read/write segment.data,.bss Read-only segment.text,.rodata unused Stack pointer for thread 0 Stack pointer for thread 1 Stack pointer for thread 2 PC for thread 2 PC for thread 0 PC for thread 1 Stephen Chong, Harvard University 13

Topics for today Why to synchronize multiple threads Race conditions Concurrent access to shared resource without synch. Mutual exclusion and critical sections Locks A way to to prevent races. A simple mechanism to synchronize threads. Efficiently implementing locks Stephen Chong, Harvard University 14

Mutual Exclusion We want to use mutual exclusion to synchronize access to shared resources Mutual exclusion: only one thread can access a shared resource at a time. Code that uses mutual exclusion to synchronize its execution is called a critical section Only one thread at a time can execute code in the critical section All other threads are forced to wait on entry When one thread leaves the critical section, another can enter Critical Section Thread 1 (modify account balance) Stephen Chong, Harvard University 15

Mutual Exclusion We want to use mutual exclusion to synchronize access to shared resources Mutual exclusion: only one thread can access a shared resource at a time. Code that uses mutual exclusion to synchronize its execution is called a critical section Only one thread at a time can execute code in the critical section All other threads are forced to wait on entry When one thread leaves the critical section, another can enter Critical Section Thread 2 Thread 2 must wait for critical section to clear Thread 1 (modify account balance) Thread 1 enters critical section Stephen Chong, Harvard University 16

Mutual Exclusion We want to use mutual exclusion to synchronize access to shared resources Mutual exclusion: only one thread can access a shared resource at a time. Code that uses mutual exclusion to synchronize its execution is called a critical section Only one thread at a time can execute code in the critical section All other threads are forced to wait on entry When one thread leaves the critical section, another can enter Critical Section Thread 2 allowed to enter critical section Thread 2 Thread 1 (modify account balance) Thread 1 leaves critical section Stephen Chong, Harvard University 17

Critical Section Requirements Mutual exclusion At most one thread is currently executing in the critical section Progress If thread T1 is outside the critical section, then T1 cannot prevent T2 from entering the critical section Bounded waiting (no starvation) If thread T1 is waiting on the critical section, then T1 will eventually enter the critical section Requires threads eventually leave critical sections Performance The overhead of entering and exiting the critical section is small with respect to the work being done within it Stephen Chong, Harvard University 18

Topics for today Why to synchronize multiple threads Race conditions Concurrent access to shared resource without synch. Mutual exclusion and critical sections Locks A way to to prevent races. A simple mechanism to synchronize threads. Efficiently implementing locks Stephen Chong, Harvard University 19

Locks A lock is an object (in memory) that provides two operations: acquire( ): a thread calls this before entering a critical section May require waiting to enter the critical section release( ): a thread calls this after leaving a critical section Allows another thread to enter the critical section A call to acquire( ) must have corresponding call to release( ) Between acquire( ) and release( ), the thread holds the lock acquire( ) does not return until the caller holds the lock At most one thread can hold a lock at a time (usually!) We'll talk about the exceptions later... What can happen if acquire( ) and release( ) calls are not paired? Stephen Chong, Harvard University 20

Using Locks int withdraw(account, amount) { acquire(lock); balance = get_balance(account); balance -= amount; put_balance(account, balance); release(lock); return balance; } critical section Why is the return statement outside of the critical section? Stephen Chong, Harvard University 21

Execution with Locks Execution sequence as seen by CPU acquire(lock); balance = get_balance(account); balance -= amount; acquire(lock); put_balance(account, balance); release(lock); balance = get_balance(account); balance -= amount; put_balance(account, balance); release(lock); Thread 1 runs Thread 2 waits on lock Thread 1 completes Thread 2 resumes Stephen Chong, Harvard University 22

Spinlocks Very simple way to implement a lock: struct lock { int held = 0; } void acquire(lock) { while (lock->held) ; lock->held = 1; } void release(lock) { lock->held = 0; } The caller busy waits for the lock to be released Why doesn't this work? Stephen Chong, Harvard University 23

Implementing Spinlocks Problem: internals of the lock acquire/release have critical sections too! The acquire( ) and release( ) actions must be atomic Atomic means that the code cannot be interrupted during execution All or nothing execution struct lock { int held = 0; } void acquire(lock) { while (lock->held) ; lock->held = 1; } void release(lock) { lock->held = 0; } What can happen if there is a context switch here? Stephen Chong, Harvard University 24

Implementing Spinlocks Problem: internals of the lock acquire/release have critical sections too! The acquire( ) and release( ) actions must be atomic Atomic means that the code cannot be interrupted during execution All or nothing execution struct lock { int held = 0; } void acquire(lock) { while (lock->held) ; lock->held = 1; } void release(lock) { lock->held = 0; } This sequence needs to be atomic! Stephen Chong, Harvard University 25

Implementing Spinlocks Achieving atomicity requires hardware support Disabling interrupts Prevent context switches from occurring Only works on uniprocessors. Why? Atomic instructions CPU guarantees entire action will execute atomically Test-and-set Compare-and-swap Stephen Chong, Harvard University 26

Spinlocks using test-and-set CPU provides the following as one atomic instruction: bool test_and_set(bool *flag) { bool old = *flag; *flag = True; return old; } So to fix our broken spinlocks, we do this: struct lock { int held = 0; } void acquire(lock) { while(test_and_set(&lock->held)); } void release(lock) { lock->held = 0; } Stephen Chong, Harvard University 27

What's wrong with spinlocks? So spinlocks work (if you implement them correctly), and are simple. What's the catch? struct lock { int held = 0; } void acquire(lock) { while(test_and_set(&lock->held)); } void release(lock) { lock->held = 0; } Stephen Chong, Harvard University 28

Problems with spinlocks Inefficient! Threads waiting to acquire locks spin on the CPU Eats up lots of cycles, slows down progress of other threads Note that other threads can still run... how? What happens if you have a lot of threads trying to acquire the lock? Usually, spinlocks are only used as primitives to build higher-level, more efficient, synchronization constructs Stephen Chong, Harvard University 29

Efficiently implementing locks Really want a thread waiting to enter a critical section to block Put the thread to sleep until it can enter the critical section Frees up the CPU for other threads to run? 1) Check lock state Thread 1 Lock state unlocked Lock wait queue Stephen Chong, Harvard University 30

Efficiently implementing locks Really want a thread waiting to enter a critical section to block Put the thread to sleep until it can enter the critical section Frees up the CPU for other threads to run Thread 1 1) Check lock state 2) Set state to locked 3) Enter critical section Lock state locked Lock wait queue Stephen Chong, Harvard University 31

Efficiently implementing locks Really want a thread waiting to enter a critical section to block Put the thread to sleep until it can enter the critical section Frees up the CPU for other threads to run Thread 2? Thread 1 1) Check lock state 2) Add self to wait queue (sleep) Lock state locked Lock wait queue Stephen Chong, Harvard University 32

Efficiently implementing locks Really want a thread waiting to enter a critical section to block Put the thread to sleep until it can enter the critical section Frees up the CPU for other threads to run Thread 1 1) Check lock state 2) Add self to wait queue (sleep) Lock state locked Z Z Z Lock wait queue Thread 2 Stephen Chong, Harvard University 33

Efficiently implementing locks Really want a thread waiting to enter a critical section to block Put the thread to sleep until it can enter the critical section Frees up the CPU for other threads to run Thread 3? Thread 1 1) Check lock state 2) Add self to wait queue (sleep) Lock state locked Z Z Z Lock wait queue Thread 2 Stephen Chong, Harvard University 34

Efficiently implementing locks Really want a thread waiting to enter a critical section to block Put the thread to sleep until it can enter the critical section Frees up the CPU for other threads to run Thread 1 1) Check lock state 2) Add self to wait queue (sleep) Lock state locked Z Z Z Z Z Z Lock wait queue Thread 2 Thread 3 Stephen Chong, Harvard University 35

Efficiently implementing locks Really want a thread waiting to enter a critical section to block Put the thread to sleep until it can enter the critical section Frees up the CPU for other threads to run Thread 1 1) Thread 1 finishes critical section Lock state locked Z Z Z Z Z Z Lock wait queue Thread 2 Thread 3 Stephen Chong, Harvard University 36

Efficiently implementing locks Really want a thread waiting to enter a critical section to block Put the thread to sleep until it can enter the critical section Frees up the CPU for other threads to run A blocked thread can now acquire lock Lock state unlocked Z Z Z Z Z Z Lock wait queue Thread 2 Thread 3 Stephen Chong, Harvard University 37

Efficiently implementing locks Really want a thread waiting to enter a critical section to block Put the thread to sleep until it can enter the critical section Frees up the CPU for other threads to run A blocked thread can now acquire lock Thread 3 No guarantee on which blocked thread will get the lock!!! Lock state locked Z Z Z Lock wait queue Thread 2 Stephen Chong, Harvard University 38

Locks in PThreads Pthreads provides a pthread_mutex_t to represent a lock for mutual exclusion, a mutex. Threads using the mutex must have access to the pthread_mutex_t object. Usually, this means declaring it as a global variable. pthread_mutex_t mylock; /* Must be global so all * threads using the lock * can access this variable. */ /* Initialize it. */ /* Only one thread has to do this. */ pthread_mutex_init(&mylock, NULL); void *mythread(void *arg) { /* Do something with the lock */ pthread_mutex_lock(&mylock); /* Do stuff... */ } pthread_mutex_unlock(&mylock); Stephen Chong, Harvard University 39

Lock granularity Locks are great, and simple, but have limitations What if you have a more complex resource than a single location? Coarse-grained lock: Could use one lock to protect all resources E.g., Many bank accounts, use one lock to protect access to all accounts Fine-grained lock: Protect each resource with a separate lock E.g., Many bank accounts, one lock per account Coarse vs. fine-grained? More locks harder to manage locks E.g., transfer money from account A to account B at same time as transferring from B to A. What order to acquire locks? More on this next week... Fewer locks less concurrency Stephen Chong, Harvard University 40

Next Lecture Higher level synchronization primitives: How do to fancier stuff than just locks Semaphores, monitors, and condition variables Implemented using basic locks as a primitive Allow applications to perform more complicated coordination schemes Stephen Chong, Harvard University 41