chapter 6 process thread synchronization
play

Chapter 6: Process [& Thread] Synchronization Why is - PowerPoint PPT Presentation

Chapter 6: Process [& Thread] Synchronization Why is synchronization needed? CSCI [4|6] 730 Synchronization Language/Definitions: What are race conditions? Operating Systems What are critical sections? What are atomic


  1. Chapter 6: Process [& Thread] Synchronization ● Why is synchronization needed? CSCI [4|6] 730 ● Synchronization Language/Definitions: » What are race conditions? Operating Systems » What are critical sections? » What are atomic operations? Synchronization Part 1 : The Basics ● How are locks implemented? Maria Hybinette, UGA Maria Hybinette, UGA 1,2, 3.. Why does cooperation require deposit synchronization? Example Execution deposit(amount) { balance = balance + amount; } ● Example: Two threads: Maria and Tucker share an 1. Initialization: balance = 100 account with shared variable � balance � in memory. 2. Maria: deposit( 200 ) deposit (Maria): 3. Tucker: deposit( 10 ) load RegisterA, 100 ● Compiled to assembly: ● Code to deposit() : add RegisterA, 200 store RegisterA, balance void deposit( int amount ) deposit: deposit: { load RegisterA, balance load RegisterA, balance deposit (Tucker): balance = balance + amount; add RegisterA, amount add RegisterA, amount } store RegisterA, balance store RegisterA, balance load RegisterA, 300 Time add RegisterA, 10 store RegisterA, balance ● Both Maria & Tucker deposit money into account: Memory: Memory: Memory: Memory: Memory: Memory: Memory: » Initialization: balance = 100 balance = 100 balance = 300 balance = 310 balance = 300 balance = 300 balance = 100 balance = 100 » Maria: deposit( 200 ) RegisterA = 310 RegisterA = 310 RegisterA = 100 RegisterA = 300 RegisterA = 0 RegisterA = 300 RegisterA = 300 Which variables are shared? Which are private? » Tucker: deposit( 10 ) Maria Hybinette, UGA Maria Hybinette, UGA

  2. What program data is (or is not) 4. Memory: 1. Memory: 2. Memory: 3. Memory: 4. Memory: 2. Memory: 3. Memory: 1. Memory: 310? 300? balance = 100 balance = 100 balance = 300 balance = 100 balance = 100 balance = 110 balance = 100 balance = 100 110? Concurrency shared? RegisterA = 300 RegisterA = 100 RegisterA = 0 RegisterA = 300 RegisterA = 110 RegisterA = 110 RegisterA = 0 RegisterA = 100 ● What happens if M & T deposit the funds deposit(amount) { balance = balance + amount; } � concurrently � ? » Strategy: deposit: ● Local variables are not shared (private) – Assume that any interleaving is possible load RegisterA, balance » Each thread has its own stack » No assumption about scheduler add RegisterA, amount » Observation: When a thread is interrupted » Local variables are allocated on private stack content of registers are saved (and restored) by store RegisterA, balance interrupt handlers (dispatcher/context switcher) ● Global variables and static objects are shared Time – Initialization: balance = 100 – Maria: deposit( 200 ) » Stored in the static data segment, accessible by any – Tucker: deposit( 10 ) threads deposit (Maria): deposit (Tucker): » Pass by (variable) ‘reference’ : &data1 load RegisterA, balance M ● Dynamic objects and other heap objects are shared T load RegisterA, balance M » Allocated from heap with malloc / free or new / delete add RegisterA, 200 add RegisterA, 10 T Beware of Weird Bugs: Never pass, share, or store store RegisterA, balance M T a pointer * to a local variable on another threads store RegisterA, balance stack Maria Hybinette, UGA Maria Hybinette, UGA Race Condition How to avoid race conditions ● Idea: Prohibit one or more threads from ● Results depends on the order of execution reading and writing shared data at the same » Result in non-deterministic bugs, these are hard to find! time! ⇒ Provide Mutual Exclusion (what?) – Deterministic : Input alone determines results, i.e., the ● Critical Section: Part of program (or ‘slice”) same inputs always produce the same results: where shared memory is accessed ● Example: Sqrt (4) = 2 ● Intermittent – Critical Section » A time dependent `bug � » a small change may hide the real bug (e.g., print void credit( int amount ) void debit( int amount ) statements can hide the real bug because they slow { { down processing time and consequently impacts the int x = 5; int i; timing of the threads). printf( � Adding money � ); balance = balance - amount; balance = balance + amount; for( i = 0; i < 5; i++ ); } } Maria Hybinette, UGA Maria Hybinette, UGA

  3. What We Want: THE Critical Section Problem? Mutual Exclusion (!) ● Problem: Avoiding race conditions (i.e., provide Maria enters her critical section Maria leaves her critical section mutual exclusion) is not sufficient for having threads cooperate correctly (no progress) and Process Maria efficiently: » What about if no one gets into the critical section even if Process Tucker several threads wants to get in? (No progress at ALL!) » What about if someone waits outside the critical section and never gets a turn? (starvation, NOT FAIR!) Time Tucker is blocked, and waits Tucker enters his void deposit( int amount ) critical section Tucker leaves his { Tucker attempts to enter critical section balance = balance + amount; his critical section } 10 Maria Hybinette, UGA Maria Hybinette, UGA Solve : THE Critical Section Critical Section Problem: Properties Problem: � Proper � Synchronization Memorize Required � Proper � ties : Required Properties: ● Mutual Exclusion ● Mutual Exclusion: ● Progress (someone gets the CS) » Only one thread in the critical section at a time ● Bounded waiting (starvation-free, eventually you will run) ● Progress (e.g., someone gets the CS): It � s » Not block others out: if there are requests to enter the Desirable Properties: Available CS must allow one to proceed ● Efficient: » Must not depend on threads outside critical section » Don � t consume substantial resources while waiting. – Example : Do not busy wait (i.e., spin wait) – If no one is in CS then someone must be let in … ● Fair: ● We take no reservations! » Don � t make some processes wait longer than others ● Bounded waiting (starvation-free): ● Simple: Should be easy to reason about and use » Must eventually allow each waiting thread 11 12 » to enter Maria Hybinette, UGA Maria Hybinette, UGA

  4. Critical Section Problem: Need Atomic Operations Disabling Interrupts ● Basics: Need atomic operations: ● Kernel provides two system calls: void Aquire() { » No other instructions can be interleaved (low level) » Acquire() and disable interrupts » Completed in its entirety without interruption (no craziness) » Release() } ● Examples of atomic operations: ● No preemption when interrupts are off! void Release() » Loads and stores of words » No clock interrupts can occur { – load register1, B ● Disadvantage: enable interrupts } – store register2, A » unwise to give processes power to turn of » Idea: : Code between interrupts on uniprocessors interrupts – Disable timer interrupts, don � t do any I/O – Never turn interrupts on again! » Special hardware instructions (later) » Does not work on multiprocessors – � load, store � in one instruction ● When to use?: – Test&Set » But it may be good for kernel itself to disable – Compare&Swap interrupts for a few instructions while it is Who do you trust? updating variables or lists Do you trust your kernel? Do you trust your friend � s kernel? 13 14 Do you trust your kernel � s friends? Maria Hybinette, UGA Maria Hybinette, UGA Software Solutions Attempt 1: Shared Lock Variable ● Single shared lock variable ● Assumptions: boolean lock = false; // lock available shared variable » We have an atomic load operation (read) void deposit(int amount) { » We have an atomic store operation (assignment) while( lock == true ) {} /* while lock is set : wait */ ; Entry CS: lock = true; /* gets the lock */ ● Notation [lock=true, lock=false] CS: balance += amount; // critical section » True: means un-available (lock is set, someone has the lock) Exit CS: lock = false; /* release the lock */ } » False: means available (e.g., lock is not set, as the CS is available, no one is in the CS) ● Uses busy waiting ● Does this work? » Are any of the principles violated (i.e., does it ensure mutual, progress and bounded waiting)? 15 16 Maria Hybinette, UGA Maria Hybinette, UGA

Recommend


More recommend