49
Chapter 7: Deadlocks The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks Don’t care Deadlock Prevention Deadlock Avoidance Deadlock Detection and Recovery (from Deadlock)

Mch7 deadlock

  • Upload
    wahab13

  • View
    324

  • Download
    2

Embed Size (px)

Citation preview

Chapter 7: Deadlocks

The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks

Don’t care Deadlock Prevention Deadlock Avoidance Deadlock Detection and Recovery (from Deadlock)

Chapter Objectives

To develop an understanding of deadlocks, which prevents a set of concurrent processes from completing their tasks, while waiting for some resources occupied by each other indefinitely

To present a number of different methods for preventing, avoiding and handling deadlocks in a computer system.

Example: Narrow Bridge Crossing

Traffic can flow only in one direction on a bridge Each section of a bridge can be viewed as a

resource. If a deadlock occurs, it can be resolved if one car

backs up (preempt resources and rollback). Several cars may have to be backed up, if a

deadlock occurs. Starvation is also possible.

Traffic Crossing

R1

R2

R3

R4

Traffic 1

Traffic 3

Traffic 2

Traffic 2

Traffic 2

There are 4 resources R1,R2,R3 and R4

There are 4 users. traffic T1,T2,T3 and T4

Deadlock: Traffic Crossing

R1

R2

R3

R4

Traffic 1

Traffic 3

Traffic 2

Traffic 2

Traffic 2

Deadlock

T1 holds resource R1, waiting for R2

T2 holds resource R2, waiting for R3

T3 holds resource R3, waiting for R4

T4 holds resource R4, waiting for R1

Circular waitUser T1 waiting for T2 to completeUser T2 waiting for T3 to completeUser T3 waiting for T4 to completeUser T4 waiting for T1 to complete

The Deadlock ProblemDeadlock: A state in which a set of blocked processes,

each holding one or few resources and waiting to acquire one or few resources held by other processes in the set, in a cycle.

Example A System has only 2 disk drives. P1 and P2 each hold one disk drive and each needs another one. Keep the resource Occupied and wait for the resource occupied

by others Example

Concurrent processes P0 and P1 share semaphores A and B, which are initialized to 1

P0 P1

wait (A); wait(B)wait (B); wait(A)……… ………

Process P0 waiting for process P1 to release (signal) semaphore B and P1 is waiting for semaphore A to be signaled by P0

Resources System Model Resource types R1, R2, . . ., Rm

Physical:-CPU cycles, memory space, I/O devices Virtual:-That exists only logically (through software) Critical:-That is sharable, but can be allocated to only

one requester Multiple Resources: Each resource type Ri may

have Wi instances. Utilization Protocol: Each process utilizes a

resource as follows: requests uses releases

Deadlock Characterization

Mutual exclusion: Exclusive use of a resource, only by one process at a time.

Hold and wait: A process holding at least one resource, is waiting to acquire additional resources held by other processes.

Non preemption: A resource cannot be released, until that process has completed its task.

Circular wait: there exists a set {P0, …, P0} of waiting processes such that P0 is waiting for a resource that is being held by P1, P1 is waiting for a resource that is held by P2, so on … and Pn is waiting for a resource that is held by P0.

Deadlock can occur if following conditions hold simultaneously.

Resource-Allocation Graph (to determine deadlock in a system)

A Graph consists of a set of vertices V and a set of edges E

V is partitioned into two types: Processes = {P1, P2, …, Pn}, the set of all the processes in the

system. Resources = {R1, R2, …, Rm}, the set of all resource types in the

system.

E is partitioned into two types: Request edge: A process requesting a resource

– directed edge Pi Rk

Assignment (Allocation) edge: A resource allocated to a process

– directed edge Rj Pk

.

Resource-Allocation Graph (Processes and Multiple instances of resources.) Process

Resource Type with 1 & 4 instances

Pi requests an instance of Rj

Pi is holding an instance of RjPi

Pi

Rj

Rj

Example of a Resource Allocation Graph

Resource Allocation Graph With A Deadlock

Graph With A Cycle But No Deadlock

Basic Facts

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.

Methods for Handling Deadlocks1. Ignore the problem and pretend that deadlocks

never occur in the system; used by most operating systems, including UNIX.

2. Ensure that the system will never enter in a deadlock state.

Prevention Avoidance

