resource allocation, deadlock and banker’s algorithm 60-330 supplementary notes dr. r. d. kent...

Post on 18-Dec-2015

218 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Resource Allocation, Deadlock and Banker’s Algorithm

60-330 Supplementary NotesDr. R. D. Kent

Last modified: Dec. 11, 2006

Introduction

These slides are intended to illustrate and clarify some details of deadlock detection and avoidance applied to resource allocation. In particular, they clarify examples in the

textbook by providing significant detail Assume that all resource types and the

number of instances of each type are known by the O/S Initially configured at system startup/bootstrap Tracked along with processes as they

Request resources for the first time Use resources dynamically

Assumed Knowledge (1)

Configured at startup and updated regularly All resource types and the number of instances

of each type Stored in a vector called Available with M entries (one

for each resource type). Available[ j ] = k ; /* implies there are k instances of

resource type j */

At a given, specific time The maximum resource demand of each of N

processes Stored in a NxM matrix Max Max[ i ][ j ] = k ; /* process Pi may request at most k

instances of resource type Rj

Assumed Knowledge (2)

At a given, specific time: The number of resources of each type currently

allocated to each of N processes Stored in a NxM matrix Allocation Allocation[ i ][ j ] = k ; /* process Pi is currently

allocated k instances of resource type Rj

The remaining resource needs of each process Stored in a NxM matrix Need Need[ i ][ j ] = k ; /* process Pi may need k instances of

resource type Rj to complete its task NOTE: Need = Max – Allocation ;

Assumed Knowledge (3)

Comments: The contents of these data structures varies

over time as the processes progress to completion through the states

Sometimes it is convenient to refer to an entire row of an NxM matrix X as Xi, where Xi is a vector of length M that is associated with process Pi.

Notation: Assume X, Y are vectors of length N X ≤ Y if and only if X[j] ≤ Y[j] for all j = 1.. N Ex. X = (1,7,3,2) and Y = (0,3,2,1) implies Y ≤ X

Assumed Knowledge (4)

Comments: (continued) The required vector and matrix data structures

may be declared (statically) at compile time Need to consider the maximum number of processes

(large) and resources (small) that the hardware and operating system can manage. Ex.

int Available [ MAX_Process ] ; int Max [MAX_Process ] [ MAX_Resource ] ;

If structures are allocated dynamically, code must be developed to manage the heap

Banker’s Algorithm

Used to determine safe resource allocation to processes Safety implies no deadlocks will occur

By studying different scenarios of possible resource allocation, we try to find at least one scenario that is deadlock free, or safe

Time consuming (algorithm complexity)

Resource allocation is performed using safe scenarios Can a request be safely granted?

Banker’s Algorithm

At a given time assume that current values are stored in structures Define:

NP :: Number of current processes NR :: Number of currently available resources

Data Structures: Available – vector with NR entries Max, Allocation, Need – matrices with NP

rows and NR entries in each row

Safety Algorithm

Periodically, we need to determine if the system in a safe state.

Define vectors int Work [ NR ] ; int Finish [ NP ] ;

Initialize Work = Available ; For all k, Finish[ k ] = false ; /* 0 */

Safe State:For a set of processes there exists at least one schedule of allocating resources so that each

process can progress to completion.

Safety Algorithm

Goal: If all values Finish [ k ] == true (or 1), then the system is in a safe state. Starting at k = 0 (ie. first process).

First step: If both Finish[ k ] == false and Need k ≤ Work

Then goto Third step, otherwise continue.

Second step: Work = Work + Allocation k ;

Finish[ k ] = true ; /* or 1 */ k = k + 1 ; if k < NR then goto First step, otherwise report “No safe state exists”.

Safety Algorithm

Third step: If, for all k from 0 to NR-1,

Finish[ k ] == true

Then system is in a safe state.

Note that it is possible that no safe state exists (see Second step). Therefore there is possibility of deadlock.

Resource-Request Algorithm

We need to determine if a resource request can be safely granted.

