chap7.doc

30
Chapter 7 – Deadlock Resources Examples of computer resources Printers Tape drives Tables Preemptable resources Can be taken away from a process with no ill effects Nonpreemptable resources Will cause the process to fail if taken away Reusable resources Used by one process at a time and not depleted by that use Examples: Processors, I/O channels, main and secondary memory, files, databases, and semaphores Shared and exclusive resources Example of shared resource: FILE Example of exclusive resource: PRINTER Consumable resources Created (produced) and destroyed (consumed) by a process Prepared by Dr. Amjad Mahmood 7.1

Transcript of chap7.doc

Chapter 7 Deadlock

Chapter 7 Deadlock

Resources Examples of computer resources

Printers

Tape drives

Tables

Preemptable resources

Can be taken away from a process with no ill effects

Nonpreemptable resources

Will cause the process to fail if taken away

Reusable resources

Used by one process at a time and not depleted by that use

Examples: Processors, I/O channels, main and secondary memory, files, databases, and semaphores

Shared and exclusive resources

Example of shared resource: FILE

Example of exclusive resource: PRINTER

Consumable resources

Created (produced) and destroyed (consumed) by a process

Examples: Interrupts, signals, messages, and information in I/O buffers

System Model

A system consists of a number of resources to be distributed among a number of competing processes.

There are different types of resources R1, R2,..., Rm.

CPU cycles, memory space, I/O devices Each resource type Ri has Wi instances. For example, if two CPUs then resource type CPU has two instances.

Sequence of Events Required to Use a Resource

Each process utilizes a resource as follows:

Request a resource: Request is made through a system call

Process must wait if request is denied

Requesting process may be blocked

may fail with error code

Use the resource: The process can operate on the resource.

Release the resource: The process releases the resource. A resource is released through a system call.

Deadlock Formal Definition

A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause

Usually the event is release of a currently held resource

None of the processes can

Run

Release resources

Be awakened

Involve conflicting needs for resources by two or more processes

Examples of Deadlock Example 1

System has 2 tape drives. P1 and P2 each hold one tape drive and each needs another one.

Example 2 Semaphores A and B, initialized to 1

P0

P1

wait (A);

wait(B)

wait (B);

wait(A)

Example 3 Space is available for allocation of 200K bytes, and the following sequence of events occurP0

.

Request 80KB;

Request 60KB;P1

Request 70KB;

Request 80KB;

Deadlock occurs if both processes progress to their second requestFour Conditions for Deadlock Deadlock can arise if four conditions hold simultaneously Mutual exclusion condition: Only one process at a time can use a resource (non-shareable resource). Each resource is assigned to a process or is available Hold and wait condition: A process holding at least one resource can request for additional resources No preemption condition: A resource can be released only voluntarily by the process holding it. That is previously granted resources cannot be forcibly taken away. Circular wait condition: 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

Deadlocks can be described more precisely in terms of a directed graph, called a system resource-allocation graph.

This graph consists of 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.

E is partitioned into two types as well:

