Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7: Deadlock CSS503 Systems Programming

Similar presentations


Presentation on theme: "Chapter 7: Deadlock CSS503 Systems Programming"— Presentation transcript:

1 Chapter 7: Deadlock CSS503 Systems Programming
Prof. Munehiro Fukuda Computing & Software Systems University of Washington Bothell CSS503 Chapter 7: Deadlock

2 Deadlock Example Can the following parent/child process exchange messages? #define LOOP 100 int main( int argc, char *argv[] ) { if ( argc != 2 ) { cerr << "usage: deadlock_processes bufsize" << endl; return -1; } int size = atoi( argv[1] ); char *buf = new char[size]; int toChild[2], toParent[2]; // two pipes pipe( toChild ); pipe( toParent ); if ( fork( ) != 0 ) { // parent close( toChild[0] ); close( toParent[1] ); for ( int i = 0; i < LOOP; i++ ) { cout << "Round " << i << ": parent will send a message." << endl; // write a message write( toChild[1], buf, size ); // read a message write( toParent[0], buf, size ); cout << "Round " << i << ": parent got a message." << endl; wait( NULL ); cout << "Parent synchronized with child." << endl; Parent Child message toChild toParent [0] [1] } else { // child close( toChild[1] ); close( toParent[0] ); for ( int i = 0; i < LOOP; i++ ) { cout << "Round " << i << ": child will send a message." << endl; // write a message write( toParent[1], buf, size ); // read a message write( toChild[0], buf, size ); cout << "Round " << i << ": child got a message." << endl; } exit( 0 ); CSS503 Chapter 7: Deadlock

3 System Model Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices Each resource type Ri has Wi instances. Each process utilizes a resource as follows: request use release CSS503 Chapter 7: Deadlock

4 Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously. 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 {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0. CSS503 Chapter 7: Deadlock

5 Resource Allocation Graph
Pi Rj Process Resource Type with 4 instances Pi requests instance of Rj Pi is holding an instance of Rj Pi releases an instance of Rj The sequence of Process’s recourse utilization Request edge Assignment edge CSS503 Chapter 7: Deadlock

6 Resource-allocation graph
Can a deadlock happen? CSS503 Chapter 7: Deadlock

7 Resource Allocation Graph With A Deadlock
There are two cycles found. CSS503 Chapter 7: Deadlock

8 Resource Allocation Graph With A Cycle But No Deadlock
If graph contains no cycles  no deadlock. If graph contains a cycle  if only one instance per resource type, then deadlock. if several instances per resource type, possibility of deadlock. CSS503 Chapter 7: Deadlock

9 Methods for Handling Deadlocks
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. CSS503 Chapter 7: Deadlock

10 Deadlock Prevention Restrain the following four conditions
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. Require a process to request and be allocated all its resources before its execution: Low resource utilization Allow process to request resources only when the process has none: starvation possible. No Preemption – If a process holding some resources requests another resource that cannot be immediately allocated to it, all resources currently being held are released. If a process P1 requests a resource R1 that is allocated to some other process P2 waiting for additional resource R2, R1 is allocated to P1. Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. CSS503 Chapter 7: Deadlock

11 Deadlock Prevention Circular Wait
Not allowed Order(tape)=1 < Order(printer)=4 tape disk scanner printer 1 2 3 4 P1 P2 P3 CSS503 Chapter 7: Deadlock

12 Deadlock Avoidance Resource-Allocation Algorithm
Let processes supply OS with future resource requests Cycle possibly formed (unsafe state), thus P2 has to wait for a safe state Claim edge (future request) Works out to only a single instance of each resource type. CSS503 Chapter 7: Deadlock 11

13 Deadlock Avoidance Banker’s Algorithm
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 Allocation Max Need Initial Available A B C A B C A B C A B C A B C P P P P P CSS503 Chapter 7: Deadlock 12

14 Deadlock Avoidance Banker’s Algorithm – P1 requested (1,0,2)
Request (1,0,2) <= Available(3,3,2) is true Temporarily allocate it to P1 Work = Available = (2,3,0) Finish(f, f, 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 Allocation Max Need Initial Available A B C A B C A B C A B C A B C P P P P P 3 0 2 2 3 0 0 2 0 added < added > added < added > added > Work: (2,3,0)->(5,3,2) -> (7,4,3) -> (7,4,5) -> (7,5,5) -> (10,5,7) CSS503 Chapter 7: Deadlock Thus, safe

15 Deadlock Detection and Recovery
Cyclically construct resource-allocation graph and find a cycle in it. Recovery: Process termination Abort all deadlocked processes Abort processes one by one till a cycle is cut off. Successively preempt resources Selecting a victim Rolling back a resource use Ensuring avoiding starvation Should not select the same victim infinitely! CSS503 Chapter 7: Deadlock

16 Discussion Can the following pthreads cause a deadlock?
If so, write a resource allocation graph with a deadlock. #define LOOP 100 pthread_mutex_t resource[3]; void *threadA( void *arg ) { cout << "A starts" << LOOP << endl; for ( int i = 0; i < LOOP; i++ ) { pthread_mutex_lock( &resource[1] ); cout << "Round " << i << ": A got rsc 1" << endl; pthread_mutex_lock( &resource[0] ); cout << "Round " << i << ": A got rsc 0" << endl; pthread_mutex_unlock( &resource[0] ); pthread_mutex_unlock( &resource[1] ); } cout << "A finished" << endl; void *threadB( void *arg ) { cout << "B starts" << LOOP << endl; pthread_mutex_lock( &resource[3] ); cout << "Round " << i << ": B got rsc 3" << endl; cout << "Round " << i << ": B got rsc 0" << endl; pthread_mutex_lock( &resource[2] ); cout << "Round " << i << ": B got rsc 2" << endl; pthread_mutex_unlock( &resource[2] ); pthread_mutex_unlock( &resource[3] ); cout << "B finished" << endl; void *threadC( void *arg ) { cout << "C starts" << endl; for ( int i = 0; i < LOOP; i++ ) { pthread_mutex_lock( &resource[2] ); cout << "Round " << i << ": C got rsc 2" << endl; pthread_mutex_lock( &resource[1] ); cout << "Round " << i << ": C got rsc 1" << endl; pthread_mutex_unlock( &resource[1] ); pthread_mutex_unlock( &resource[2] ); } cout << "C finished" << endl; int main( int argc, char *argv[] ) { pthread_t tid[3]; pthread_create( &tid[0], NULL, threadA, NULL ); pthread_create( &tid[1], NULL, threadB, NULL ); pthread_create( &tid[2], NULL, threadC, NULL ); for ( int i = 0; i < 3; i++ ) pthread_join( tid[i], NULL ); return 0; CSS503 Chapter 7: Deadlock


Download ppt "Chapter 7: Deadlock CSS503 Systems Programming"

Similar presentations


Ads by Google