Define an NxM matrix Request that contains all current resource requests (from M resource types) for all N processes.

Request k is the resource request vector (a row in the matrix) for the process Pk.

Resource request information (integer values) is obtained when jobs are submitted (through job headers).

Resource-Request Algorithm

When Pk requests resources:

Step 1: If Requestk ≤ Needk , goto Step 2, otherwise report

“Processes exceed max. claim”

Step 2: If Requestk ≤ Available, goto Step 3, otherwise make

Pk wait since resources are not available

Resource-Request Algorithm

Step 3: (Prove the scenario is safe)

Available = Available - Requestk ; Allocationk = Allocationk + Requestk ; Needk = Needk – Requestk ;

If the resulting resource-allocation scenario is safe, the allocate the resources to Pk to complete the transaction. Requires applying the Safety Algorithm

If the state is not safe, then Pk must wait for Requestk , and the old state is restored.

Example: Safe State

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 0 1 0 7 5 3

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

Available [0] [1] [2]

3 3 2

Various processes have requested the maximum amount of resources they intend to use (Max). They are allocated resources so that, at a given moment, one knows the Allocation matrix and also what is currently Available.

Is the system in a safe state?

What is a safe sequence?

Following the textbook, we assume that the maximum resources available are: (10, 5, 7)

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 0 1 0 7 5 3

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

Available [0] [1] [2]

3 3 2

Need [0] [1] [2]

P0 7 4 3

P1 1 2 2

P2 6 0 0

P3 0 1 1

P4 4 3 1

Determine current Need = Max – Allocation

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 0 1 0 7 5 3

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

Available [0] [1] [2]

3 3 2

Need [0] [1] [2]

P0 7 4 3

P1 1 2 2

P2 6 0 0

P3 0 1 1

P4 4 3 1

Determine current Need = Max – Allocation

Max – Allocation → Need :: 3 – 2 = 1 2 – 0 = 2 2 – 0 = 2

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 0 1 0 7 5 3

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

Available [0] [1] [2]

3 3 2

Need [0] [1] [2]

P0 7 4 3

P1 1 2 2

P2 6 0 0

P3 0 1 1

P4 4 3 1

Determine all processes that can be satisfied by the Available resources.

NOTE: Both P1 and P3 are candidates, but we choose P1 because it occurs next.

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 0 1 0 7 5 3

P1 3 2 2 3 2 2

P2 3 0 2 9 0 2

P3 2 1 1 2 2 2

P4 0 0 2 4 3 3

Available [0] [1] [2]

2 1 0

Need [0] [1] [2]

P0 7 4 3

P1 - - -

P2 6 0 0

P3 0 1 1

P4 4 3 1

Now perform the Allocation of Needed resources to P1

Add Need

Subtract Need

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 0 1 0 7 5 3

P1 3 2 2 3 2 2

P2 3 0 2 9 0 2

P3 2 1 1 2 2 2

P4 0 0 2 4 3 3

Available [0] [1] [2]

2 1 0

Need [0] [1] [2]

P0 7 4 3

P1 - - -

P2 6 0 0

P3 0 1 1

P4 4 3 1

Once a process’ needs are fulfilled, free those resources to re-allocate to the next process in sequence.

Available [0] [1] [2]

5 3 2

Since the Maximum requirement is fulfilled, the process can run to completion in order to free its resources to other processes.

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 0 1 0 7 5 3

P1 - - - 3 2 2

P2 3 0 2 9 0 2

P3 2 1 1 2 2 2

P4 0 0 2 4 3 3

Available [0] [1] [2]

5 3 2

Need [0] [1] [2]

P0 7 4 3

P1 - - -

P2 6 0 0

P3 0 1 1

P4 4 3 1

Reapply the updated Available to Need and arrive at the choice of P3 (P4 would work, but occurs later).

Then, free its resources.

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 0 1 0 7 5 3

P1 - - - 3 2 2

P2 3 0 2 9 0 2

P3 2 2 2 2 2 2

P4 0 0 2 4 3 3

