Unformatted text preview:

Review Deadlock Starvation vs Deadlock CS162 Operating Systems and Systems Programming Lecture 10 Starvation thread waits indefinitely Deadlock circular waiting for resources Deadlock Starvation but not other way around Four conditions for deadlocks Mutual exclusion Deadlock cont d Thread Scheduling 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 February 18 2010 Ion Stoica http inst eecs berkeley edu cs162 No preemption Resources are released only voluntarily by the threads Circular wait There exists a set T1 Tn of threads with a cyclic waiting pattern 2 18 10 request edge directed edge T1 Rj assignment edge directed edge Rj Ti R2 R1 R2 Lec 10 2 Review Methods for Handling Deadlocks Review Resource Allocation Graph Examples Recall R1 CS162 UCB Fall 2010 Allow system to enter deadlock and then recover R1 Requires deadlock detection algorithm Some technique for selectively preempting resources and or terminating tasks T2 Ensure that system will never enter a deadlock T1 T2 R3 T3 T2 T3 T1 Need to monitor all lock acquisitions Selectively deny those that might lead to deadlock T3 Ignore the problem and pretend that deadlocks never occur in the system R4 Simple Resource Allocation Graph 2 18 10 T1 R3 R4 Allocation Graph With Deadlock CS162 UCB Fall 2010 R2 used by most operating systems including UNIX T4 Allocation Graph With Cycle but No Deadlock 2 18 10 Lec 10 3 Page 1 CS162 UCB Fall 2010 Lec 10 4 Goals for Today Deadlock Detection Algorithm Only one of each type of resource look for loops More General Deadlock Detection Algorithm Preventing Deadlock Scheduling Policy goals Policy Options Implementation Considerations Let X represent an m ary vector of non negative integers quantities of resources of each type FreeResources RequestX AllocX Current free resources each type Current requests from thread X Current resources held by thread X See if tasks can eventually terminate on their own Avail FreeResources Add all nodes to UNFINISHED do done true Foreach node in UNFINISHED if Requestnode Avail remove node from UNFINISHED Avail Avail Allocnode done false until done Note Some slides and or pictures in the following are adapted from slides 2005 Silberschatz Galvin and Gagne Gagne Many slides generated from my lecture notes by Kubiatowicz 2 18 10 CS162 UCB Fall 2010 R1 T1 2 18 10 R1 T2 T3 R2 Available 1 0 RequestT1 1 0 RequestT1 Available T4 T1 T3 R2 R1 T4 CS162 UCB Fall 2010 T1 T3 R2 CS162 UCB Fall 2010 T4 Lec 10 6 In Bridge example Godzilla picks up a car hurls it into the river Deadlock solved Shoot a dining philosopher But not always possible killing a thread holding a mutex leaves world inconsistent Preempt resources without killing off thread T2 Take away resources from thread temporarily Doesn t always fit with semantics of computation Roll back actions of deadlocked threads T3 R2 T2 What to do when detect deadlock Terminate thread force it to give up resources Available 1 1 RequestT3 0 1 RequestT3 Available T2 T1 Nodes left in UNFINISHED deadlocked 2 18 10 Lec 10 5 Deadlock Detection Algorithm Example Available 0 0 RequestT2 0 0 RequestT2 Available R1 Hit the rewind button on TiVo pretend last few minutes never happened For bridge example make one car roll backwards may require others behind him Common technique in databases transactions Of course if you restart in exactly the same way may reenter deadlock once again T4 2 18 10 Lec 10 7 Page 2 CS162 UCB Fall 2010 Lec 10 8 Techniques for Preventing Deadlock Infinite resources Techniques for Preventing Deadlock con t Make all threads request everything they ll need at the beginning 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 Problem Predicting future is hard tend to overestimate resources Example Bay bridge with 12 000 lanes Never wait Infinite disk space not realistic yet 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 No Sharing of resources totally independent threads Not very realistic Force all threads to request resources in a particular order preventing any cyclic use of resources Don t allow waiting How the phone company avoids deadlock Thus preventing deadlock Example x P y P z P 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 2 18 10 CS162 UCB Fall 2010 2 18 10 Lec 10 9 Review Train Example Wormhole Routed Network Circular dependency Deadlock Lec 10 10 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 Fix Imagine grid extends in all four directions Banker s algorithm less conservative Force ordering of channels tracks Allocate resources dynamically Protocol Always go east west first then north south Called dimension ordering X then Y Evaluate each request and grant if some ordering of threads is still deadlock free afterward Technique pretend each request is granted then run deadlock detection algorithm substituting Maxnode Allocnode Avail for Requestnode Avail Grant request if result is deadlock free conservative Keeps system in a SAFE state i e there exists a sequence T1 T2 Tn with T1 requesting all remaining resources finishing then T2 requesting all remaining resources etc d we lo le al u is R D By CS162 UCB Fall 2010 CS162 UCB Fall 2010 Banker s Algorithm for Preventing Deadlock Each train wants to turn right Blocked by other trains Similar problem to multiprocessor networks 2 18 10 Make tasks request disk then memory then Keep from deadlock on freeways around SF by requiring everyone to go clockwise Algorithm allows the sum of maximum resource needs of all current threads to be greater than total resources 2 18 10 Lec 10 11 Page 3 CS162 UCB Fall 2010 Lec 10 12 Banker s Algorithm Example Administrivia Project 1 code due this Monday 2 22 Autograder will be available by tomorrow morning Midterm coming up in two 1 2 weeks Tuesday 3 9 3 30 6 30 Requested this room Should be 2 hour exam with extra time Closed book one page of hand written notes both sides Banker s


View Full Document

Berkeley COMPSCI 162 - Deadlock Thread Scheduling

Documents in this Course
Lecture 1

Lecture 1

12 pages

Nachos

Nachos

41 pages

Security

Security

39 pages

Load more
Loading Unlocking...
Login

Join to view Deadlock Thread Scheduling and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Deadlock Thread Scheduling and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?