Chapter 4 : Deadlock 1By : Jigar M. Pandya. Deadlock 2 A Process Must request a resource before...

Post on 31-Mar-2015

229 views 1 download

Tags:

Transcript of Chapter 4 : Deadlock 1By : Jigar M. Pandya. Deadlock 2 A Process Must request a resource before...

1

Chapter 4 : Deadlock

By : Jigar M. Pandya

By : Jigar M. Pandya 2

DeadlockA Process Must request a resource

before using it.And Process release the resource after

using it.

Sequence of events required to use a resource:

1.Request the resource.2.Use the resource.3.Release the resource.

By : Jigar M. Pandya 3

DeadlockExamaple :

By : Jigar M. Pandya 4

Deadlock

A deadlock consists of a set of blocked processes, each holding a resource and waiting to acquire a resource held by another process in the set.

By : Jigar M. Pandya 5

Deadlock Characterization Deadlock can arise if four conditions hold simultaneously.

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

By : Jigar M. Pandya 6

Deadlock Characterization Deadlock can arise if four conditions hold simultaneously.

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

By : Jigar M. Pandya 7

Deadlock Characterization Deadlock can arise if four conditions hold simultaneously.

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

By : Jigar M. Pandya 8

Deadlock Characterization Deadlock can arise if four conditions hold simultaneously.

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

9By : Jigar M. Pandya

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

By : Jigar M. Pandya 10

Resource-Allocation Graph

Process

Resource Type with 4 instances

Pi requests instance of Rj

Pi is holding an instance of Rj

Pi

Pi

By : Jigar M. Pandya 11

Resource Allocation Graph With A Deadlock

Before P3 requested an instance of R2

After P3 requested an instance of R2

A Cycle In the Graph May cause Deadlock

By : Jigar M. Pandya 12

Graph With A Cycle But No Deadlock

Process P4 may release its instance of resource type R2. That resource can then be allocated to P3, thereby breaking the cycle.

By : Jigar M. Pandya 13

Deadlock

By : Jigar M. Pandya 14

Methods for Handling Deadlocks

Prevention ◦ Ensure that the system will never enter a

deadlock stateAvoidance

◦ Ensure that the system will never enter an unsafe state

Detection ◦ Allow the system to enter a deadlock state and

then recover

Do Nothing ◦ Ignore the problem and let the user or system

administrator respond to the problem; used by most operating systems, including Windows and UNIX

By : Jigar M. Pandya 15

Deadlock Prevention

By : Jigar M. Pandya 16

Deadlock PreventionTo prevent deadlock, we can restrain the ways that a request can be made

 Do not allow one of the four conditions to occur.

Mutual Exclusion :

• if no resource were ever assigned to a single process exclusively ,we would never have deadlock.

• Shared entities (read only files) don't need mutual exclusion (and aren’t susceptible to deadlock.)

• Prevention not possible, since some devices are naturally non-sharable

By : Jigar M. Pandya 17

Deadlock PreventionTo prevent deadlock, we can restrain the ways that a request can be made

 Do not allow one of the four conditions to occur.

Hold And Wait : we 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 it begins execution

• allow a process to request resources only when the process has none

Result: Low resource utilization; starvation possible

By : Jigar M. Pandya 18

Deadlock PreventionTo prevent deadlock, we can restrain the ways that a request can be made

 Do not allow one of the four conditions to occur.

No Preemption : 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

Pi Rj Pj Rk A process will be restarted only when it can

regain its old resources, as well as the new ones that it is requesting.

Allow preemption - if a needed resource is held by another process, which is also waiting on some resource, steal it. Otherwise wait.

By : Jigar M. Pandya 19

Deadlock PreventionTo prevent deadlock, we can restrain the ways that a request can be made

 Do not allow one of the four conditions to occur.

Circular Wait : • impose a total ordering of all resource types, and

require that each process requests resources in an increasing order of enumeration.

F : R NF(ri) < F(rj)

