IOS103
This presentation is the property of its rightful owner.
Sponsored Links
1 / 59

IOS103 OPERATING SYSTEM DEADLOCKS PowerPoint PPT Presentation


  • 40 Views
  • Uploaded on
  • Presentation posted in: General

IOS103 OPERATING SYSTEM DEADLOCKS. Objectives. At the end of the course, the student should be able to: Define deadlock; Discuss how the deadlock occurs; Discuss how to prevent deadlock; Discuss how to avoid deadlock; Discuss how to detect deadlock; Discuss how to recover from deadlock.

Download Presentation

IOS103 OPERATING SYSTEM DEADLOCKS

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Ios103 operating system deadlocks

IOS103

OPERATING SYSTEM

DEADLOCKS


Ios103 operating system deadlocks

Objectives

  • At the end of the course, the student should be able to:

  • Define deadlock;

  • Discuss how the deadlock occurs;

  • Discuss how to prevent deadlock;

  • Discuss how to avoid deadlock;

  • Discuss how to detect deadlock;

  • Discuss how to recover from deadlock.


Ios103 operating system deadlocks

Deadlocks

Introduction

  • In a multiprogramming environment, several processes may compete for a finite number of resources. A process requests resources such as memory space, CPU cycles, files and I/O devices, if the resources are not available at that time, the process enters a waiting state.

  • Sometimes, a waiting process is never again able to change state because the resources it has requested are held by other waiting processes, this is called deadlock.


Ios103 operating system deadlocks

Deadlocks

Introduction

  • A system consists of a finite number of resources to be distributed among a number of competing processes. The resources are partitioned into several types, each consisting of some number of identical instances.

  • If a process requests an instance of a resource type, the allocation of any instance of the type will satisfy the request. If it will not, then the instances are not identical and the resource type classes have not been defined properly.


Ios103 operating system deadlocks

Deadlocks

Introduction

  • A process must request a resource before using it and must release the resource after using it. A process may request as many resources as it requires to carry out its designated task. Obviously, the number of resources requested may not exceed the total number of resources available in the system. In other words, a process cannot request three printers if the system has only two.


Ios103 operating system deadlocks

Deadlocks

  • Under the normal mode of operation, a process may utilize a resource in only the following sequence:

  • Request: The process requests the resource. If the request cannot be granted immediately, then the requesting process must wait until it can acquire the resource.

  • Use: The process can operate on the resource (for example, if the resource is a printer, the process can print on the printer).

  • Release: The process releases the resource.


Ios103 operating system deadlocks

Deadlocks

  • To illustrate deadlock state:

  • Consider a system with three CD RW drives. Suppose each of three processes holds one of these CD RW drives. If each process now requests another drive, the processes will be in deadlock state. (The same resource type)

  • Consider a system with one printer and one DVD drive. Suppose that process PI is holding the DVD and process PJ is holding the printer. If PI requests the printer and PJ requests the DVD drive, a deadlock occurs. (Different resource type)


Ios103 operating system deadlocks

Deadlocks

  • Deadlock can arise if four conditions hold simultaneously:

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


Ios103 operating system deadlocks

Deadlocks

  • Circular wait: there exists a set {P0, P1, …, Pn} 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


Ios103 operating system deadlocks

Resource- Allocation Graph

  • Deadlocks can be described more precisely in terms of a directed graph called a system resource-allocation graph. This graph consists of a set of vertices V and a set of edges E. The set of vertices V is partitioned into two different types of nodes:

    • 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


Ios103 operating system deadlocks

Resource- Allocation Graph

  • A directed edge from process Pi to resource type Rj is denoted by Pi→Rj; it signifies that process Pi has requested an instance of resource type Rj and is currently waiting for that resource. A directed edge from resource type Rj to process Pi is denoted by Rj→Pi; it signifies that an instance of resource type Rj has beed allocated to process Pi.

  • Request edge – directed edge Pi→Rj

  • Assignment edge – directed edge Rj→Pi


Ios103 operating system deadlocks

Resource- Allocation Graph

  • Pictorially, we represent:

  • Process: a circle

  • Resource Type with 4 instances: rectangle with dots

  • Pirequests instance of Rj

  • Pi is holding an instance of Rj

Pi

Rj

Pi

Rj


Ios103 operating system deadlocks

Resource- Allocation Graph

  • Example:

  • The sets P, R and E:

  • P = { P1, P2, P3 }

  • R = { R1, R2, R3, R4}

  • E = {P1 → R1 , P2 → R3 , R1 → P2 ,

  • R2 → P2 , R2 → P1 , R3 → P3 }

  • Resource instances:

  • One instance of resource type R1

  • Two instances of resource type R2

  • One instance of resource type R3

  • Three instances of resource type R4


