CSE 451: Operating Systems Spring 2012 Module 7 Synchronization Ed Lazowska Allen Center 570.

Slides:



Advertisements
Similar presentations
Operating Systems ECE344 Midterm review Ding Yuan
Advertisements

Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
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.
Mutual Exclusion.
CSE 451: Operating Systems Winter 2012 Synchronization Mark Zbikowski Gary Kimura.
CS444/CS544 Operating Systems Introduction to Synchronization 2/07/2007 Prof. Searleman
Synchronization. Shared Memory Thread Synchronization Threads cooperate in multithreaded environments – User threads and kernel threads – Share resources.
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
CS444/CS544 Operating Systems Synchronization 2/16/2007 Prof. Searleman
1 Synchronization Coordinating the Activity of Mostly Independent Entities.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Process Synchronization Ch. 4.4 – Cooperating Processes Ch. 7 – Concurrency.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
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.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CENG334 Introduction to Operating Systems
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
IT 344: Operating Systems Module 6 Synchronization Chia-Chi Teng CTB 265.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
CSE 153 Design of Operating Systems Winter 2015 Lecture 5: Synchronization.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
1 CSE451 Basic Synchronization Autumn 2002 Gary Kimura Lecture #7 & 8 October 14 & 16, 2002.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CSE 451: Operating Systems Spring 2010 Module 7 Synchronization John Zahorjan Allen Center 534.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
CSE 120 Principles of Operating
Background on the need for Synchronization
Synchronization.
Synchronization.
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Synchronization, Monitors, Deadlocks
CENG334 Introduction to Operating Systems
CSE 451: Operating Systems Winter 2007 Module 6 Synchronization
Kernel Synchronization II
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 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
CSE 451: Operating Systems Spring 2008 Module 7 Synchronization
CSE 451: Operating Systems Winter 2007 Module 6 Synchronization
CSE 451: Operating Systems Autumn 2009 Module 7 Synchronization
CSE 153 Design of Operating Systems Winter 2019
CSE 451: Operating Systems Spring 2007 Module 7 Synchronization
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

CSE 451: Operating Systems Spring 2012 Module 7 Synchronization Ed Lazowska Allen Center 570

© 2012 Gribble, Lazowska, Levy, Zahorjan 2 Temporal relations Instructions executed by a single thread are totally ordered –A < B < C < … Absent synchronization, instructions executed by distinct threads must be considered unordered / simultaneous –Not X < X’, and not X’ < X

© 2012 Gribble, Lazowska, Levy, Zahorjan 3 Example

© 2012 Gribble, Lazowska, Levy, Zahorjan 4 Critical Sections / Mutual Exclusion Sequences of instructions that may get incorrect results if executed simultaneously are called critical sections (We also use the term race condition to refer to a situation in which the results depend on timing) Mutual exclusion means “not simultaneous” –A < B or B < A –We don’t care which Forcing mutual exclusion between two critical section executions is sufficient to ensure correct execution – guarantees ordering One way to guarantee mutually exclusive execution is using locks

© 2012 Gribble, Lazowska, Levy, Zahorjan 5 Critical sections

© 2012 Gribble, Lazowska, Levy, Zahorjan 6 When do critical sections arise? One common pattern: –read-modify-write of –a shared value (variable) –in code that can be executed concurrently (Note: There may be only one copy of the code (e.g., a procedure), but it can be executed by more than one thread at a time) Shared variable: –Globals and heap-allocated variables –NOT local variables (which are on the stack) (Note: Never give a reference to a stack-allocated (local) variable to another thread, unless you’re superhumanly careful …)

© 2012 Gribble, Lazowska, Levy, Zahorjan 7 Example: buffer management Threads cooperate in multithreaded programs –to share resources, access shared data structures e.g., threads accessing a memory cache in a web server –also, to coordinate their execution e.g., a disk reader thread hands off blocks to a network writer thread through a circular buffer disk reader thread network writer thread circular buffer