• P1 r1 P2 r2 P3……. Pn-1 rn Pn

For example: F(tape drive) = 1 F(disk drive) = 5 F(printer) = 12

By : Jigar M. Pandya 20

Deadlock Prevention

• EACH of these prevention techniques may cause a decrease in utilization and/or resources. For this reason, prevention isn't necessarily the best technique.

• Prevention is generally the easiest to implement.

21

Deadlock Avoidance

Process must declare all the required resource before starting the execution.

P1 Requires A,B.P2 Requires B,A.

By : Jigar M. Pandya

P1

B

P2

A

1) P1 A

2) P2 BWait Wait Wait

3) P1 B

4) P2 B

5) P2 A

Process P1 Complete Release Resource

Process P2 Complete Release Resourece

22

Deadlock Avoidance

By : Jigar M. Pandya

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

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

23

Deadlock Avoidance

By : Jigar M. Pandya

Possible states are: 

Deadlock No forward progress can be made.

 Unsafe state A state that may allow

deadlock. Safe state A state is safe if a sequence of

processes exist such that there are enough resources for the first to finish, and as each finishes and releases its resources there are enough for the next to finish.

 The rule is simple: If a request allocation would cause an unsafe state, do not honor that request.

24

Deadlock Avoidance

By : Jigar M. Pandya

NOTE: All deadlocks are unsafe, but all unsafes are NOT deadlocks.

SAFEDEADLOCK

UNSAFE

Only with luck will processes avoid

deadlock.

O.S. can avoid deadlock.

25

Deadlock Avoidance

By : Jigar M. Pandya

EXAMPLE:

There exists a total of 12 resources. Each resource is used exclusively by a process. The current state looks like this:

• In this example, < p1, p0, p2 > is a workable sequence. • Free = 2

• Suppose p2 requests and is given one more resource. What happens then?

• Free= 1

Process Max Needs Allocated Current Needs

P0 10 5 5

P1 4 2 2

P2 9 3 7

Process Max Needs Allocated Current Needs

P0 10 5 5

P1 4 2 2

P2 9 4 7

By : Jigar M. Pandya 26

Avoidance algorithms

For a single instance of a resource type, use a resource-allocation graph

For multiple instances of a resource type, use the banker’s algorithm

By : Jigar M. Pandya 27

Resource-Allocation Graph withClaim Edges

Requestedge

Assignmentedge

Claimedge

Claimedge

By : Jigar M. Pandya 28

Unsafe State In Resource-Allocation Graph

Assignmentedge

Requestedge

AssignmentedgeClaim

edge

By : Jigar M. Pandya 29

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

30By : Jigar M. Pandya

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.

Let n = number of processes, and m = number of resources types.

31By : Jigar M. Pandya

Banker’s Algorithm

A method used to determine if a particular state is safe. It's safe if there exists a sequence of processes such that for all the processes, there’s a way to avoid deadlock:

The algorithm uses these variables: 

Need[I] – the remaining resource needs of each process.Work - Temporary variable – how many of the resource

are currently available.Finish[I] – flag for each process showing we’ve analyzed

that process or not.

need <= available + allocated[0] + .. + allocated[I-1] Sign of success

 Let work and finish be vectors of length m and n respectively. 

Safety Algorithm

32By : Jigar M. Pandya

Banker’s Algorithm

1. Initialize work = availableInitialize finish[i] = false, for i = 1,2,3,..n

 2. Find an i such that:

finish[i] == false and need[i] <= work 

If no such i exists, go to step 4. 3. work = work + allocation[i]

finish[i] = truegoto step 2

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

Safety Algorithm

33

Banker’s Algorithm

By : Jigar M. Pandya

Context Of Matrix need Is Calculated As Need = Max - Allocation

By : Jigar M. Pandya 34

Banker’s Algorithm

By : Jigar M. Pandya 35

Banker’s Algorithm

By : Jigar M. Pandya 36

Banker’s Algorithm