chapter 7: deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · operating...

42
Deadlocks

Upload: vuongkhanh

Post on 24-Mar-2018

217 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

Deadlocks

Page 2: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Deadlocks

The Deadlock Problem

System Model

Deadlock Characterization

Methods for Handling Deadlocks

Deadlock Prevention

Deadlock Avoidance

Deadlock Detection

Recovery from Deadlock

Page 3: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

The Deadlock Problem

A set of blocked processes each holding a resource and

waiting to acquire a resource held by another process in the

set.

Example

System has 2 disk drives.

P1 and P2 each hold one disk drive and each needs

another one.

Page 4: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Bridge Crossing Example

Traffic only in one direction.

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

Page 5: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

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

Page 6: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Deadlock Characterization

Mutual exclusion: only one process at a time can use a resource.

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

Deadlock can arise if four conditions hold simultaneously.

Page 7: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Resource-Allocation Graph

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 multi-set consisting of all

resource types in the system.

request edge – directed edge P1 Rj

assignment edge – directed edge Rj Pi

A set of vertices V and a set of edges E.

Page 8: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.8 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Resource-Allocation Graph (Cont.)

Process

Resource Type with 4 instances

Pi requests instance of Rj

Pi is holding an instance of Rj

Pi

Pi

Rj

Rj

Page 9: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.9 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Example of a Resource Allocation Graph

Page 10: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.10 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Resource Allocation Graph With A Deadlock

Page 11: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.11 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Graph With A Cycle But No Deadlock

Page 12: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.12 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

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.

Page 13: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.13 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Methods for Handling Deadlocks

We can use protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlock state.

We can allow the system to enter a deadlock state, detect it, and recover.

Deadlock prevention and deadlock detection algorithm is used for ignoring the deadlocks. Deadlock prevention is a set of methods for ensuring that at least one of the necessary condition cannot hold. These methods prevent deadlocks by constraining how requests for resources can be made.

Deadlocks avoidance requires that the operating system be given in advance, additional information concerning which resources, a process will request and use during its lifetime.

If a system does not employ either a deadlock prevention or a deadlock avoidance algorithm, then a deadlock situation may occur.

If a system does not ensure that a deadlock will never occur and also does not provide a mechanism for deadlock detection and recovery, then the system is in a deadlock state.

Page 14: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.14 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Deadlock Prevention

Methods for preventing deadlock are of two classes : indirect method and direct method.

An indirect method is to prevent the occurrence of one of the three necessary condition i.e. mutual exclusion, hold and wait and no preemption.

A direct method is to prevent the occurrence of a circular wait.

Mutual Exclusion – Mutual exclusion condition must hold for non-sharable resource. If access to a resource requires mutual exclusion, then mutual exclusion must be supported by the operating system.

Some resources, such as files, may allow multiple accesses for reads but only exclusive access for writes.

In this case, deadlock can occur if more than one process requires write permission.

Page 15: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.15 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Deadlock Prevention (Cont.)

Hold & Wait- The hold and wait condition can be eliminated by forcing a process to release all resources held by it whenever it requests a resource that is not available.

For example, process copies data from a floppy disk to a hard disk, sort a disk file and then prints the results to printer.

If all the resources must be requested at the beginning of the process, then the process must initially request the floppy disk, hard disk and a printer.

It will hold the printer for its entire execution, even though it needs the printer only at then end.

In these two method, resource utilization is low in the first method and second method is affected by the starvation.

Page 16: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.16 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Deadlock Prevention (Cont.)

No Preemption -If a process holding certain resources is denied a further request. That process must release its original resources and if necessary request them again together with additional resource.

If a process requests a resource that is currently held by another process, the operating system may preempt the second process and require it to release its resources.

In general, sequential I/O device cannot be preempted.

Preemption is possible for certain types of resources, such as CPU and main memory.

Circular-Wait-One way to prevent the circular-wait condition is by linear ordering of different types of system resources. In this system resources are divided into different classes.

If a process has been allocated resources of type R, then it may subsequently request only those resource types. Following R in the ordering

Page 17: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.17 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

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.

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.

Page 18: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.18 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Safe State

When a process requests an available resource, system must

decide if immediate allocation leaves the system in a safe state.

System is in safe state if there exists a sequence <P1, P2, …, Pn>

of ALL the processes is the system such that for each Pi, the

resources that Pi can still request can be satisfied by currently

available resources + resources held by all the Pj, with j < i.

That is:

If Pi resource needs are 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, Pi +1 can obtain its needed resources,

and so on.

Page 19: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.19 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Safe, Unsafe , Deadlock State

Page 20: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.20 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Basic Facts

System is in a safe state no deadlocks

If system is deadlocked state is unsafe

System is in unsafe state possibility of deadlock.

OS cannot prevent processes from requesting