Ios103 operating system deadlocks

Resource- Allocation Graph

  • Process states:

  • Process P1 is holding an instance of resource type R2 and is waiting for an instance of resource type R1.

  • Process P2 is holding an instance of R1 and an instance of R2 and is waiting for an instance of R3.

  • Process P3 is holding an instance of R3.


Ios103 operating system deadlocks

Resource- Allocation Graph

  • If the graph contains no cycles, then no process in the system is deadlocked. If the graph does contain a cycle, then a deadlock may exist.

  • If each resource type has several instances, then a cycle does not necessarily imply that a deadlock has occurred. In this case, a cycle in the graph is a necessary but not a sufficient condition for the existence of deadlock.


Ios103 operating system deadlocks

Resource- Allocation Graph

  • Let’s use the previous graph:

  • Suppose that process P3 requests an instance of resource type R2. Since no resource instance is currently available, a request edge P3→R2 is added to the graph. At this point, two minimal cycles exist in the system.

  • P1→R1→P2 →R3 →P3 →R2 →P1

  • P2→R3→P3 →R2 →P2


Ios103 operating system deadlocks

Resource- Allocation Graph

  • Therefore:

  • Processes P1, P2 and P3 are deadlocked. Process P2 is waiting for the resource R3, which is held by process P3. Process P3 is waiting for either process P1 or process P2 to release resource R2. In addition, process P1 is waiting for process P2 to release resource R1.


Ios103 operating system deadlocks

Resource- Allocation Graph

  • Consider:

  • P1→R1→P3 →R2 →P1

  • However, there is no deadlock. Observe that process P4 may release its instance of resource type R2. That resource can then be allocated to P3, breaking the cycle.


Ios103 operating system deadlocks

Resource- Allocation Graph

  • In summary, if a resource-allocation graph does not have a cycle, then the system is not in a deadlocked state. If there is a cycle, then the system may or may not be in deadlocked state. This observation is important when we deal with the deadlock problem.


Ios103 operating system deadlocks

Resource- Allocation Graph

  • Generally:

    • 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


Ios103 operating system deadlocks

Methods for Handling Deadlocks

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

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

  • We can ignore the problem altogether and pretend that deadlocks never occur in the system.


Ios103 operating system deadlocks

Deadlock Prevention

Mutual Exclusion

  • This condition must hold for non-sharable resources. (Example: a printer cannot be simultaneously shared by several processes.)

  • Sharable resources, in contrast, do not require mutually exclusive access and thus cannot be involved in a deadlock. (Example: read-only files.)

  • In general, however, we cannot prevent deadlocks by denying the mutual-exclusion condition, because some resources are intrinsically non-sharable.


Ios103 operating system deadlocks

Deadlock Prevention

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.

  • Example1: A process that copies data from DVD drive to a file on disk, sorts the file and then prints the results to a printer. (Request first the DVD drive, disk file and printer.)


Ios103 operating system deadlocks

Deadlock Prevention

Hold and Wait

  • Example 2: Request the DVD drive and disk file then copy the file from DVD drive. Then request the disk file and printer then prints the file from disk file.

  • Disadvantages:

    • Resource utilization may be low. Since the resources were requested and waited to be used.

    • Starvation. Other processes will wait indefinitely for resources being used.


Ios103 operating system deadlocks

Deadlock Prevention

No Preemption

  • There be no preemption of resources that have already been allocated.

  • If a process is holding some resources and requests another resource that cannot be immediately allocated to it (the process must wait), then all resources the process is currently holding are preempted.

  • The preempted resources are added to the list of resources for which the process is waiting.

  • The process will be restarted only when it can regain its old resources as well as the new ones that is requesting.


Ios103 operating system deadlocks

Deadlock Prevention

No Preemption

  • This is often applied to resources whose state can be easily saved and restored later such as CPU registers and memory space.

  • It cannot generally be applied to such resources as printers and tape drives.


Ios103 operating system deadlocks

Deadlock Prevention

Circular Wait

  • This is to impose a total ordering of all resource types and to require that each process requests resources in an increasing order of enumeration.

  • Example:F(tape drive) = 1

  • F(disk drive) = 5

  • F(printer) = 12

  • In the example, requests first the tape drive before the printer. In developing the ordering or hierarchy, it is up to application developers based on the normal order of usage of the resources.


Ios103 operating system deadlocks

Deadlock Avoidance

  • An alternative method for avoiding deadlocks is to require additional information about how resources are to be requested.

  • The simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. Given this a priori information, it is possible to construct an algorithm that ensures that the system will never enter a deadlocked state. Such an algorithm defines the deadlock-avoidance approach.


Ios103 operating system deadlocks