Available [0] [1] [2]

5 2 1

Need [0] [1] [2]

P0 7 4 3

P1 - - -

P2 6 0 0

P3 - - -

P4 4 3 1

Reapply the updated Available to Need and arrive at the choice of P3 (P4 would work, but occurs later).

Then, free its resources.

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 0 1 0 7 5 3

P1 - - - 3 2 2

P2 3 0 2 9 0 2

P3 - - - 2 2 2

P4 0 0 2 4 3 3

Available [0] [1] [2]

7 4 3

Need [0] [1] [2]

P0 7 4 3

P1 - - -

P2 6 0 0

P3 - - -

P4 4 3 1

Reapply the updated Available to Need and arrive at the choice of P4 (the next one that is satisfied).

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 0 1 0 7 5 3

P1 - - - 3 2 2

P2 3 0 2 9 0 2

P3 - - - 2 2 2

P4 4 3 3 4 3 3

Available [0] [1] [2]

3 1 2

Need [0] [1] [2]

P0 7 4 3

P1 - - -

P2 6 0 0

P3 - - -

P4 - - -

Allocate the Need resources to P4.

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 0 1 0 7 5 3

P1 - - - 3 2 2

P2 3 0 2 9 0 2

P3 - - - 2 2 2

P4 - - - 4 3 3

Available [0] [1] [2]

7 4 5

Need [0] [1] [2]

P0 7 4 3

P1 - - -

P2 6 0 0

P3 - - -

P4 - - -

Allocate the Need resources to P4.

Then, since P4 can run to completion, free its resources.

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 0 1 0 7 5 3

P1 - - - 3 2 2

P2 3 0 2 9 0 2

P3 - - - 2 2 2

P4 - - - 4 3 3

Available [0] [1] [2]

7 4 5

Need [0] [1] [2]

P0 7 4 3

P1 - - -

P2 6 0 0

P3 - - -

P4 - - -

Both P0 and P2 can be satisfied from Available resources. Choose P0 to Allocate the Need. (NOTE: the textbook chooses P2 for this example.)

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 7 5 3 7 5 3

P1 - - - 3 2 2

P2 3 0 2 9 0 2

P3 - - - 2 2 2

P4 - - - 4 3 3

Available [0] [1] [2]

0 0 2

Need [0] [1] [2]

P0 - - -

P1 - - -

P2 6 0 0

P3 - - -

P4 - - -

Both P0 and P2 can be satisfied from Available resources. Choose P0 to Allocate the Need. (NOTE: the textbook chooses P2 for this example.)

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 - - - 7 5 3

P1 - - - 3 2 2

P2 3 0 2 9 0 2

P3 - - - 2 2 2

P4 - - - 4 3 3

Available [0] [1] [2]

7 5 5

Need [0] [1] [2]

P0 - - -

P1 - - -

P2 6 0 0

P3 - - -

P4 - - -

Both P0 and P2 can be satisfied from Available resources. Choose P0 to Allocate the Need. (NOTE: the textbook chooses P2 for this example.)

Then, free the resources

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 - - - 7 5 3

P1 - - - 3 2 2

P2 3 0 2 9 0 2

P3 - - - 2 2 2

P4 - - - 4 3 3

Available [0] [1] [2]

7 5 5

Need [0] [1] [2]

P0 - - -

P1 - - -

P2 6 0 0

P3 - - -

P4 - - -

Finally, we handle P2 which can clearly be satisfied because, as required,

Need ≤ Available

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 - - - 7 5 3

P1 - - - 3 2 2

P2 9 0 2 9 0 2

P3 - - - 2 2 2

P4 - - - 4 3 3

Available [0] [1] [2]

1 5 5

Need [0] [1] [2]

P0 - - -

P1 - - -

P2 - - -

P3 - - -

P4 - - -

Finally, we handle P2 which can clearly be satisfied because, as required,

Need ≤ Available

Example

Allocation

[0] [1] [2] Max [0] [1] [2]

P0 - - - 7 5 3