© 2012 Gribble, Lazowska, Levy, Zahorjan 8 Example: shared bank account Suppose we have to implement a function to withdraw money from a bank account: int withdraw(account, amount) { int balance = get_balance(account); // read balance -= amount;// modify put_balance(account, balance);// write spit out cash; } Now suppose that you and your partner share a bank account with a balance of $ –what happens if you both go to separate ATM machines, and simultaneously withdraw $10.00 from the account?

© 2012 Gribble, Lazowska, Levy, Zahorjan 9 Assume the bank’s application is multi-threaded A random thread is assigned a transaction when that transaction is submitted int withdraw(account, amount) { int balance = get_balance(account); balance -= amount; put_balance(account, balance); spit out cash; } int withdraw(account, amount) { int balance = get_balance(account); balance -= amount; put_balance(account, balance); spit out cash; }

© 2012 Gribble, Lazowska, Levy, Zahorjan 10 Interleaved schedules The problem is that the execution of the two threads can be interleaved, assuming preemptive scheduling: What’s the account balance after this sequence? –who’s happy, the bank or you? How often is this sequence likely to occur? balance = get_balance(account); balance -= amount; balance = get_balance(account); balance -= amount; put_balance(account, balance); spit out cash; put_balance(account, balance); spit out cash; Execution sequence as seen by CPU context switch

© 2012 Gribble, Lazowska, Levy, Zahorjan 11 Which interleavings are ok? Which are not? Other Execution Orders int withdraw(account, amount) { int balance = get_balance(account); balance -= amount; put_balance(account, balance); spit out cash; } int withdraw(account, amount) { int balance = get_balance(account); balance -= amount; put_balance(account, balance); spit out cash; }

© 2012 Gribble, Lazowska, Levy, Zahorjan 12 int xfer(from, to, amt) { withdraw( from, amt ); deposit( to, amt ); } How About Now? int xfer(from, to, amt) { withdraw( from, amt ); deposit( to, amt ); } Morals: –Interleavings are hard to reason about We make lots of mistakes Control-flow analysis is hard for tools to get right –Identifying critical sections and ensuring mutually exclusive access is … “easier”

© 2012 Gribble, Lazowska, Levy, Zahorjan 13 i++; Another example i++;

© 2012 Gribble, Lazowska, Levy, Zahorjan 14 Correct critical section requirements Correct critical sections have the following requirements –mutual exclusion at most one thread is in the critical section –progress if thread T is outside the critical section, then T cannot prevent thread S from entering the critical section –bounded waiting (no starvation) if thread T is waiting on the critical section, then T will eventually enter the critical section –assumes 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

© 2012 Gribble, Lazowska, Levy, Zahorjan 15 Mechanisms for building critical sections Spinlocks –primitive, minimal semantics; used to build others Semaphores (and non-spinning locks) –basic, easy to get the hang of, somewhat hard to program with Monitors –higher level, requires language support, implicit operations –easier to program with; Java “ synchronized() ” as an example Messages –simple model of communication and synchronization based on (atomic) transfer of data across a channel –direct application to distributed systems

© 2012 Gribble, Lazowska, Levy, Zahorjan 16 Locks A lock is a memory object with two operations: –acquire() : obtain the right to enter the critical section –release() : give up the right to be in the critical section acquire() prevents progress of the thread until the lock can be acquired (Note: terminology varies: acquire/release, lock/unlock)

© 2012 Gribble, Lazowska, Levy, Zahorjan 17 Locks: Example

© 2012 Gribble, Lazowska, Levy, Zahorjan 18 Acquire/Release Threads pair up calls to acquire() and release() –between acquire() and release(), the thread holds the lock –acquire() does not return until the caller “owns” (holds) the lock at most one thread can hold a lock at a time –What happens if the calls aren’t paired (I acquire, but neglect to release)? –What happens if the two threads acquire different locks (I think that access to a particular shared data structure is mediated by lock A, and you think it’s mediated by lock B)? (granularity of locking)

© 2012 Gribble, Lazowska, Levy, Zahorjan 19 Using locks What happens when green tries to acquire the lock? int withdraw(account, amount) { acquire(lock); balance = get_balance(account); balance -= amount; put_balance(account, balance); release(lock); spit out cash; } acquire(lock) balance = get_balance(account); balance -= amount; balance = get_balance(account); balance -= amount; put_balance(account, balance); release(lock); spit out cash; put_balance(account, balance); release(lock); acquire(lock) critical section spit out cash;

© 2012 Gribble, Lazowska, Levy, Zahorjan 20 Roadmap … Where we are eventually going: –The OS and/or the user-level thread package will provide some sort of efficient primitive for user programs to utilize in achieving mutual exclusion (for example, locks or semaphores, used with condition variables) –There may be higher-level constructs provided by a programming language to help you get it right (for example, monitors – which also utilize condition variables) But somewhere, underneath it all, there needs to be a way to achieve “hardware” mutual exclusion (for example, test-and-set used to implement spinlocks) –This mechanism will not be utilized by user programs –But it will be utilized in implementing what user programs see

© 2012 Gribble, Lazowska, Levy, Zahorjan 21 Spinlocks How do we implement spinlocks? Here’s one attempt: Why doesn’t this work? –where is the race condition? struct lock_t { int held = 0; } void acquire(lock) { while (lock->held); lock->held = 1; } void release(lock) { lock->held = 0; } the caller “busy-waits”, or spins, for lock to be released  hence spinlock

© 2012 Gribble, Lazowska, Levy, Zahorjan 22 Implementing spinlocks (cont.) Problem is that implementation of spinlocks has critical sections, too! –the acquire/release must be atomic atomic == executes as though it could not be interrupted code that executes “all or nothing” Need help from the hardware –atomic instructions test-and-set, compare-and-swap, … –disable/reenable interrupts to prevent context switches

© 2012 Gribble, Lazowska, Levy, Zahorjan 23 Spinlocks redux: Hardware Test-and-Set CPU provides the following as one atomic instruction: Remember, this is a single atomic instruction … bool test_and_set(bool *flag) { bool old = *flag; *flag = True; return old; }

© 2012 Gribble, Lazowska, Levy, Zahorjan 24 Implementing spinlocks using Test-and-Set So, to fix our broken spinlocks: –mutual exclusion? (at most one thread in the critical section) –progress? (T outside cannot prevent S from entering) –bounded waiting? (waiting T will eventually enter) –performance? (low overhead (modulo the spinning part …)) struct lock { int held = 0; } void acquire(lock) { while(test_and_set(&lock->held)); } void release(lock) { lock->held = 0; }

© 2012 Gribble, Lazowska, Levy, Zahorjan 25 Reminder of use … How does a thread blocked on an “acquire” (that is, stuck in a test-and-set loop) yield the CPU? –calls yield( ) (spin-then-block) –there’s an involuntary context switch (e.g., timer interrupt) int withdraw(account, amount) { acquire(lock); balance = get_balance(account); balance -= amount; put_balance(account, balance); release(lock); spit out cash; } acquire(lock) balance = get_balance(account); balance -= amount; balance = get_balance(account); balance -= amount; put_balance(account, balance); release(lock); spit out cash; put_balance(account, balance); release(lock); acquire(lock) critical section spit out cash;

© 2012 Gribble, Lazowska, Levy, Zahorjan 26 Problems with spinlocks Spinlocks work, but are wasteful! –if a thread is spinning on a lock, the thread holding the lock cannot make progress You’ll spin for a scheduling quantum –(pthread_spin_t) Only want spinlocks as primitives to build higher-level synchronization constructs –Why is this okay? We’ll see later how to build blocking locks –But there is overhead – can be cheaper to spin –(pthread_mutex_t)

© 2012 Gribble, Lazowska, Levy, Zahorjan 27 Another approach: Disabling interrupts struct lock { } void acquire(lock) { cli(); // disable interrupts } void release(lock) { sti(); // reenable interrupts }

© 2012 Gribble, Lazowska, Levy, Zahorjan 28 Problems with disabling interrupts Only available to the kernel –Can’t allow user-level to disable interrupts! Insufficient on a multiprocessor –Each processor has its own interrupt mechanism “Long” periods with interrupts disabled can wreak havoc with devices Just as with spinlocks, you only want to use disabling of interrupts to build higher-level synchronization constructs

© 2012 Gribble, Lazowska, Levy, Zahorjan 29 Race conditions Informally, we say a program has a race condition (aka “data race”) if the result of an executing depends on timing –i.e., is non-deterministic Typical symptoms –I run it on the same data, and sometimes it prints 0 and sometimes it prints 4 –I run it on the same data, and sometimes it prints 0 and sometimes it crashes

© 2012 Gribble, Lazowska, Levy, Zahorjan 30 Summary Synchronization introduces temporal ordering Adding synchronization can eliminate races Synchronization can be provided by locks, semaphores, monitors, messages … Spinlocks are the lowest-level mechanism –primitive in terms of semantics – error-prone –implemented by spin-waiting (crude) or by disabling interrupts (also crude, and can only be done in the kernel) In our next exciting episode … –semaphores are a slightly higher level abstraction Importantly, they are implemented by blocking, not spinning Locks can also be implemented in this way –monitors are significantly higher level utilize programming language support to reduce errors