Deadlock Avoidance

  • A deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that a circular-wait condition can never exist.

  • The resource-allocation state is defined by the number of available and allocated resources and the maximum demands of the processes.


Ios103 operating system deadlocks

Deadlock Avoidance

Safe State

  • A state is safe if the system can allocate resources to each process (up to its maximum) in some order and still avoid a deadlock.

  • More formally, a system is in safe state only if there exists a safe sequence.

  • When a process requests an available resources, system must decide if immediate allocation leaves the system in a safe state.


Ios103 operating system deadlocks

Deadlock Avoidance

Safe State

  • System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the processes is the systems 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


Ios103 operating system deadlocks

Deadlock Avoidance

Safe State

  • That is:

    • If Pi resource needs are not immediately available, then Pi can wait until all Pjhave 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


Ios103 operating system deadlocks

Deadlock Avoidance

Safe State

  • If a system is in safe state = no deadlocks

  • If a system is in unsafe state = possibility of deadlocks

  • Avoidance = ensure that a system will never enter an unsafe state


Ios103 operating system deadlocks

Deadlock Avoidance

Safe State

  • Example: A system with 12 magnetic tape drives and 3 processes.

  • Max. NeedsCurrent Needs

  • P0105

  • P142

  • P292

  • A t0, P0 holding 5, P1 holding 2 and P2 holding 2. So 3 free tape drives. Therefore, Safe State.

  • A t1, P2 requests 1 tape drive (total of 3), P0 requests 5 tape drives (but only 2 available) and P1 will wait. Therefore, Unsafe State.


Ios103 operating system deadlocks

Deadlock Avoidance

Resource-Allocation-Graph Algorithm

  • Claim edge Pi Rj indicated 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

  • This is applicable for single instance of resource type.


Ios103 operating system deadlocks

Deadlock Avoidance

Resource-Allocation-Graph Algorithm

  • If no cycle exists, then the allocation of the resource will leave the system in a safe state.


Ios103 operating system deadlocks

Deadlock Avoidance

Resource-Allocation-Graph Algorithm

  • If a cycle is found, then the allocation will put the system in an unsafe state. In that case, process Pi will have to wait for its requests to be satisfied.


Ios103 operating system deadlocks

Deadlock Avoidance

Banker’s Algorithm

  • This algorithm applicable for multiple instances of a resource type.

  • The name was chosen because the algorithm could be used in a banking system to ensure that the bank never allocated its available cash in such a way that it could no longer satisfy the needs of all its customers.

  • Each process must declare the maximum number of instances of each resource type that may need.


Ios103 operating system deadlocks

Deadlock Avoidance

Banker’s Algorithm

  • Data Structures for the Banker’s Algorithm:

  • Let:n =no. of processes & m=number of resources types

  • Available: Vector of length m. If available [j] = k, there are k instances of resource type Rjavailable

  • Max: n x m matrix. If Max [i,j] = k, then process Pimay 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 Rjto complete its task

    Need [i,j] = Max[i,j] – Allocation [i,j]


Ios103 operating system deadlocks

Deadlock Avoidance

Banker’s Algorithm

Data Structures for the Banker’s Algorithm:

Example:

Let X and Y be vectors of length n.

And X ≤ Y if and only if X[i] ≤ Y[i] for all i=1, 2,…n.

If X=(1, 7, 3, 2) and Y=(0, 3, 2, 1)

then Y ≤ X. In addition, Y < X if Y ≤ X and Y ≠ X


Ios103 operating system deadlocks

Deadlock Avoidance