Request edge directed edge P1 ( Rj

Assignment edgedirected edge Rj (Pi Different symbols are used to represent processes and resources as given below:

Process:

Resource type of 4 instances:

Pi requests instance of Rj:

Pi is holding an instance of Rj:

Method of Handling Deadlocks

Just ignore the problem altogether Prevention

Ensure that the system will never enter a deadlock state Requires negating one of the four necessary conditions

Dynamic avoidance

Require careful resource allocation

Detection and recovery Allow the system to enter a deadlock state and then recover We need some methods to determine whether or not the system has entered into deadlock.

We also need algorithms to recover from the deadlock.The Ostrich Algorithm

Pretend there is no problem The system will eventually stop functioning

Reasonable if

Deadlocks occur very rarely

Cost of prevention is high

UNIX and Windows takes this approach

It is a trade off between

Convenience

Correctness

Deadlock Prevention Prevent/deny Mutual Exclusion condition Use shareable resource. Impossible for practical system. Prevent/Deny Hold and Wait condition(a) Pre-allocation - Require processes to request resources before starting A process never has to wait for what it needs(b) Process must give up all resources and then request all immediately needed

Problems

May not know required resources at start of run Low resource utilization many resources may be allocated but not used for long time Starvation possible a process may have to wait indefinitely for popular resources. Prevent/deny No Preemption condition (a) If a process that is holding some resources 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.(b) The required resource(s) is/are taken back from the process(s) holding it/them and given to the requesting process

Problems Some resources (e.g. printer, tap drives) cannot be preempted without detrimental implications.

May require the job to restart

Prevent/Deny Circular Wait Order resources (each resource type is assigned a unique integer) and allow process to request for them only in increasing order If a process needs several instances of the same resource, it should issue a single request for all of them.

Alternatively, we can require that whenever a process requests an instance of a resource type it has released all the resources which are assigned a smaller inter value. Problem:

Adding a new resource that upsets ordering requires all code ever written to be modified

Resource numbering affects efficiency

A process may have to request a resource well before it needs it, just because of the requirement that it must request resources in ascending order An example:

Deadlock Avoidance

OS never allocates resources in a way that could lead to a deadlock

Processes must tell OS in advance how many resources they will request

Some Definitions

State of a system

An enumeration of which processes hold, are waiting for or might request which resource

Safe state

1. No process is deadlocked, and there exits no possible sequence of future request in which deadlock could occur

2. No process is deadlocked and the current state will not lead to a dead lock state

3. Safe state is where there is at least one sequence that does not result in deadlock Unsafe state Is a state that is not safe

Basic Facts

If a system is in safe state ( no deadlocks.

If a system is in unsafe state ( possibility of deadlock.

Avoidance ( ensure that a system will never enter an unsafe stateDeadlock Avoidance with Resource-Allocation Graph This algorithm can be used if we have only one instance of each resource type.

In addition to the request and assignment edges, a claim edge is also introduced.

Claim edge Pi ( Rj indicated that process Pj may request resource Rj in future; represented by a dashed line.

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

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

Resources must be claimed a priori in the system. That is, before a process starts executing, all of its claim edges must already appear in the resource-allocation graph.

Suppose that process Pi requests resource Rj. The request can be granted only if converting the request edge if converting the request edge Pi(Rj to an assignment edge does not result in a cycle in the resource-allocation graph. That is we use a cycle detection algorithm is used. If no cycle exits, the process Pi will have to wait.

Resource-allocation graph for deadlock avoidance

An unsafe state in the resource-allocation graph

Bankers Algorithm

Applicable to system with multiple instances of resource types.

Each process must a priori claim maximum use.

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. Bankers algorithm runs each time: A process requests resource Is it sage?

A process terminates Can I allocate released resources to a suspended process waiting for them? A new state is safe if and only if every process can complete after allocation is made

Make allocation and then check system state and deallocate if unsafeData Structures for Bankers algorithm

Let n = number of processes, and m = number of resources types. Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj available. Max: n x m matrix. Max [i,j] = k mean that process Pi may request at most k instances of Rj.

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].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.

2. Find and i such that both:

(a) Finish [i] = false

(b) Needi ( Work

If 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 = 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 restoredExample of Bankers Algorithm

5 processes P0 through P4; 3 resource types

A (10 instances), B (5 instances), and

C (7 instances).Snapshot at time T0:

AllocationMaxAvailable

ABCABCABC

P00 1 07 5 33 3 2

P12 0 03 2 2

P23 0 29 0 2

P32 1 12 2 2

P40 0 24 3 3

The content of the matrix. Need is defined to be Max Allocation.ProcessNeed

A B C

P07 4 3

P11 2 2

P26 0 0

P30 1 1

P44 3 1

The system is in a safe state since the sequence satisfies safety criteria. Example P1 Request (1,0,2) Check that Request ( Available

that is, (1,0,2) ( (3,3,2) ( true.

ProcessAllocationNeedAvailable

A B CA B CA B C

P00 1 07 4 32 3 0

P13 0 20 2 0

P23 0 16 0 0

P32 1 10 1 1

P40 0 24 3 1

Executing safety algorithm shows that sequence satisfies safety requirement.

Can request for (3,3,0) by P4 be granted?

Can request for (0,2,0) by P0 be granted?

Deadlock Detection Recovery Allow system to enter deadlock state

Need a detection algorithm

Need a recovery algorithm

How to Detect a Deadlock Using a Resource-Graph?

If each resource type has exactly one instance and the graph has a cycle then a deadlock has occurred. Or if the cycle involves only a set of resource types, each of which has only a single instance, then the deadlock has occurred.

Therefore, a cycle in the graph is both a necessary and sufficient condition for the existence of a deadlock.

Examples:

Resource-allocation graph with a deadlock

Recovery from Deadlocks Process Termination

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?

Recovery from Deadlocks Resource Preemption

Selecting a victim minimize cost.

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

Starvation same process may always be picked as victim, include number of rollback in cost factor.

Combined Approach to Deadlock Handling

Combine the three basic approaches prevention

avoidance

detection

allowing the use of the optimal approach for each of resources in the system.

Partition resources into hierarchically ordered classes.

Use most appropriate technique for handling deadlocks within each class.

Rj

Rj

Pi

Pi

PAGE 7.19Prepared by Dr. Amjad Mahmood