Deadlocks: Part II Detection and RecoveryDining Philophers ProblemFour requirements for DeadlockPowerPoint PresentationTechniques for Preventing DeadlockTechniques for Preventing Deadlock (con’t)Banker’s Algorithm for Avoiding DeadlockBanker’s AlgorithmBasic AlgorithmSafety CheckBanker’s Algorithm: ExampleSlide 12Slide 13Banker’s Algorithm ExampleRemaining deadlock discussionDeadlock Detection & RecoveryUsing the RAG Algorithm to detect deadlocksResource-Allocation Graph and Wait-for Graph2nd Detection AlgorithmSlide 20ExampleSlide 22Slide 23Slide 24Slide 25When to run Detection Algorithm?Deadlock RecoveryFYI: Java 1.5 Concurrency ToolsSummarySummary (2)Deadlocks: Part IIDetection and Recovery2Dining Philophers Problem•Five chopsticks/Five philosophers (really cheap restaurant)–Free-for all: Philosopher will grab any one they can–Need two chopsticks to eat•What if all grab at same time?–Deadlock!•How to prevent deadlock?–Make one of them give up a chopstick (Hah!)–Eventually everyone will get chance to eat•How to avoid deadlock?–Never let philosopher take last chopstick if no philosopher has two chopsticks afterwards3Four requirements for Deadlock•Mutual exclusion–Only one thread at a time can use a resource.•Hold and wait–Thread holding at least one resource is waiting to acquire additional resources held by other threads•No preemption–Resources are released only voluntarily by the thread holding the resource, after thread is finished with it•Circular wait–There exists a set {T1, …, Tn} of waiting threads•T1 is waiting for a resource that is held by T2•T2 is waiting for a resource that is held by T3•…•Tn is waiting for a resource that is held by T14•We saw that you can prevent deadlocks. –By negating one of the four necessary conditions. (which are..?)•We saw that the OS can schedule processes in a careful way so as to avoid deadlocks.–Using a resource allocation graph.–Banker’s algorithm.–What are the downsides to these?The story so far..5Techniques for Preventing Deadlock•Infinite resources–Include enough resources so that no one ever runs out of resources. Doesn’t have to be infinite, just large–Give illusion of infinite resources (e.g. virtual memory)–Examples:•Brooklyn bridge with 12,000 lanes. Never wait!•Infinite disk space (not realistic yet?)•No Sharing of resources (totally independent threads)–Not very realistic•Don’t allow waiting –How the phone company avoids deadlock•Call to your Mom in Toledo, works its way through the phone lines, but if blocked get busy signal. –Technique used in Ethernet/some multiprocessor nets•Everyone speaks at once. On collision, back off and retry–Inefficient, since have to keep retrying•Consider: driving to New York City; when hit traffic jam, suddenly you’re transported back home and told to retry!6Techniques for Preventing Deadlock (con’t)•Make all threads request everything they’ll need at the beginning.–Problem: Predicting future is hard, tend to over-estimate resources–Example:•If need 2 chopsticks, request both at same time•Don’t leave home until we know no one is using any intersection between here and where you want to go; only one car on the Bay Bridge at a time•Force all threads to request resources in a particular order preventing any cyclic use of resources–Thus, preventing deadlock–Example (x.P, y.P, z.P,…)•Make tasks request disk, then memory, then…•Keep from deadlock on freeways around SF by requiring everyone to go clockwise7•Toward right idea: –State maximum resource needs in advance–Allow particular thread to proceed if:(available resources - #requested) max remaining that might be needed by any thread•Banker’s algorithm (less conservative):–Allocate resources dynamically•Evaluate each request and grant if some ordering of threads is still deadlock free afterward •Technique: pretend each request is granted, then run safety algorithm, - Check for ([Maxnode]-[Allocnode] ≤ [Avail])- Grant request if result is deadlock free (conservative!)•Keeps system in a “SAFE” state, i.e. there exists a sequence {P1, P2, … Pn} with P1 requesting all remaining resources, finishing, then T2 requesting all remaining resources, etc..–Algorithm allows the sum of maximum resource needs of all current threads to be greater than total resourcesBanker’s Algorithm for Avoiding Deadlock8Banker’s Algorithm•Decides whether to grant a resource request. •Data structures:n: integer # of processesm: integer # of resourcesavailable[1..m] available[i] is # of avail resources of type imax[1..n,1..m] max demand of each Pi for each Riallocation[1..n,1..m] current allocation of resource Rj to Pineed[1..n,1..m] max # resource Rj that Pi may still requestneedi = maxi - allocationilet request[i] be vector of # of resource Rj Process Pi wants9Basic Algorithm1. If request[i] > need[i] then error (asked for too much)2. If request[i] > available[i] then wait (can’t supply it now)3. Resources are available to satisfy the requestLet’s assume that we satisfy the request. Then we would have:available = available - request[i]allocation[i] = allocation [i] + request[i]need[i] = need [i] - request [i]Now, check if this would leave us in a safe state:if yes, grant the request, if no, then leave the state as is and cause process to wait.10Safety Checkfree[1..m] = available /* how many resources are available */finish[1..n] = false (for all i) /* none finished yet */Step 1: Find an i such that finish[i]=false and need[i] <= work /* find a proc that can complete its request now */ if no such i exists, go to step 3 /* we’re done */Step 2: Found an i:finish [i] = true /* done with this process */ free = free + allocation [i] /* assume this process were to finish, and its allocation back to the available list */go to step 1Step 3: If finish[i] = true for all i, the system is safe. Else Not11Banker’s Algorithm: Example Allocation Max Available A B C A B C A B CP0 0 1 0 7 5 3 3 3 2P1 2 0 0 3 2 2 P2 3 0 2 9 0 2 P3 2 1 1 2 2 2 P4 0 0 2 4 3 3 this is a safe state: safe sequence <P1, P3, P4, P2, P0>Suppose that P1 requests (1,0,2) - add it to P1’s allocation and subtract it from Available12Banker’s Algorithm: Example
View Full Document