cse 120
play

CSE 120 Definition: one that cant affect or be affected State not - PowerPoint PPT Presentation

Independent Processes CSE 120 Definition: one that cant affect or be affected State not shared in any way by any other process Deterministic and reproducible Input state alone determines results Can stop and restart with no bad


  1. Independent Processes CSE 120 Definition: one that can’t affect or be affected � State not shared in any way by any other process � Deterministic and reproducible – Input state alone determines results � Can stop and restart with no bad effects – No side-effects, other than timing � Example: program that calculates the 10th prime July 12, 2006 Sharing state examples � Sharing a file Day 3 � Uses input from another process � Generates output for another process Synchronization � Want to use the same resources as another process – (disk/printer/peripheral) Instructor: Neil Rhodes From theoretical point of view, independent processes are interesting � In real life, most processes aren’t independent 2 Cooperating Processes Bank Balance Problem Computation based on collection of cooperating processes sharing Process A Process B some state Deposit(int amt ) Withdraw(int amt) � Want reproducible results { � Don’t care about runtime/interleaving { � Can we rerun a set of cooperating processes and have it execute exactly the balance = balance - amt; balance = balance + amt; same way? } } – Not at a micro level—runtime/interleaving may be different - System clock must be set to same starting value - Disk heads must be at same locations - Data structures in kernel must be identical - Disk layout the same � Can we get the same results? – Yes, possible Assumptions Why have cooperating processes? balance is a shared variable � Sharing: one database of parts, many sales agents Read and assignment are each atomic � Speed: One process reads while another processes Question � Modularity � If balance starts at 100, and we do a Deposit(50) and Withdraw(30) simultaneously, what is the ending balance? Race Condition � Result of computation depends on exactly which process runs when 3 4

  2. Atomic operation What We Are Trying to Solve Indivisible Critical Section Problem � Either completely finishes, or doesn’t do anything � A number of processes each of which have a critical section of code. � Can’t be interrupted entry section critical section Loads and stores of single value atomic (in hardware) exit section � int a; remainder section int b = 3; a = b � No assumption about the relative speed of the processes � Don’t want more than one process to manipulate shared resources simultaneously Loads and stores of aggregate values not atomic � struct MyStruct a, b; Desires � Mutual Exclusion : … – Only one process can be executing their critical section at a time � Progress a = b; – No process not in its entry, critical, or exit sections can block any other process wishing Normally, hardware guarantees that a single instructions is atomic to enter its critical section. � Bounded waiting – A process can’t wait forever to enter its critical section We can build up higher-level operations out of a low-level atomic operation � Semaphores, Monitors, Signals, Mutexes. 5 6 Bank Balance Problem Revisited Bank Balance Problem Part III Process A Process B Process A Process B Deposit(int amt ) Withdraw(int amt) Deposit(int amt ) Withdraw(int amt) { { { { while (guard == 1) while (turn == A) while (guard == 1) while (turn == B) ; ; � ; � ; guard = 1; balance = balance - amt; guard = 1; balance = balance + amt; balance = balance - amt; turn = A; balance = balance + amt; turn = B; guard = 0; } guard = 0; } } } Guard is there to protect against both processes trying to manipulate turn tells whose turn it is � 0 means A, 1 means B balance � Starts out at 0 Any problems? Any problems? 7 8

  3. Bank Balance Problem Part V Bank Balance Problem Part IV (Peterson’s solution) Process A Process B Process A Process B Deposit(int amt ) Withdraw(int amt) Deposit(int amt ) Withdraw(int amt) { { { { Turn off interrupts flagB = true; Turn off interrupts flagA = true; balance = balance - amt; turn = A; balance = balance + amt; turn = B; Turn on interrupts while (flagA && turn==A) Turn on interrupts while (flagB && turn == B) } ; // do nothing } � ; // do nothing balance = balance - amt; balance = balance + amt; flagB = false; flagA = false; } } Any problems? Initialization � turn = A � flagA = flagB = false Any problems? 9 10 Bank Balance Problem Part VI Desires (Hardware test&set solution) Process A Process B Don’t want more than one process manipulating shared data simultaneously Deposit(int amt ) Withdraw(int amt) Definitions { { � Critical region : region of code where only one process should be executing while (tset(&lock) == 1) while (tset(&lock) == 1) ; � ; � Mutual Exclusion : if one process is using a resource, another process is balance = balance - amt; balance = balance + amt; excluded from that resource lock = 0; lock = 0; Requirements for a solution } } � No two processes may be inside their critical regions (mutual exclusion) � No assumption may be made about the speed or numbers of CPUs Test and Set (tset) � No process running outside its critical region may block another process � Hardware operation that does the following (atomically) – Requiring alternating Deposit/Withdrawal not OK � No process should have to wait forever to enter its critical region (starvation) tset(memory_loc) Desires for a solution int return_value = *memory_loc � Efficient: don’t use lots of resources when waiting (no busy waiting) *memory_loc = 1 � Simple: should be easy to use return return_value Any problems? 11 12

  4. Mutual Exclusion using Locks Locks How to use: Used for mutual exclusion � Lock before manipulating shared data � Unlock afterwards Two operations: � Do not lock again if you’ve already locked it � Acquire : obtains the lock. Only one thread may have the lock at a time. � Don’t unlock unless you should (usually, you locked it) � Don’t spend lots of time in critical region – If the lock is already acquired by another thread, acquire blocks � Release : Releases the lock. If another thread is waiting for the lock, it is � Don’t fail in critical region (make sure to unlock on exception) awoken class Lock Acquire() � Implementation Disable interrupts Release() Enable interrupts 13 14 Locks Locks Used for mutual exclusion Used for mutual exclusion class Lock Boolean locked = false; Two operations: Two operations: � Acquire: � Acquire: obtains the lock. Only one thread may have the lock at a time. Acquire() � Release – If the lock is already acquired by another thread, acquire blocks � Implementation � Release: Releases the lock. If another thread is waiting for the lock, it is if (locked) awoken class Lock put thread on wait queue Boolean locked = 0; put thread to sleep � Implementation Acquire() else while (tset(&locked) == 1) locked = true; ; Release() Release() locked = 0; if thread waiting on wait queue, put the thread on the ready queue else locked = false 15 16

  5. Locks Semaphores Used for mutual exclusion Synchronization variable � Value is non-negative class Lock Boolean locked = 0; Two atomic operations: Boolean lockGuard = 1; � down ( P or wait ) Acquire() – If value is zero, put process to sleep – Else, decrement value if (locked) � up ( V or signal ) put thread on wait queue – If process is waiting, wakeup up one waking process go to sleep – Else, increment value else locked = 1; Binary semaphores � Value is only 0 or 1 Release() Counting semaphores � Value represents some count if thread waiting on wait queue, put thread on ready queue else locked = 0 17 18 Bank Balance Problem Part VII Semaphore Process A Process B Machine independent Simple Deposit(int amt ) Withdraw(int amt) Solve Mutual Exclusion { { balanceSemaphore.P(); Solve Synchronization balanceSemaphore.P(); balance = balance - amt; balance = balance + amt; No busy wait balanceSemaphore.V(); balanceSemaphore.V(); Can acquire multiple resources } } � Each with its own semaphore Can have many different critical sections � Use different semaphore for each different critical section Can work for more than 2 processes Initialization � balanceSemaphore=Semaphore(1) Any problems? 19 20

  6. Semaphore Implementation Synchronization Examples P() and V() are atomic operations From the (free) book Little Book of Semaphores by Allen Downey � The entire procedures are critical sections � http://www.greenteapress.com/semaphores Must achieve mutual exclusion for those procedures using a lower- level mechanism � Test-and-set lock � Turning off interrupts (unless multiprocessor) � Peterson’s Algorithm Still may have busy-wait � But critical section is very short (not many instructions) 21 22 Signal Problem Rendezvous Problem Two threads A and B Two threads A and B � The statement a1 in A should execute before the statement b1 in B � The statement a1 in A should execute before the statement b2 in B � The statement b1 in B should execute before the statement a2 in B A: B: A: B: statement a1 statement b1 statement a1 statement b1 statement a2 statement b2 statement a2 statement b2 � Initialization � Initialization 23 24

Recommend


More recommend