Sie sind auf Seite 1von 14

Deadlocks

System Model Conditions for Deadlock Deadlock Prevention Deadlock Avoidance Deadlock Detection & Recovery

System Model
A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.
No Efficient solution

Reusable Resources
Used by one process at a time and not depleted by that use Processes obtain resources that they later release Processor time, I/O channels, main and secondary memory, files, databases, and semaphores
Deadlock may occur if each process holds one resource and requests the other

Consumable Resources
Created (produced) and destroyed (consumed) by a process Interrupts, signals, messages, and information in I/O buffers
Deadlocks may occur if a Receive message is blocking May take a rare combination of events to cause deadlock

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

Necessary Conditions for Deadlock


Mutual exclusion
One process holds a resource in a non-sharable mode. Other processes requesting that resource must wait for the resource to be released.

Hold and wait


a process holding at least one resource is waiting to acquire additional resources held by other processes.

No preemption
a resource can be released only voluntarily by the process holding it, after that process has completed its task.

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; ;Pn1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0.

Resource-Allocation Graph
A set of Vertices V and a set of Edges E V is partitioned into two types: P = {P1, P2, , Pn}, the set consisting of all the processes in the system.
R = {R1, R2, , Rm}, the set consisting of all resource types in the system. Request edge directed edge Pi Rj Assignment edge directed edge Rj Pi

Deadlock Example

Graph with cycle but no deadlock

Deadlock Handling
Deadlock Handling
Prevention ensure that at least one of the necessary conditions for deadlock gets violated Avoidance requires the OS know in advance the resource usage requirements of all processes. Then for each request the OS decides if it could lead to a deadlock before granting.
Banker's Algorithm

Detection and Recovery Manual Deadlock Management left to the users or the system operator to detect and recover.

Bankers Algorithm I Safety Algorithm


1. Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i - 1,3, , n. Find an i (that is, a Pi) such that both: Finish [i] == false // Pi has not completed Needi Work // Pi can allocate remaining Resources If no such i exists, go to step 4. Set Work = Work + Allocationi Set Finish[i] = True go to step 2. If Finish [i] == True for all i, then the system is in a safe state. Otherwise it is unsafe.

2.

3.

4.

Bankers Algorithm II Resource-Request


Algorithm for Pi
If Requesti [j] = k,then process Pi wants k instances of resource type Rj
1. 2. 3. If Requesti Needi,, then go to step 2. Otherwise, raise error condition, (process exceeded its claim). If Requesti Available, then go to step 3. Otherwise Pi must wait, since resources are not available. Pretend to allocate requested resources to Pi: Available = Available - Requesti; Allocationi = Allocationi + Requesti; Needi = Needi Requesti;; If safe the resources are allocated to Pi. If unsafe Pi must wait, and the old resource-allocation state is restored

Bankers Algorithm - example


5 processes P0 through P4; 3 resource types A (10 instances), B (5instances, and C (7 instances). Snapshot at time T0: Allocation Max Available ABC ABC ABC P0 010 753 332 P1 200 322 P2 302 902 P3 211 222 P4 002 433

Bankers Algorithm example


The content of the matrix. Need is defined to be Max Allocation. Need ABC P0 743 P1 122 P2 600 P3 011 P4 431 The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria.

Example P1 Request (1,0,2)


Check that Request Available (that is, (1,0,2) (3,3,2) true. Allocation Need Available ABC ABC ABC P0 010 743 230 P1 302 020 P2 301 600 P3 211 011 P4 002 431 Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety requirement. Can request for (3,3,0) by P4 be granted? Can request for (0,2,0) by P0 be granted?

Detection & Recovery


Detection
When, and how often, to invoke the algorithm

Recovery
Abort all deadlocked processes. Abort one process at a time until the deadlock cycle is eliminated. In which order should we choose to abort? Priority of the process. How long process has computed, and how much longer to completion. Resources the process has used. Resources process needs to complete. How many processes will need to be terminated. Is process interactive or batch?

Das könnte Ihnen auch gefallen