Docsity

Deadlock: Characterization, Prevention, and Avoidance - Prof. Michael W. Hicks, Study notes of Operating Systems

Partial preview of the text

Download Deadlock: Characterization, Prevention, and Avoidance - Prof. Michael W. Hicks and more Study notes Operating Systems in PDF only on Docsity!

CMSC 412

Deadlock

Announcements

  • Reading
    • Chapter 8

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 tape drives.
    • P 1 and P 2 each hold one tape drive and each needs another one.
  • Example - semaphores A and B , set to 1 P 0 P 1 wait (A); wait(B) wait (B); wait(A)

System Model

  • Resource types R 1 , R 2 ,.. ., R m CPU cycles, memory space, I/O devices
  • Each resource type R i has W i instances.
  • Each process utilizes a resource as follows: - request - use - release

Resource Allocation Graph

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 has two types
    • request edge – directed edge P1 → Rj
    • assignment edge – directed edge Rj → Pi

Example Resource Allocation Graph

Graph With A Deadlock

Graph With A Cycle, No Deadlock

Deadlock Prevention

Restrain the ways a request can be made

  • Mutual Exclusion – Sharable resources do not require mutually exclusive access and cannot be involved in a deadlock.
  • Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. - Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none. - Low resource utilization; starvation possible.

Deadlock Prevention

  • No Preemption – Virtualize resources and permit them to be preempted. For example, the CPU can be preempted.
  • Circular Wait – Impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.

Deadlock Avoidance

  • Deadlock prevention restricts some large class of behaviors a priori - Some behaviors within this class might be legal in some circumstances
  • Deadlock avoidance permits more behaviors, relying on dynamic checks - Actions that could possibly lead to deadlock are avoided

Deadlock Avoidance Approach

  • Each process declares the maximum number of resources of each type that it may need.
  • OS dynamically ensures that a request can never cause the resource-allocation state to eventually be in a circular-wait condition.
  • Resource-allocation state is defined by
    • The number of available resources
    • The number of allocated resources, and
    • The maximum demands of the processes.

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

Safe, Unsafe, Deadlock State

Resource-Allocation Graph Algorithm

  • Claim edge Pi → Rj indicated that process Pj may request resource Rj ; represented by a dashed line. One instance per resource type.
  • 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.

Resource-Allocation Graph

For Deadlock Avoidance

Banker’s Algorithm

  • Variables: n is the number of processes m is the number of resource types
    • Available - vector of length m indicating the number of available resources of each type
    • Max - n by m matrix defining the maximum demand of each process
    • Allocation - n by m matrix defining number of resources of each type currently allocated to each process
    • Need: n by m matrix indicating remaining resource needs of each process
  • 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,2, …, n. 2.Find and i such that both: (a) Finish [ i ] = false (b) Needi ≤ Work If no such i exists, go to step 4.

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

Resource-Request Algorithm for Pi Requesti = request vector for process Pi. If Requesti [ j ] = k then process Pi wants k instances of resource type Rj. Algorithm:

  1. If Requesti ≤ Needi go to step 2. Otherwise error: process has exceeded its maximum claim.
  2. If Requesti ≤ Available go to step 3. Otherwise Pi waits, since resources are not available. Resource-Request Algorithm for Pi
  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 ⇒ P i must wait, and the old resource-allocation state is restored

Example P

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 P 0 0 1 0 7 4 3 2 3 0 P 1 3 0 2 0 2 0 P 2 3 0 1 6 0 0 P 3 2 1 1 0 1 1 P 4 0 0 2 4 3 1
  • Executing safety algorithm shows that sequence < P 1 , P 3 , P 4 , P 0 , P 2 > satisfies safety requirement.

Example P

Requests

Allocation Need Available A B C A B C A B C P 0 0 1 0 7 4 3 2 3 0 P 1 3 0 2 0 2 0 P 2 3 0 1 6 0 0 P 3 2 1 1 0 1 1 P 4 0 0 2 4 3 1

  • Can request for (3,3,0) by P 4 be granted?
  • Can request for (0,2,0) by P 0 be granted?

Deadlock Detection

  • Allow system to enter deadlock state
  • Detection algorithm
  • Recovery scheme

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.

Detection Algorithm

1.Let Work and Finish be vectors of length m and n , respectively Initialize: (a) Work = Available (b) For i = 1,2, …, n , 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.

Detection Algorithm (Cont.)

  1. Work = Work + Allocationi Finish [ i ] = true go to step 2. 4.If Finish [ i ] == false, for some i , 1 ≤ i ≤ n , then the system is in deadlock state. Moreover, if Finish [ i ] == false , then Pi is deadlocked. Algorithm requires an order of O(m x n2)^ operations to detect whether the system is in deadlocked state.

Example of Detection Algorithm

  • Five processes P 0 through P 4
  • Three resource types A (7 instances), B ( instances), and C (6 instances). Allocation Request Available A B C A B C A B C P 0 0 1 0 0 0 0 0 0 0 P 1 2 0 0 2 0 2 P 2 3 0 3 0 0 0 P 3 2 1 1 1 0 0 P 4 0 0 2 0 0 2
  • Sequence < P 0 , P 2 , P 3 , P 1 , P 4 > will result in Finish [ i ] = true for all i.

Example (Cont.)

  • P 2 requests an additional instance of type C. Request A B C P 0 0 0 0 P 1 2 0 1 P 2 0 0 1 P 3 1 0 0 P 4 0 0 2
  • State of system?
    • Can reclaim resources held by process P 0 , but cannot fulfill other processes’ requests.
    • Deadlock with processes P 1 , P 2 , P 3 , and P 4.