3. Allow the system to enter a deadlock state detect it and then recover.

Detection and Recovery

Deadlock Prevention

Mutual Exclusion – not required for sharable resources; must hold for non-sharable resources.

Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. Requires a process to release the allocated resources

before it requests for more (break hold condition), i.e. request only when the process has no resource.

Low resource utilization; starvation possible.

Bulk allocation of all required resources (by process itself or optionally by OS), in the beginning, if possible, otherwise reject request.

Alternatively break any necessary condition to prevent deadlock

Deadlock Prevention (Cont.) Non Preemption –

If a process that is holding some resource, requests another resource that cannot be immediately allocated to it, then all resources currently being held are released.

Preempted resources are added to the list of resources for which the process is waiting.

Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting.

Circular Wait – impose a total ordering of all resource types, and each process requests resources in an increasing order of enumeration. (Break circularity: no process could request already allocated resource)

Deadlock Avoidance

Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need.

The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition, before servicing a request for resource allocation.

Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes.

Requires that the system has some additional a priori information available.

Safe, Unsafe , Deadlock Threat

Maximum Need Allocated claim

P0 10 6 4

P1 4 2 2

P2 9 2 7

Current state: Total resources = 12 Available = 2

Safe: if next allocation is done to process P1 (leaves 1 tape, needs of P1 can be fulfilled that will return 4)

Unsafe: if next allocation is done to process P0 or P2 (needs of none can be fulfilled)

No deadlock threat

Basic Facts If a system is in safe state no threat of

deadlock.

If a system is in unsafe state possibility of deadlock (since processes usually state maximum need, but may not use all resources).

Avoidance ensure that a system will never enter in an unsafe state, before allocation of resources.

Safe Allocation (Banker’s Algorithm) When a process requests a resource, system decides, if current allocation leaves the system in a safe state then it allocates the resource, otherwise denies allocation.

System is in safe state if there exists a possibility that current allocation leaves some of the processes that can complete their execution and the resources thereby released can satisfy needs of some other processes

That is: If Pi needs a resource, already allocated to Pj , and is not

immediately available, then Pi can wait until all Pj have finished.

When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate.

When Pi terminates, Pk can obtain its needed resources, and so on.

Avoidance algorithms In case of Single instance of a resource type.

Use a resource-allocation graph Multiple instances of a resource type. Use the

banker’s algorithm Know total needs of borrowers Do not allocate all money (resources) at a time Keep track of allocations and claims (still needed) Allocate only, if current allocation leaves enough

reserves to fulfill the need of some one else, who will return all borrowed money to fulfill need of others

Resource-Allocation Graph Scheme Claim edge Pi Rj indicated that process Pj may request resource Rj; represented by a dashed line.

Claim edge converts to request edge when a process requests a resource.

Request edge converted to an assignment edge when the resource is allocated to the process.

When a resource is released by a process, assignment edge reconverts to a claim edge.

Resources must be claimed a priori in the system.

Resource-Allocation GraphR1 resource has been allocated to P1, P1 may request R2 resource

P2 have request for R1 resource and may request R2 resource

Assignment Edge Request Edge

May request (needed)

Unsafe State In Resource-Allocation GraphR1 resource has been allocated to P1, P1 may request R2 resourceP2 have request for R1 resource and R2 resource has been allocated to P2

No threat of deadlock until P1 requests R2 resource

Resource-Allocation Graph Algorithm Suppose that process Pi requests a resource Rj

The request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph (Circular wait)

Banker’s Algorithm (Single Resource Type)

Multiple instances of single type of resources. Each process must a priori claim maximum need

(State total need). Allocation in parts, not in total When a process requests a resource it may have

to wait. When a process gets all its resources it must

return them in a finite amount of time. Returned resources can fulfill other’s need

Banker’s Algorithm (Safe Allocation)Algorithm for one type, that can be extended for more types of resources.

Safe-allocation is a procedure to avoid deadlocks, invoked in an allocation procedure

Needed[ ] and Allocated[ ] Arrays contain no. of still needed and allocated resources for each process. Following program supposedly allocates resource to nth process among all P processes, calls safe_allocation procedure, if safe, make actual allocation, otherwise not