P1 - - - 3 2 2

P2 - - - 9 0 2

P3 - - - 2 2 2

P4 - - - 4 3 3

Available [0] [1] [2]

10 5 7

Need [0] [1] [2]

P0 - - -

P1 - - -

P2 - - -

P3 - - -

P4 - - -

Freeing resources may have to be limited by the actual maximum resource availability (not specified for this example). Also, recall that processes request maximum resources throughout their lifetime, not those needed immediately.

Example

Need [0] [1] [2]

P0 7 4 3

P1 1 2 2

P2 6 0 0

P3 0 1 1

P4 4 3 1

This leads to the possible resource allocation orderings:

P1, P3, P4, P0, P2

Again, this is different than discussed in the textbook (P2 comes first, then P0).

For these sequences the vector

Finish [ k ] == true for all NP=5 processes

1

2

3

4

5

Available [0] [1] [2]

10 5 7

All of the stated Needs have been shown to be allocatable from the total allowed resource set Available.

NOTE: All resources have now been reclaimed, in agreement with initial assumption.

Example: Resource Allocation

Allocation [0] [1] [2] Need [0] [1] [2]

P0 0 1 0 7 4 3

P1 2 0 0 1 2 2

P2 3 0 2 6 0 0

P3 2 1 1 0 1 1

P4 0 0 2 4 3 1

Available [0] [1] [2]

3 3 2

We established that the state is safe.

If P1 makes a new request: Request 1 = { 1, 0, 2 }

Can this request be granted safely and immediately?

We started with the state tables populated with safe state values.

Example

Allocation [0] [1] [2] . Need

[0] [1] [2]

P0 0 1 0 7 4 3

P1 2+1=3 0+0=0 0+2=2 1-1=0 2-0=2 2-2=0

P2 3 0 2 6 0 0

P3 2 1 1 0 1 1

P4 0 0 2 4 3 1

Available [0] [1] [2]

3-1=2 3-0=3 2-2=0

For: Request 1 = { 1, 0, 2 }

Assume it can be allocated immediately.

Example

Allocation [0] [1] [2] . Need [0] [1] [2]

P0 0 1 0 7 4 3

P1 3 0 2 0 2 0

P2 3 0 2 6 0 0

P3 2 1 1 0 1 1

P4 0 0 2 4 3 1

Available [0] [1] [2]

2 3 0

For: Request 1 = { 1, 0, 2 }

After modifying the Need and Available, then test for safety.

Example

Allocation [0] [1] [2] . Need

[0] [1] [2]

P0 0 1 0 7 4 3

P1 3 2 2 0 0 0

P2 3 0 2 6 0 0

P3 2 1 1 0 1 1

P4 0 0 2 4 3 1

Available [0] [1] [2]

2 1 0

For: Request 1 = { 1, 0, 2 }

After modifying the Need and Available, then test for safety.

