Dave Eckhardt de0u@andrew.cmu.edu Synchronization (2) Dave Eckhardt de0u@andrew.cmu.edu.

Slides:



Advertisements
Similar presentations
Operating Systems Part III: Process Management (Process Synchronization)
Advertisements

Synchronization. How to synchronize processes? – Need to protect access to shared data to avoid problems like race conditions – Typical example: Updating.
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.
Parallel Processing (CS526) Spring 2012(Week 6).  A parallel algorithm is a group of partitioned tasks that work with each other to solve a large problem.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
Concurrency.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Synchronization Solutions
Hardware solutions So far we have looked at software solutions for the critical section problem. –algorithms whose correctness does not rely on any other.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Threads and Critical Sections Vivek Pai / Kai Li Princeton University.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 Synchronization (1) Dave Eckhardt
Concurrency, Mutual Exclusion and Synchronization.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Chapter 28 Locks Chien-Chung Shen CIS, UD
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
15-410, F’ Synchronization #2 Sep. 20, 2004 Dave Eckhardt Bruce Maggs L09_Synch “Strangers in the night...”
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
Spin Locks and Contention Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
15-410, S’ Synchronization #2 Jan. 30, 2004 Dave Eckhardt Bruce Maggs L09b_Synch “Strangers in the night...”
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Outline Introduction Centralized shared-memory architectures (Sec. 5.2) Distributed shared-memory and directory-based coherence (Sec. 5.4) Synchronization:
CSE 120 Principles of Operating
CS703 – Advanced Operating Systems
Concurrency.
Atomic Operations in Hardware
Chapter 5: Process Synchronization
Lecture 12: Peterson’s Solution and Hardware Support
Lecture 11: Mutual Exclusion
Designing Parallel Algorithms (Synchronization)
Concurrency: Locks Questions answered in this lecture:
Topic 6 (Textbook - Chapter 5) Process Synchronization
Jonathan Walpole Computer Science Portland State University
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Lecture 2 Part 2 Process Synchronization
Critical section problem
Architectural Support for OS
Grades.
Implementing Mutual Exclusion
Chapter 6: Process Synchronization
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
Implementing Mutual Exclusion
Lecture 12: Peterson’s Solution and Hardware Support
Lecture 11: Mutual Exclusion
Basic Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
Synchronization on a Parallel Machine Transactional Memory
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
Atomicity, Mutex, and Locks
Process/Thread Synchronization (Part 2)
CSE 542: Operating Systems
Presentation transcript:

Dave Eckhardt de0u@andrew.cmu.edu Synchronization (2) Dave Eckhardt de0u@andrew.cmu.edu

Synchronization Handing in Issues are possible Don't mail us your assignment Try hand-in now Thank you

Outline Last time Today Two building blocks Three requirements for mutual exclusion Algorithms people don't use for mutual exclusion Today Ways to really do mutual exclusion

Mutual Exclusion: Reminder Protects an atomic instruction sequence Do "something" to guard against CPU switching to another thread Thread running on another CPU Assumptions Atomic instruction sequence will be “short” No other thread “likely” to compete

Mutual Exclusion: Goals Typical case (no competitor) should be fast Atypical case can be slow Should not be “too wasteful”

Mutex aka Lock aka Latch Object specifies interfering code sequences Data item(s) “protected by the mutex” Methods encapsulate entry & exit protocols mutex_lock(&store->lock); cash = store->cash cash += 50; personal_cash -= 50; store->cash = cash; mutex_unlock(&store->lock); What's inside?

Mutual Exclusion: Atomic Exchange Intel x86 XCHG instruction intel-isr.pdf page 754 xchg (%esi), %edi int32 xchg(int32 *lock, int32 val) { register int old; old = *lock; /* bus is locked */ *lock = val; /* bus is locked */ return (old); }