Allocate (n) { // make allocation for nth processAllocated [n] = Allocated [n] + 1 // Assume allocation for nth processNeeded[ n] = Needed [ n ] -1 // decrease need of one resource

If (safe_ allocation ( P ) ) {

// go ahead for actual allocation

}

else {Allocated [n] = Allocated [n] - 1 // revert supposed allocation Needed[ n] = Needed [ n ] + 1 // reset need

}

}

Cont. Banker’s Algorithm (Safe Allocation)Safe-allocation algorithm for single type of resource (Say Mag. Tapes), but of multiple

instances. It can be extended for other types of resources

logical safe-allocation( int P) // P is total number of currently running processes

{

logical finish [ ] ; // To contain indication that process can finish or complete

logical done = FALSE; // Checked all

int resources = TOTAL ; // Total available resources of this type

for ( n=0 ; n<P ; n++) // do for all processes

{

finish [ n ] = FALSE ; // assume nth process can not finish

resources = resources – Allocated [ n ]; // Presently available resources

}

Cont: safe-allocation procedure.while ( ! done )

{done = TRUE ; // assume all processes are done / checkedfor (n=0; n < P ; n++) // check processes that can pay-back, if finished{

if ( ! finish [n] ) && Needed [n] <= resources ){ finish [n] = TRUE; // Oh, It can complete resources = resources + Allocated [ n ]; // reclaim resources done = FALSE ; // needs further checking

}}

}

if ( resources == TOTAL ) // all processes finished, all resources returnedreturn ( TRUE ); // allocation is safe

elsereturn ( FALSE );

} // end of safe-allocation procedure

Example: Single Type

A

0

.

5

6

7

6

7

F

8

.

3

2

1

2

1

A

0

-

4

4

4

4

5

N

6

-

2

2

2

2

1

A

0

-

0

1

1

1

1

N

3

-

3

2

2

2

2

A

0

-

1

1

2

1

1

N

4

-

3

3

2

3

3 Safe

Unsafe

Safe

Safe

Resources Process 1 Process 2 Process 3

A: Allocated; F: Free; N : still Needed Request

Data Structures: Banker’s Algorithm (multiple types)

Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj available.

Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj. (It can be avoided by using only needed array)

Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.

Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task.

Need [i,j] = Max[i,j] – Allocation [i,j].

Extension to multiple types of resources:- (use multidimensional array)

Let n = Maximum number of processes,

and m = number of resource types. {one vector (dimension) for each resource type}

Safety Algorithm1.Let Work and Finish be vectors of length m and

n, respectively. Initialize:Work = AvailableFinish [i] = false for i = 0, 1, …, n- 1.

2.Find an i such that both: (a) Finish [i] = false

(b) Needi WorkIf no such i exists, go to step 4.

3.Work = Work + Allocationi

Finish[i] = truego to step 2.

4.If Finish [i] == true for all i, then the system is in a safe state.

Resource-Request Algorithm for Process PiRequest = request vector for process Pi. If Requesti [j] = k

then process Pi wants k instances of resource type Rj.

1. If Requesti Needi go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim.

2. If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not available.

3. Pretend to allocate requested resources to Pi by modifying the state as follows:

Available = Available – Request; (Vector operation)

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

Example of Banker’s Algorithm 5 processes P0 through P4;

3 resource types (A, B and C):

A (10 instances), B (5instances), and C (7 instances).

Resource Vector is (A,B,C) or (10,5,7) Snapshot at some time T0:

Allocation Max Available

A B C A B C A B C

P0 0 1 0 7 5 3 3 3 2

P1 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

Example (Cont.) The content of the matrix Need is defined to be Max –

Allocation.

Need

A B C

P0 7 4 3

P1 1 2 2

P2 6 0 0

P3 0 1 1

P4 4 3 1

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

A B C A B C A B C

P0 0 1 0 7 4 3 2 3 0

P1 3 0 2 0 2 0

P2 3 0 1 6 0 0

P3 2 1 1 0 1 1

P4 0 0 2 4 3 1 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?

Banker’s Algorithm Example P1 Request (1,0,2)Processes

Total:

P0

P1

P2

P3

P4

1 0 2 < 3 3 2

Max. Need

A B C

7 5 3

3 2 2

9 0 2

2 2 2

4 3 3

Req. by P1

Allocated

A B C

0 1 0

2 0 0

3 0 2

2 1 1

0 0 2

Allocate it

Claim

A B C

7 4 3

1 2 2