We find a sequence: { P1,

Allocate resources to P1

Example

Allocation [0] [1] [2] . Need

[0] [1] [2]

P0 0 1 0 7 4 3

P1 - - - - - -

P2 3 0 2 6 0 0

P3 2 1 1 0 1 1

P4 0 0 2 4 3 1

Available [0] [1] [2]

5 3 2

For: Request 1 = { 1, 0, 2 }

After modifying the Need and Available, then test for safety.

We find a sequence: { P1, P3,

Free P1 resources,Find P3:

Next <= Available

Example

Allocation [0] [1] [2] . Need

[0] [1] [2]

P0 0 1 0 7 4 3

P1 - - - - - -

P2 3 0 2 6 0 0

P3 2 2 2 0 0 0

P4 0 0 2 4 3 1

Available [0] [1] [2]

5 2 1

For: Request 1 = { 1, 0, 2 }

After modifying the Need and Available, then test for safety.

We find a sequence: { P1, P3,

Allocate resources to P3

Example

Allocation [0] [1] [2] . Need

[0] [1] [2]

P0 0 1 0 7 4 3

P1 - - - - - -

P2 3 0 2 6 0 0

P3 - - - - - -

P4 0 0 2 4 3 1

Available [0] [1] [2]

7 4 3

For: Request 1 = { 1, 0, 2 }

After modifying the Need and Available, then test for safety.

We find a sequence: { P1, P3, P4,

Free P3 resources,Find P4 (next):

Next <= Available

Example

Allocation [0] [1] [2] . Need

[0] [1] [2]

P0 0 1 0 7 4 3

P1 - - - - - -

P2 3 0 2 6 0 0

P3 - - - - - -

P4 4 3 3 0 0 0

Available [0] [1] [2]

3 1 2

For: Request 1 = { 1, 0, 2 }

After modifying the Need and Available, then test for safety.

We find a sequence: { P1, P3, P4,

Allocate resources to P4

Example

Allocation [0] [1] [2] . Need

[0] [1] [2]

P0 0 1 0 7 4 3

P1 - - - - - -

P2 3 0 2 6 0 0

P3 - - - - - -

P4 - - - - - -

Available [0] [1] [2]

7 4 5

For: Request 1 = { 1, 0, 2 }

After modifying the Need and Available, then test for safety.

We find a sequence: { P1, P3, P4, P0,

Free P4 resources,Find P0 (next):

Next <= Available

Example

Allocation [0] [1] [2] . Need

[0] [1] [2]

P0 - - - - - -

P1 - - - - - -

P2 3 0 2 6 0 0

P3 - - - - - -

P4 - - - - - -

Available [0] [1] [2]

7 5 5

For: Request 1 = { 1, 0, 2 }

After modifying the Need and Available, then test for safety.

We find a sequence: { P1, P3, P4, P0, P2 }

Free P0 resources,Find P2 (next):

Next <= Available

Example

Allocation [0] [1] [2] . Need

[0] [1] [2]

P0 - - - - - -

P1 - - - - - -

P2 3 0 2 6 0 0

P3 - - - - - -

P4 - - - - - -

Available [0] [1] [2]

7 5 5

For: Request 1 = { 1, 0, 2 }

After modifying the Need and Available, then test for safety.

We find a sequence: { P1, P3, P4, P0, P2 } SAFE STATE !

Since the state is safe, then the request R 1 can be granted immediately.

Example

Allocation [0] [1] [2] . Need [0] [1] [2]

P0 0 1 0 7 4 3

P1 3 0 2 0 2 0

P2 3 0 2 6 0 0

P3 2 1 1 0 1 1

P4 0 0 2 4 3 1

Available [0] [1] [2]

2 3 0

Having already assumed: Request 1 = { 1, 0, 2 } for P1

Now consider, in addition: Request 4 = { 3, 3, 0 } for P4.

This request cannot be granted because there are not sufficient resources available.

Example

Allocation [0] [1] [2] . Need [0] [1] [2]

P0 0 1 0 7 4 3

P1 3 0 2 0 2 0

P2 3 0 2 6 0 0

P3 2 1 1 0 1 1

P4 0 0 2 4 3 1

Available [0] [1] [2]

2 3 0

Having already assumed: Request 1 = { 1, 0, 2 } for P1

Next consider: Request 0 = { 0, 2, 0 } for P0.

This request cannot be granted. Although sufficient resources are available, P0 retains a Need and this leads to an unsafe state.

It cannot be guaranteed to complete, hence it cannot free sufficient resources.

Deadlock Detection Previous Banker’s algorithms provide

answers to the questions: Is a particular system (of allocated

resources) safe? Can a request for resources be granted

safely?

However, a safe state is not necessarily deadlock free !

Deadlock Detection Now ask the question

For a given resource allocation state, does a deadlock exist?

These slides elaborate on this point and an example

This is important to support Deadlock Prevention (or Avoidance) Deadlock Recovery These slides do not discuss these points. Read

the discussion in the textbook (Silberschatz, Chapter 7)

Deadlock Detection

Data structures Scalars

NP :: Number of Processes NR :: Number of Resources

Vectors Available, Work :: NR resources Finish :: NP processes

NP x NR Matrices Allocation :: Each of NP processes have been

allocated NR resources Request :: Each of NP processes requesting NR

resources

Deadlock Detection

Algorithm Step 1: Initialize

Work = Available ; For all k < NP

if Allocation k ≠ 0 Then Finish[ k ] = false Else Finish[ k ] = true ;

Note that Finish[k]=true; means that it cannot produce a deadlock since it does not control any resources.

Deadlock Detection

Algorithm Middle logic

Step 2: Find a process index k such that

Finish[ k ] == false AND Request k ≤ Work

If no k exists, goto Step 4 (Final step) Step 3:

Work = Work + Allocation ; Finish[ k ] = true ; Goto Step 2

This investigates every possible allocation sequence

Deadlock Detection

Algorithm Final logic

Step 4: If Finish[ k ] == false for any k in 0 ≤ k ≤ (NR-1)

System is in a deadlock state Else, System is deadlock free.

NOTE: Finish[ k ] == false implies that Pk is deadlocked

Example: Deadlock Detection

Allocation [0] [1] [2] Request [0] [1] [2]

P0 0 1 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

Work [0] [1] [2]

(Available) 0 0 0

We consider the following state with declared Allocations and Available resources. A Request is made by every process at the same time.

Can a sequence be found for allocating resources that does not lead to deadlock?

Example

Allocation [0] [1] [2] Request [0] [1] [2]

P0 0 1 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

Work [0] [1] [2]

(Available) 0 0 0

There are two processes, P0 and P2, that are not requesting resources. Start at P0 because it is first (and later, if necessary, we can try P2 first).

Example

Allocation [0] [1] [2] Request [0] [1] [2]

P0 0 1 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

Work [0] [1] [2]

(Available) 0 1 0

Assume that P0 runs to completion, then frees its resources so they become available again.

Example

Allocation [0] [1] [2] Request [0] [1] [2]

P0 - - - - - -

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

Work [0] [1] [2]

(Available) 0 1 0

Next, P2 is assumed to complete.

Example

Allocation [0] [1] [2] Request [0] [1] [2]

P0 - - - - - -

P1 2 0 0 2 0 2

P2 - - - - - -

P3 2 1 1 1 0 0

P4 0 0 2 0 0 2

Work [0] [1] [2]

(Available) 3 1 3

And, P2 frees its resources.

Example

Allocation [0] [1] [2] Request [0] [1] [2]

P0 - - - - - -

P1 2 0 0 2 0 2

P2 - - - - - -

P3 2 1 1 1 0 0

P4 0 0 2 0 0 2

Work [0] [1] [2]

(Available) 3 1 3

The next state (in order) that can complete is P3.

NOTE: Also could allocate and complete P1 and P4.

Example

Allocation [0] [1] [2] Request [0] [1] [2]

P0 - - - - - -

P1 2 0 0 2 0 2

P2 - - - - - -

P3 3 1 1 - - -

P4 0 0 2 0 0 2

Work [0] [1] [2]

(Available) 2 1 3

Allocate resources to P3.

Example

Allocation [0] [1] [2] Request [0] [1] [2]

P0 - - - - - -

P1 2 0 0 2 0 2

P2 - - - - - -

P3 - - - - - -

P4 0 0 2 0 0 2

Work [0] [1] [2]

(Available) 5 2 4

Then, reclaim resources from P3.

Example

Allocation [0] [1] [2] Request [0] [1] [2]

P0 - - - - - -

P1 2 0 0 2 0 2

P2 - - - - - -

P3 - - - - - -

P4 0 0 2 0 0 2

Work [0] [1] [2]

(Available) 5 2 4

Noting that we can allocate resources and complete both P1 and P4, we choose P4 because it is next in order.

Example

Allocation [0] [1] [2] Request [0] [1] [2]

P0 - - - - - -

P1 2 0 0 2 0 2

P2 - - - - - -

P3 - - - - - -

P4 0 0 4 - - -

Work [0] [1] [2]

(Available) 5 2 2

Allocate resources to P4.

Example

Allocation [0] [1] [2] Request [0] [1] [2]

P0 - - - - - -

P1 2 0 0 2 0 2

P2 - - - - - -

P3 - - - - - -

P4 - - - - - -

Work [0] [1] [2]

(Available) 5 2 6

Then reclaim resources from P4.

Example

Allocation [0] [1] [2] Request [0] [1] [2]

P0 - - - - - -

P1 4 0 2 - - -

P2 - - - - - -

P3 - - - - - -

P4 - - - - - -

Work [0] [1] [2]

(Available) 3 2 4

Finally, allocate resources to P1.

Example

Allocation [0] [1] [2] Request [0] [1] [2]

P0 - - - - - -

P1 - - - - - -

P2 - - - - - -

P3 - - - - - -

P4 - - - - - -

Work [0] [1] [2]

(Available) 7 2 6

And reclaim the resources from P1.

Example

Allocation [0] [1] [2] Request [0] [1] [2]

P0 - - - - - -

P1 - - - - - -

P2 - - - - - -

P3 - - - - - -

P4 - - - - - -

Work [0] [1] [2]

(Available) 7 2 6

And reclaim the resources from P1.

Thus the sequence { P0, P2, P3, P4, P1 } is deadlock free, since Finish[k] = true for all k.

ExampleAnd reclaim the resources from P1.

Thus the sequence { P0, P2, P3, P4, P1 } is deadlock free, since Finish[k] = true for all k.

Other sequences that are deadlock free are:

{ P0, P2, P3, P1, P4 }{ P2, P0, P3, P1, P4 }{ P2, P0, P3, P4, P1 }

Allocation [0] [1] [2] Request [0] [1] [2]

P0 0 1 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

Work [0] [1] [2]

(Available) 0 0 0

Example: Modified requestNow, modify the resource request for P2 – it requests one more resource of type [2].

Allocation [0] [1] [2] Request [0] [1] [2]

P0 0 1 0 0 0 0

P1 2 0 0 2 0 2

P2 3 0 3 0 0 1

P3 2 1 1 1 0 0

P4 0 0 2 0 0 2

Work [0] [1] [2]

(Available) 0 0 0

This request will lead to deadlock.

Follow the trace as the scenario develops . . .

Example: Modified requestOnly P0 has Request 0 ≤ Work

Allocation [0] [1] [2] Request [0] [1] [2]

P0 0 1 0 0 0 0

P1 2 0 0 2 0 2

P2 3 0 3 0 0 1

P3 2 1 1 1 0 0

P4 0 0 2 0 0 2

Work [0] [1] [2]

(Available) 0 0 0

Example: Modified requestAllocate resources to P0, then reclaim.

Allocation [0] [1] [2] Request [0] [1] [2]

P0 - - - - - -

P1 2 0 0 2 0 2

P2 3 0 3 0 0 1

P3 2 1 1 1 0 0

P4 0 0 2 0 0 2

Work [0] [1] [2]

(Available) 0 1 0

Example: Modified requestNow we see that there are not sufficient resources to allocate to any other process.

Allocation [0] [1] [2] Request [0] [1] [2]

P0 - - - - - -

P1 2 0 0 2 0 2

P2 3 0 3 0 0 1

P3 2 1 1 1 0 0

P4 0 0 2 0 0 2

Work [0] [1] [2]

(Available) 0 1 0

Hence, this state is deadlocked and the resource request cannot be made.

Deadlock Recovery

Read Section 7.7 in the textbook.

Most systems take the simplest approach, aborting all deadlocked processes

More sophisticated approaches can be developed They take much more time to execute

hence are applied less often

They are strongly dependent on choice of policies.

Summary

We covered essential aspects of deadlock detection and avoidance in respect of resource allocation to handle process requests.

We paid special attention to Issue of safe states Resource allocation

Read Chapter 7 of Silberschatz et al.

top related