Inside a Mutex Initialization Try-lock Spin-wait Unlock int lock_available = 1; Try-lock i_won = xchg(&lock_available, 0); Spin-wait while (!xchg(&lock_available, 0) /* nothing */ ; Unlock xchg(&lock_available, 1); /*expect 0*/

Strangers in the Night, Exchanging 0's

And the winner is...

[What are the questions, again?] Does it work? [What are the questions, again?]

Does it work? Mutual Exclusion Progress Bounded Waiting

Does it work? Mutual Exclusion Progress Bounded Waiting Only one thread can see lock_available == 1 Progress Whenever lock_available == 1 a thread will get it Bounded Waiting No A thread can lose arbitrarily many times

Attaining Bounded Waiting Lock waiting[i] = true; got_it = false; while (waiting[i] && !got_it) got_it = xchg(&lock_available, false); waiting[i] = false;

Attaining Bounded Waiting Unlock j = (i + 1) % n; while ((j != i) && !waiting[j]) if (j == i) xchg(&lock_available, true); /*W*/ else waiting[j] = false;

Attaining Bounded Waiting Versus textbook Swap vs. TestAndSet “Available” vs. “locked” Atomic release vs. normal memory write Locker does XCHG, unlocker does too Mandatory on many shared-memory processors Text does “blind write” at point “W”

Evaluation One awkward requirement One unfortunate behavior

Evaluation One awkward requirement One unfortunate behavior Everybody knows size of thread population Always & instantly! Or uses an upper bound One unfortunate behavior Recall: expect zero competitors Algorithm: O(n) in maximum possible competitors Am I too demanding? After all, Baker's Algorithm has these misfeatures...

Uniprocessor Environment Lock What if xchg() didn't work the first time? Some other process has the lock That process isn't running (because you are) xchg() loop is a waste of time Unlock What about bounded waiting? Next xchg() winner “chosen” by thread scheduler How capricious are real thread schedulers?

Multiprocessor Environment Lock Spin-waiting probably justified (why?) Unlock Next xchg() winner “chosen” by memory hardware How capricious are real memory controllers?

Test&Set Conceptually simpler than XCHG? Or not boolean testandset(int32 *lock) { register boolean old; old = *lock; /* bus is locked */ *lock = true; /* bus is locked */ return (old); } Conceptually simpler than XCHG? Or not

Load-linked, Store-conditional For multiprocessors “Bus locking considered harmful” Split XCHG into halves Load-linked fetches old value from memory Store-conditional stores new value If nobody else did Your cache “snoops” the bus Better than locking it!

Intel i860 magic lock bit Instruction sets processor in “lock mode” Locks bus Disables interrupts Isn't that dangerous? 32-cycle countdown timer triggers unlock Exception triggers unlock Memory write triggers unlock

Mutual Exclusion: Software Lamport's “Fast Mutual Exclusion” algorithm 5 writes, 2 reads (if no contention) Not bounded-waiting (in theory, i.e., if contention) http://www.hpl.hp.com/techreports/Compaq- DEC/SRC-RR-7.html Why not use it? What kind of memory writes/reads?

Passing the Buck Q: Why not ask the OS to provide mutex_lock()? Easy on a uniprocessor... Kernel automatically excludes other threads Kernel can easily disable interrupts Kernel has special power on a multiprocessor Can issue “remote interrupt” to other CPUs So why not rely on OS?

Passing the Buck A: Too expensive Because... (you know this song!)

Mutual Exclusion: Tricky Software Fast Mutual Exclusion for Uniprocessors Bershad, Redell, Ellis: ASPLOS V (1992) Want uninterruptable instruction sequences? Pretend! scash = store->cash; scash += 10; wallet -= 10; store->cash = scash; Usually won't be interrupted...

How can that work? Kernel detects “context switch during sequence” Maybe a small set of instructions Maybe particular memory areas Maybe a flag no_interruption_please = 1; Kernel handles unusual case Hand out another time slice? (Is that ok?) Simulate unfinished instructions (yuck) Idempotent sequence: slide PC back to start

Review Atomic instruction sequence Voluntary de-scheduling Nobody else may interleave same/”related” sequence Short sequence of instructions Ok to force competitors to wait Probability of collision is “low” Avoid expensive exclusion method Voluntary de-scheduling Can't proceed with this world state Unlock world, yield CPU: other threads enable us