422f7647e37735ab9970a2e0c5444fcc.ppt
- Количество слайдов: 18
CSS 430 Deadlocks Textbook Ch 7 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials. CSS 430 Deadlocks 1
Deadlock Examples 1 Kansas Legislature: when two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone. Two processes exchange a long message with each other, but their socket buffer is smaller than the message. Process A message Socket buffer Process B Socket buffer CSS 430 Deadlocks 2
Deadlock Examples 2 Bridge crossing example: traffic only in one direction where two cars are driving from the opposite direction. A deadlock is not resolved unless one gets back up. Two processes try to go into the same nested critical section in a different order. P 0 wait (A); wait (B); P 1 wait(B) wait(A) CSS 430 Deadlocks 3
System Model n Resource types R 1, R 2, . . . , Rm CPU cycles, memory space, I/O devices n n Each resource type Ri has Wi instances. Each process utilizes a resource as follows: n n n request use release CSS 430 Deadlocks 4
Deadlock Characterization Deadlock can arise if four conditions hold simultaneously. n n Mutual exclusion: only one process at a time can use a resource. Hold and wait: a process holding resource(s) is waiting to acquire additional resources held by other processes. No preemption: a resource can be released only voluntarily by the process holding it upon its task completion. Circular wait: there exists a set {P 0, P 1, …, P 0} of waiting processes such that P 0 is waiting for a resource that is held by P 1, P 1 is waiting for a resource that is held by P 2, …, Pn– 1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P 0. CSS 430 Deadlocks 5
Resource Allocation Graph n n Resource Type with 4 instances n The sequence of Process’s recourse utilization Process Pi requests instance of Rj Pi Rj n Pi is holding an instance of Rj Request edge Pi Assignment edge n Pi releases an instance of Rj CSS 430 Deadlocks Rj Pi 6
Resource-allocation graph R 3 R 1 P 2 R 2 Can a deadlock happen? P 3 R 4 CSS 430 Deadlocks 7
Resource Allocation Graph With A Deadlock There are two cycles found. CSS 430 Deadlocks 8
Resource Allocation Graph With A Cycle But No Deadlock n n If graph contains no cycles no deadlock. If graph contains a cycle n n CSS 430 Deadlocks if only one instance per resource type, then deadlock. if several instances per resource type, possibility of deadlock. 9
Methods for Handling Deadlocks n n n Ensure that the system will never enter a deadlock state. (Prevention and Avoidance) Allow the system to enter a deadlock state and then recover. (Detection and Recovery) Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX. CSS 430 Deadlocks 10
Deadlock Prevention Restrain the following four conditions n n Mutual Exclusion – not required for sharable resources. (but not work always. ) Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. n Require a process to request and be allocated all its resources before its execution: Low resource utilization n Allow process to request resources only when the process has none: starvation possible. No Preemption – n If a process holding some resources requests another resource that cannot be immediately allocated to it, all resources currently being held are released. n If a process P 1 requests a resource R 1 that is allocated to some other process P 2 waiting for additional resource R 2, R 1 is allocated to P 1. Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. CSS 430 Deadlocks 11
Deadlock Prevention Circular Wait P 4 Not allowed Order(tape)=1 < Order(printer)=4 tape disk 1 scanner 2 P 1 CSS 430 Deadlocks P 2 3 printer 4 P 3 12
Deadlock Avoidance Resource-Allocation Algorithm Let processes supply OS with future resource requests Cycle possibly formed (unsafe state), thus P 2 has to wait for a safe state Claim edge (future request) Works out to only a single instance of each resource type. CSS 430 Deadlocks 13
Deadlock Avoidance Banker’s Algorithm n n n Multiple instances Each process must claim maximum use in advance. Requested resources are allocated only when there is a sequence all processes successfully obtain and release their resources. Proc P 0 P 1 P 2 P 3 P 4 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 Max A B C 7 5 3 3 2 2 9 0 2 2 4 3 3 Need Initial Available A B C 7 4 3 10 5 7 3 3 2 1 2 2 6 0 0 0 1 1 4 3 1 CSS 430 Deadlocks 14
Deadlock Avoidance Banker’s Algorithm – P 1 requested (1, 0, 2) n n n Request (1, 0, 2) <= Available(3, 3, 2) is true Temporarily allocate it to P 1 Work = Available = (2, 3, 0) Finish(f, f, f) if Finish[i] == false && Need[i] <= Work { Work += Allocation[i]; Finish[i] = true; } // allow Pi to obtain all its needs and to release them to available. Proc added > Max A B C 7 5 3 3 2 2 9 0 2 2 >4 3 3 added Need Initial Available A B C 7 4 3 10 5 7 3 3 2 2 3 0 1 2 2 0 6 0 0 < 0 1 1 4 3 1 < P 0 P 1 P 2 P 3 P 4 Allocation A B C 0 1 0 2 0 0 3 0 2 2 1 1 0 0 2 > added Work: (2, 3, 0)->(5, 3, 2) -> (7, 4, 3) -> (7, 4, 5) -> (7, 5, 5) -> (10, 5, 7) CSS 430 Deadlocks Thus, safe 15
Deadlock Detection and Recovery n Deadlock Detection: n n Cyclically construct resource-allocation graph and find a cycle in it. Recovery: n Process termination n Abort all deadlocked processes Abort processes one by one till a cycle is cut off. Successively preempt resources n n n Selecting a victim Should not select the same victim infinitely! Rolling back a resource use Ensuring avoiding starvation CSS 430 Deadlocks 16
Exercises (No turn-in) 1. 2. 3. Why aren’t deadlock detection and recovery so attractive? Solve Exercise 7. 3, 7. 6, 7. 9, 7. 10, 7. 14, and 7. 19 Can the Java code in the next slide cause a deadlock? If so, write a resource allocation graph with a deadlock. CSS 430 Deadlocks 17
public class Deadlock { public Deadlock( ) { Mutex mutex[] = new Mutex[4]; for ( int i = 0; i < 4; i++ ) mutex[i] = new Mutex( ); A thread. A = new A( mutex ); B thread. B = new B( mutex ); C thread. C = new C( mutex ); } thread. A. start( ); thread. B. start( ); thread. C. start( ); public static void main( String arg[] ) { Deadlock d = new Deadlock( ); } private class B extends Thread { private Mutex[] resource; public B( Mutex[] m ) { resource = m; } public void run( ) { System. out. println( "B started" ); synchronized ( resource[3] ) { System. out. println( "B got rsc 3" ); synchronized ( resource[0] ) { System. out. println( "B got rsc 0" ); synchronized ( resource[2] ) { System. out. println( "B got rsc 2" ); } } } System. out. println( "B finished" ); } } class Mutex{ } private class A extends Thread { private Mutex[] resource; public A( Mutex[] m ) { resource = m; } public void run( ) { System. out. println( "A started" ); synchronized ( resource[1] ) { System. out. println( "A got rsc 1" ); synchronized ( resource[0] ) { System. out. println( "A got rsc 0" ); } } System. out. println( "A finished" ); } } } private class C extends Thread { private Mutex[] resource; public C( Mutex[] m ) { resource = m; } public void run( ) { System. out. println( "C started" ); synchronized ( resource[2] ) { System. out. println( "C got rsc 2" ); synchronized ( resource[1] ) { System. out. println( "C got rsc 1" ); } } System. out. println( "C finished" ); } } CSS 430 Deadlocks 18
422f7647e37735ab9970a2e0c5444fcc.ppt