6 0 0

0 1 1

4 3 1

Available

10 5 7

10 4 7

8 4 7

5 4 5

3 3 4

3 3 2

(1 0 2) P1

P1 (0 2 0)

P3 (0 1 1)

P4 (4 3 1)

P0 (7 4 3)

P2 (6 0 0)

3 2 2

3 2 2

2 2 2

4 3 3

7 5 3

9 0 2

3 0 2

3 2 2

2 2 2

4 3 3

7 5 3

9 0 2

0 2 0

- - -

- - -

- - -

- - -

- - -

2 3 0

5 3 2

7 4 3

7 4 5

7 5 5

10 5 7

230 + 302

532 + 211

Reclaiming

755 + 302

Reclaiming:done = FALSE while ( ! done ) { done = TRUE ; for (n=0; n < P ; n++) { if ( ! finish [n] ) && Needed [n] <= resources ) { finish [n] = TRUE; resources = resources + Allocated [ n ]; done = FALSE ; } } }

Tracing algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies and the request for (1,0,2) by P1 be granted?

Allocation

Deadlock Detection and Recovery(adopted on systems with remote chances of deadlock)

Allows system to enter into deadlock state No need to pre state resources need Saves time otherwise consumed on running algorithms while

allocating each resource Detection algorithm

Execute automatically after regular intervals or interactively When system or a group of processes look “BUSY, DOING

NOTHING” When ready queue is nearly empty and blocked queues are heavily

populated When idle process is taking most of the execution time

Recovery scheme detecting deadlock, adopt conserving approach

Single Instance of Each Resource Type Maintain wait-for graph

Nodes are processes. Pi Pj if Pi is waiting for Pj.

Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock.

An algorithm to detect a cycle in a graph requires an order of n2 operations, where n is the number of vertices in the graph.

Resource-Allocation Graph and Wait-for Graph

Resource-Allocation Graph Corresponding wait-for graph

Several Instances of a Resource Type Available: A vector of length m indicates the

number of available resources of each type.

Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process.

Request: An n x m matrix indicates the current request of each process. If Request [ij] = k, then process Pi is requesting k more instances of resource type. Rj.

Detection Algorithm

1.Let Work and Finish be vectors of length m and n, respectively Initialize:(a) Work = Available

(b) For i = 1,2, …, n, if Allocationi 0, then Finish[i] = false;otherwise, Finish[i] = true.

2.Find an index i such that both:(a) Finish[i] == false

(b) Requesti Work

If no such i exists, go to step 4.

Detection Algorithm (Cont.)

3.Work = Work + Allocationi

Finish[i] = truego to step 2.

4.If Finish[i] == false, for some i, 1 i n, then the system is in deadlock state. Moreover, if Finish[i] == false, then Pi is deadlocked.

Algorithm requires an order of O(m x n2) operations to detect whether the system is in deadlocked state.

Example of Detection Algorithm Five processes P0 through P4; three resource types

A (7 instances), B (2 instances), and C (6 instances). Snapshot at time T0:

AllocationRequestAvailableA B C A B C A B C

P0 0 1 0 0 0 0 0 0 0

P1 2 0 0 2 0 2

P2 3 0 3 0 0 0

P3 2 1 1 1 0 0

P4 0 0 2 0 0 2 Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true

for all i.

Example (Cont.) P2 requests an additional instance of type C.

Request

A B C

P0 0 0 0

P1 2 0 1

P2 0 0 1

P3 1 0 0

P4 0 0 2 State of system?

Can reclaim resources held by process P0, but insufficient resources to fulfill other processes; requests.

Deadlock exists, consisting of processes P1, P2, P3, and P4.

Detection-Algorithm Usage When, and how often, to invoke depends on:

How often a deadlock is likely to occur? How many processes will need to be rolled back?

one for each disjoint cycle

If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we would not be able to tell which of the many deadlocked processes “caused” the deadlock.

Recovery from Deadlock: Process Termination

Abort all deadlocked processes.

Abort one process at a time until the deadlock cycle is eliminated.

Considerations in order to choose a process 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?

Recovery from Deadlock: Resource Preemption Selecting a victim – minimize cost.

Rollback – return to some safe state, restart process for that check pointed state.

Starvation Threat– same process may always be picked as victim for denial of resource allocation, include number of rollback in cost factor.