resources in a sequence that leads to deadlock

Deadlock Avoidance

Ensures system will never enter an unsafe state

Thereby avoids the possibility of deadlock

Page 21: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.21 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Safe States: An Example

Suppose processes P0, P1, and P2 share 12 magnetic tape drives

Currently 9 drives are held among the processes and 3 are available

Question: Is this system currently in a safe state?

Answer: Yes!

Safe Sequence: <P1, P0, P2>

Page 22: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.22 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

How to reach an Unsafe State

Suppose process P2 requests and is allocated 1 more tape drive.

Question: Is the resulting state still safe?

Answer: No! Because there does not exist a safe sequence anymore.

Only P1 can be allocated its maximum needs.

IF P0 and P2 request 5 more drives and 6 more drives,

respectively, then the resulting state will be deadlocked.

3

Page 23: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.23 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Deadlock Avoidance - Concepts

Key Ideas:

Initially the system is in a safe state

Whenever a process requests an available resource,

system will allocate resource immediately only if the

resulting state is still safe!

Otherwise, requesting process must wait.

Why does this work?

By induction, all reachable states are safe states

By definition, all safe states are not deadlocked

Page 24: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.24 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Avoidance Algorithms

Single instance of a resource type.

Use a resource-allocation graph

Cycles are necessary are sufficient for deadlock

Multiple instances of a resource type.

Use the banker’s algorithm

Cycles are necessary, but not sufficient for deadlock

Page 25: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.25 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Resource-Allocation Graph Scheme

Claim edge Pi Rj indicates 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.

Page 26: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.26 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Resource-Allocation Graph

P2 requesting R1, but R1 is already allocated to P1.

Both processes have a claim on resource R2

What happens if P2 now requests resource R2?

Page 27: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.27 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Unsafe State In Resource-Allocation Graph

Cannot allocate resource R2 to process P2

Why? Because resulting state is unsafe

P1 could request R2, thereby creating deadlock!

Page 28: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.28 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Resource-Allocation Graph Algorithm

Use only when there is a single instance of each resource type

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

Page 29: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.29 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Banker’s Algorithm

Multiple instances.

Each process claims maximum resource needs a priori.

When a process requests a resource it may have to wait.

When a process gets all of its resources it must return

them in a finite amount of time.

Page 30: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.30 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Data Structures for the Banker’s Algorithm

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.

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

Let n = number of processes

m = number of resources types

Page 31: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.31 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Safety Algorithm

1. Let Work and Finish be vectors of length m and n, respectively. Initialize:

Work = Available

Finish [i] = false for i = 0, 1, …, n- 1.

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] = true go to step 2.

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

Page 32: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.32 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Resource-Request Algorithm for Process Pi

Request = 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;

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

Page 33: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.33 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Example of Banker’s Algorithm

5 processes P0 through P4;

3 resource types:

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

Snapshot at 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

Page 34: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.34 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

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.

Page 35: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.35 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

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?

Page 36: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.36 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Deadlock Detection

If a system does not employ either a deadlock prevention or a deadlock-avoidance algorithm, then a deadlock situation may occur.

In this environment the system must provide

An algorithm tat examines the state of the system to determine whether a deadlock has occurred

An algorithm to recover from the deadlock.

Page 37: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.37 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Single Instance of Each Resource Type

If all resources have only a single instance, then deadlock detection algorithm that uses a variant of the resource allocation graph, called wait-for graph.

Wait-for graph is obtained from resource-allocation graph. Nodes of resource is removed and collapsing the appropriate edge. i.e. assignment and request edge.

Page 38: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.38 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Resource-Allocation Graph and Wait-for Graph

Resource-Allocation Graph Corresponding wait-for graph

Page 39: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.39 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Several Instances of a Resource Type

The wait for graph scheme is not applicable to a resource allocation system with multiple instances of each 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.

Page 40: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.40 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Recovery from Deadlock: Process Termination

Abort all deadlocked processes.

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

Aborting a process may not easy. If the process was in the midst of updating a file, terminating it will leave that file in an incorrect state. Similarly, if the process was in the midst of printing data on the printer, the system must reset the printer to a correct state before printing the next job.

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.

Page 41: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

7.41 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005

Recovery from Deadlock: Resource Preemption

1. Selecting a Victim : - Which resources and which processes are to be preempted? As in process termination, we must determine the order of preemption to minimize cost. Cost factors may include such parameters as the number of resources a deadlock process is holding, and the amount of time a deadlocked process has thus far consumed during its execution.

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

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

Page 42: Chapter 7: Deadlocksstudy-study.weebly.com/uploads/1/1/9/6/11963458/chapter-7.pdf · Operating System Concepts - th7 Edition, Feb 14, 2005 7.13 Silberschatz, ... Suppose processes

End of Chapter