Docsity

Deadlock Avoidance, Safe State, Basic Facts - Lecture Slides | CSE 30341, Study notes of Operating Systems

Toggle sidebar
bg1
page 12/20/06 CSE 30341: Operating Systems Principles
Deadlock Avoidance
Requires that the system has some additional
a
priori
information available.
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Deadlock Avoidance, Safe State, Basic Facts - Lecture Slides | CSE 30341 and more Study notes Operating Systems in PDF only on Docsity!

Deadlock Avoidance

Requires that the system has some additional a

priori information available.

 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

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 safe sequence of all processes  Sequence <P 1

, P

2

, …, P

n

is safe if for each P i , the resources that P i can still request can be satisfied by currently available resources + resources held by all the P j , with j<I  If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished  When Pj is finished, P i can obtain needed resources, execute, return allocated resources, and terminate  When Pi terminates, Pi +1 can obtain its needed resources, and so on

Safe, Unsafe , Deadlock State

Resource-Allocation Graph Algorithm

Claim edge P i

→ R

j indicated that process P j may request resource R j ; 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.

Unsafe State In Resource-Allocation

Graph

Banker’s Algorithm

 Multiple instances.  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

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-
  2. Find an i such that both: (a) Finish [ i ] = false (b) Needi ≤ Work If no such i exists, go to step 4.
  3. Work = Work + Allocation i Finish [ i ] = true go to step 2.
  4. If Finish [ i ] == true for all i , then the system is in a safe state.

Resource-Request Algorithm for Process P i Request = request vector for process P i

. If Request i [ j ] = k then process P i wants k instances of resource type R j.

  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 restored

Example (Cont.)

 The content of the matrix. Need is defined to be Max – Allocation. Need A B C P 0

P

1

P

2

P

3

P

4

 The system is in a safe state since the sequence < P 1

, P

3

, P

4

, P

2

, P

0

satisfies safety criteria.

Example P

1

Request (1,0,2) (Cont.)

 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 P_ 0

P

1

P

2

P

3

P

4

 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?

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.  An algorithm to detect a cycle in a graph requires an order of n 2 operations, where n is the number of vertices in the graph.

Resource-Allocation Graph Corresponding wait-for graph

Resource-Allocation Graph and Wait-for

Graph

Detection Algorithm

  1. Let Work and Finish be vectors of length m and n , respectively Initialize: (a) Work = Available (b) For i = 0,1, …, n-1 , 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. 3. Work = Work + Allocation i Finish [ i ] = true go to step 2
  3. If Finish [ i ] == false, for some i , 1 ≤ i ≤ n , then the system is in deadlock state. Moreover, if Finish [ i ] == false , then P i is deadlocked.

Example of Detection Algorithm

 Five processes P 0 through P 4 ; three resource types A (7 instances), B (2 instances), and C ( instances).  Snapshot at time T 0

AllocationRequest Available A B C A B C A B C P 0

P

1

P

2

P

3

P

4

 Sequence < P 0

, P

2

, P

3

, P

1

, P

4

will result in Finish [ i ] = true for all i