Banker’s Algorithm

  • Safety Algorithm

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

    • Work = Available & Finish [i] = false for i = 0, 1, …, n- 1

  • Find and i such that both:

    (a) Finish [i] = false

    (b) Needi Work

    If no such i exists, go to step 4

  • Work = Work + AllocationiFinish[i] = truego to step 2

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

  • This require an order of m x n2 operations to determine whether a state is safe.


  • Ios103 operating system deadlocks

    Deadlock Avoidance

    Banker’s Algorithm

    • Resource-Request Algorithm

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

      1.If Requesti Needigo 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


    Ios103 operating system deadlocks

    Deadlock Avoidance

    Banker’s Algorithm

    • Examples:

    • 5 processes P0 through P4;

      3 resource types:

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

      Snapshot at time T0:

      AllocationMaxAvailable

      A B C A B C A B C

      P00 1 0 7 5 3 3 3 2

      P12 0 0 3 2 2

      P23 0 2 9 0 2

      P32 1 1 2 2 2

      P40 0 2 4 3 3


    Ios103 operating system deadlocks

    Deadlock Avoidance

    Banker’s Algorithm

    • Example: Then

    • The content of the matrix Need is defined to be Max – Allocation

      Need

      A B C

      P07 4 3

      P11 2 2

      P26 0 0

      P30 1 1

      P44 3 1

    • The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria


    Ios103 operating system deadlocks

    Deadlock Avoidance

    Banker’s Algorithm

    • Example: What is P1 requests (1,0,2)

    • Check that Request  Available (that is, (1,0,2)  (3,3,2) =true

      AllocationNeedAvailable

      A B CA B CA B C

      P00 1 0 7 4 3 2 3 0

      P13 0 2 0 2 0

      P23 0 1 6 0 0

      P32 1 1 0 1 1

      P40 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? No

    • Can request for (0,2,0) by P0 be granted? No


    Ios103 operating system deadlocks

    Deadlock Detection

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

    • Therefore the system may provide an algorithm that examines the state of the system to determine whether a deadlock has occurred.

    • And an algorithm to recover from the deadlock.


    Ios103 operating system deadlocks

    Deadlock Detection

    Single Instance of Each Resource Type

    • Maintain wait-for graph

      • Nodes are processes

      • Pi Pjif Piis waiting forPj

    • Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock

    • An algorithm to detect a cycle in a graph requires an order of n2 operations, where n is the number of vertices in the graph


    Ios103 operating system deadlocks

    Deadlock Detection

    Single Instance of Each Resource Type

    Resource-Allocation Graph

    Corresponding wait-for graph


    Ios103 operating system deadlocks

    Deadlock Detection

    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. Therefore: similar to banker’s algorithm.

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


    Ios103 operating system deadlocks

    Deadlock Detection

    Several Instances of a Resource Type

    • Example1:

    • 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

    • Find an index i such that both:

      (a)Finish[i] == false

      • Requesti Work

        If no such i exists, go to step 4


    Ios103 operating system deadlocks

    Deadlock Detection

    Several Instances of a Resource Type

    • Example1:

    • Work = Work + Allocationi

    • Finish[i] = true

    • go to step 2

    • 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


    Ios103 operating system deadlocks

    Deadlock Detection

    Several Instances of a Resource Type

    • Example2:

    • Five processes P0 through P4;three resource types

    • A (7 instances), B (2 instances), and C (6 instances)

    • Snapshot at time T0:

      AllocationRequestAvailable

      A B C A B C A B C

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

    • Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i


    Ios103 operating system deadlocks

    Deadlock Detection

    Several Instances of a Resource Type

    • Example2:

    • P2 requests an additional instance of type C

      Request

      A B C

      P00 0 0

      P12 0 1

      P20 0 1

      P31 0 0

      P40 0 2

    • State of system?

      • Can reclaim resources held by process P0, but insufficient resources to fulfill other processes; requests

      • Deadlock exists, consisting of processes P1, P2, P3, and P4


    Ios103 operating system deadlocks

    Deadlock Detection

    Detection-Algorithm Usage

    • When, and how often, to invoke depends on:

      • How often a deadlock is likely to occur?

      • How many processes will need to be rolled back?

        • one for each disjoint cycle

    • If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we would not be able to tell which of the many deadlocked processes “caused” the deadlock


    Ios103 operating system deadlocks

    Recovery from Deadlock

    • When a detection algorithm determines that a deadlock exists:

      • Inform the operator that a deadlock has occurred and let the operator deal with the deadlock manually.

      • Let the system recover from the deadlock automatically.


    Ios103 operating system deadlocks

    Recovery from Deadlock

    Options for breaking a deadlock:

    • Simply to abort one or more processes to break the circular-wait.

    • To preempt some resources from one or more of the deadlocked processes.


    Ios103 operating system deadlocks

    Recovery from Deadlock

    Process Termination

    • To eliminate deadlocks by aborting a process, we use one of two methods. In both methods, the system reclaims all resources allocated to the terminated processes.

    • Abort all deadlocked processes. This method clearly will break the deadlock cycle, but at great expense.

    • Abort one process at a time until the deadlock cycle is eliminated. This method incurs considerable overhead, since after each process is aborted.


    Ios103 operating system deadlocks

    Recovery from Deadlock

    Process Termination

    • In which order of the processes 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

      • Is process interactive or batch?


    Ios103 operating system deadlocks

    Recovery from Deadlock

    Resource Preemption

    • To eliminate deadlocks, we successfully preempt some resources from processes and give these resources to other processes until the deadlock cycle is broken.

    • Selecting a victim. We must determine the order of preemption to minimize cost.

    • Rollback. We must rollback the process to some safe state and restart it from that state.

    • Starvation. How can we guarantee that resources will not always be preempted from the same process? (same process may always be picked as victim or include number of rollback in cost factor)


  • Login