SlideShare a Scribd company logo
Deadlock Handling
Objectives
 To develop a description of deadlocks,
  which prevent sets of concurrent
  processes from completing their tasks
 To present a number of different
  methods for preventing or avoiding
  deadlocks in a computer system.
Deadlock
   Defined as the permanent blocking of a
    set of processes that compete for a
    system resource, including database
    records and communication lines

   A situation wherein each of a collection of
    processes is waiting for something from
    other processes in the collection. Since all
    are waiting, none can provide any of the
    things being waited for.
Deadlock
   A situation wherein a process is
    requesting a resource which is withheld
    by another process and never get hold
    of the resource.
Example
   System has 2 tape drives
     P1 and P2 each hold one tape drive and
     each needs another one.


   Semaphores A and B initialized to 1
     P0                P1
     Wait(A);          Wait(B);
     Wait(B);          Wait(A);
Deadlock
   Deadlock occurs when a set of
    processes are in a wait state, because
    each process is waiting for a resource
    that is held by some other waiting
    process. Therefore, all deadlocks
    involve conflicting resource that are
    needed by two or more processes.
Bridge Crossing Example




    Traffic only in one direction.
    Each section of a bridge can be viewed as a resource.
    If a deadlock occurs, it can be resolved if one car backs up
     (preempt resources and rollback).
    Several cars may have to be backed up if a deadlock occurs.
    Starvation is possible.
Deadlock
   Unlike some other problems in
    multiprogramming systems, there is no
    efficient solution to the deadlock
    problem in the general case. It is one
    area where there is a strong theory but it
    is completely ignored in practice
    because solutions to deadlock are
    expensive.
Resource
   A hardware device (e.g. tape drive,
    memory)

   A piece of information (e.g. a locked
    record in a database)
General Categories of Resources
   Reusable – something that can be
    safely used by one process at a time
    and is not depleted by that use.
    Processes obtain resources that they
    later release for reuse by others.
     Example:
     CPU, Memory, specific I/O devices or files.
General Categories of Resources
   Consumable – these can be created and
    destroyed. When a resource is acquired
    by a process, the resource ceases to
    exist.
     Examples:
     Interrupts, signals, messages.
Types of Resources
   Preemptable resource – a resource that
    can be taken away from the process
    owning it with no ill effect (needs
    save/restore)

   Example: Memory or CPU
Types of Resources
   Non-preemptable – a resource that
    cannot be taken away from its current
    owner without causing the computation
    to fail.

   Example: Printer or floppy disk.
Note
   Deadlocks occur when sharing reusable
    and non-preemptable resources
Pop Exercise

   Classify the following as either sharable
    or non-sharable
     A. Hard disk
     B. file
     C. keyboard
System Model
 System consists of a finite number of
  resources and a finite number of
  processes competing for resources.
 Resources are partitioned into several
  types, each of which consists of some
  number of identical instances
System Model
   When a process requests an instance of
    a resource type, the allocation of any
    instance of the type will satisfy the
    request (otherwise, resource types have
    not been defined properly).
Protocol for resource utilization
 Request – the process will request for a
  certain 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 the
  resource
 Release – the process relinquishes the
  resources.
Note
   A set of processes is deadlocked when
    every process in the set is waiting for an
    event that can be caused only by
    another process in the set.
Deadlock Characterization
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.
 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 P0 is waiting for a resource that is held by P0.
Additional characteristic
   PROCESS IRREVERSIBLE – Unable to
    reset to an earlier state where resources
    are not held.
Systems avoiding deadlocks
 Systems involving only shared
  resources cannot deadlock. – negates
  mutual exclusion
 System that aborts processes which is
  requesting a resource but currently
  being used. – negates hold and wait
 Preemption may be possible if a process
  does not use its resources until it has
  acquired all it needs – negates no
  preemption
Systems…
 Transaction processing systems provide
  checkpoints so that processes may back
  out of a transaction. – negates
  irreversible process
 System that detects or avoids deadlocks
  – prevents cycle.
Resource-Allocation Graph (RAG)
   Consists of a set of vertices V and a set of edges
    E. Devised by Holt in 1972.

   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
Resource-Allocation Graph (RAG)


    Process

    Resource Type with 4 instances

    Pi requests instance of Rj        Pi
                                                 Rj



    Pi is holding an instance of Rj        Pi
                                                      Rj
Example
 P = P1, P2, P3
 R = R1, R2, R3, R4
 Instances: R1=1, R2=2, R3=1, R4=3


   E={ P1 -> R1, P2 -> R3, R1 -> P2, R2 ->
    P1, R2 -> P2, R3 -> P3,

   Question: is there a deadlock?
Example
Resource Allocation Graph With A
Deadlock
Graph With A Cycle But No Deadlock
Basic Facts
   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.
Methods for handling deadlocks
   3 strategies:

1. Ostrich Solution: Ignore the problem
   and pretend deadlocks never occur in
   the system. Used by most OS, including
   Unix
Methods for handling deadlocks
2. Ensure that the system will never enter a
   deadlock state.

 Deadlock prevention – Design a system
  in such a way that the 4 conditions
  leading to deadlock will never occur.
 Deadlock avoidance – make a decision
  dynamically checking whether the request
  will, if granted, potentially lead to a
  deadlock or not.
Methods for handling deadlocks
3. Allow the system to enter a deadlock
   state and recover

   Deadlock detection and recovery
Deadlock Prevention
   Preventing the occurrence of one of the
    necessary conditions leading to
    deadlock. Deadlock prevention strategy
    is very conservative. It solves the
    problem of deadlock by limiting access
    to resources and by imposing
    restrictions on processes.
Mutual Exclusion
   In general, this condition cannot be
    required for non-sharable resources but
    is a must for sharable resources.
Hold and Wait
   Must guarantee that whenever a process requests a
    resource, it does not hold any other resources. A
    process is blocked until all requests can be granted
    simultaneously.

   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
No preemption
 If a process that is holding some
  resource requests another resource that
  cannot be immediately allocated to it,
  then all resources currently being held
  are released.
 Preempted resources are added to the
  list of resources for which the process is
  waiting.
 Process will be restarted only when it
  can regain its old resources, as well as
  new ones that it is requesting.
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
 •   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.
Ways to avoid deadlock by careful
resource allocation
1. Resource trajectories
2. Safe/Unsafe states
3. Dijkstra’s Banker’s Algorithm
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
Avoidance algorithms
   Single instance of a resource type. Use
    a resource-allocation graph

   Multiple instances of a resource type.
    Use the banker’s algorithm
Resource-Allocation Graph Scheme
   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.
Resource-Allocation Graph
Unsafe State In Resource-Allocation Graph
Sample Algorithm
   Given the RAG of the system (Initially on
    a safe state, grant a resource allocation
    request if and only if doing so will not
    introduce a cycle in the graph. (taking
    into consideration all assignment,
    request and claim edges). Otherwise,
    the requesting process has to wait.
Sample Algorithm
1. Operation: P2 requests R3
   Action: Request granted
   System State: SAFE

2. Operation: P3 requests R2
   Action: REQUEST DENIED!!!
   System State: Leads to deadlock
Sample 2
   Consider a system with 15 identical instances of a resource

    Process        Max Need          Curr Allo      Remaining
      P0              14                5
      P1              10                5
      P2               3                2
 Currently available resource: 15 – (5+5+2) = 3
 Is it safe?
     Yes with a safe sequence of <P2, P1, P0>
   What if P0 requests two more resources, will it be granted?
     No, OS cannot satisfy remaining needs of all processes. Unsafe.
Sample 3
   Total instances: 8

    Process       Max Need      Curr Allo     Remaining
      P0             5             3
      P1             8             1
      P2             7             2


   Is it safe or unsafe? What is the safe sequence?
Sample 4
   Total instances: 8

    Process     Max Need       Curr Allo    Remaining
      P0           5              3
      P1           8              1
      P2           7              2


   Is it safe or unsafe? What is the safe sequence?
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.
Data Structures for the Banker’s Algorithm
    Let n = number of processes, and m = number of resources
     types.

    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.
          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 = 0, 1, …, n- 1.
2. Find and i such that both:
    (a) Finish [i] = false
    (b) Needi ≤ Work
    If no such i exists, go to step 4.
3. 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.
Example of Banker’s Algorithm
 5 processes P0 through P4;
  3 resource types:
       A (10 instances), B (5instances), and C (7 instances).
 Snapshot at time T0:
               Allocation     Max                   Available
               ABC            ABC                   ABC
      P0       010            753                   332
       P1      200            322
       P2      302            902
       P3      211            222
       P4      002            433
Example (Cont.)
   The content of the matrix Need is defined to be Max – Allocation.
                Need
                ABC
        P0      743
        P1      122
        P2      600
        P3      011
        P4      431


   The system is in a safe state since the sequence < P1, P3, P4, P2,
    P0> satisfies safety criteria.
Example: P1 Request (1,0,2)
 Check that Request ≤ Available (that is, (1,0,2) ≤ (3,3,2) ⇒
  true.
               Allocation        Need           Available
               ABC               ABC            ABC
       P0      010               743            230
       P1      302               020
       P2      302               600
       P3      211               011
       P4      002               431
 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?
Deadlock detection and recovery
 Implies no conscious effort to
  prevent/avoid deadlocks
 At certain times during processing, the
  system executes an algorithm to
  determine whether or not a deadlock
  has occurred.
 If so, an attempt to recover from the
  deadlock is made.
Detection Algorithm
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. 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.
Resource-Allocation Graph and Wait-for
Graph




      Resource-Allocation Graph   Corresponding wait-for graph
Several Instances of a Resource Type

   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.
Detection Algorithm
1. Let Work and Finish be vectors of length m and n, respectively
   Initialize:
    (a) Work = Available
    n 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.

3. 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.
Note:
The 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 P0 through P4; three resource types
  A (7 instances), B (2 instances), and C (6 instances).
 Snapshot at time T0:
                 Allocation     Request          Available
                ABC             ABC              ABC
         P0     010             000                        000
         P1     200             202
         P2     303             000
         P3     211             100
         P4     002             002
 Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for
  all i. Therefore, no deadlock
Example (cont.)
 P2 requests an additional instance of type C.
               Request
               ABC
       P0      000
       P1      202
       P2      001
       P3      100
       P4      002
 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.
Detection Algorithm Usage
   When and how often, to invoke deadlock detection?

   Factors to consider:
     How often a deadlock is likely to occur
     How many process will need to be rolled back
   Approaches
     Execute the algorithm at predetermined time interval
     Execute the algorithm whenever a resource request cannot
      be granted immediatetly
Recovery from deadlock: Process
Termination
   Who’s responsibility?
     Inform the operator and let him deal with the
      deadlock manually
     Allow the system to recover from deadlock
      automatically.
Approaches: I. Process Termination

   Abort all deadlocked processes

   Abort one process at a time until the
    deadlock cycle is eliminated
In which order should we choose to abort?

 Priority of process
 How long process has computed and
  how much longer to completion
 Resources the process has used
 Resources process needs to complete
 How many process will need to be
  terminated?
 Is process interactive or batch?
II. Process preemption
 Selecting a victim – minimize cost
 Rollback – return to some safe state,
  restart process for that state.
 Starvation – same process may always
  be picked as victim, include number of
  rollback in cost factor
Quiz
 Consider the following and answer the questions that follow:
 P = P1, P2, P3, P4, P5
 R = R1, R2, R3, R4
 Instances: R1=2, R2=3, R3=1, R4=1
 E={ R3 -> P1, P1 -> R2, R2 -> P2, R2 -> P4, R1 -> P2, R1 -> P3,
     P4 -> R4}
 2 points each:
  Question 1: Is there a cycle? (Y/N)
  Question 2: Is there a deadlock? (Y/N)
  Question 3: Will P1’s request for R2 be granted? (Y/N)
  Question 4: Will P4’s request for R4 be granted? (Y/N)
  Question 5: How many available instances are there for R2?
Quiz
 8 processes P0 through P7; 4 resource types:
  A (10 instances), B (5), C (11) and D (6).
 Snapshot at time T0:
                 Allocation       Max
                 ABCD                    ABCD
       P0        0121             7492
       P1        2100             3220
       P2        0020             9072
       P3        2102             2243
       P4        0020             4363
       P5        2020             2132
       P6        2111             5221
       P7        1020             7485
Question 6: Is the system safe?
Question 7: If yes, what is the safe sequence? If no, what

More Related Content

What's hot (20)

PPTX
Process synchronization
Ali Ahmad
 
DOCX
BANKER'S ALGORITHM
Muhammad Baqar Kazmi
 
PPT
Deadlock
Rajandeep Gill
 
PPT
deadlock avoidance
wahab13
 
PDF
OS - Deadlock
vinay arora
 
PPTX
Operating system - Deadlock
Shashank Yenurkar
 
PDF
Deadlock Avoidance - OS
MsAnita2
 
PPT
Deadlock Detection in Distributed Systems
DHIVYADEVAKI
 
PPTX
GC in C#
Kamal1997
 
PDF
Bankers
mandeep kaur virk
 
PPT
Overfitting and-tbl
Digvijay Singh
 
PPTX
Deadlock Prevention
prachi mewara
 
PPTX
Semophores and it's types
Nishant Joshi
 
PPTX
Deadlock Avoidance in Operating System
Mohammad Hafiz-Al-Masud
 
PPTX
Dead Lock in operating system
Ali Haider
 
PPTX
deadlock handling
Suraj Kumar
 
PPTX
VALIDATION BASED PROTOCOL AND MULTIPLE GRANULARITY.pptx
SamyakJain710491
 
PPTX
Dining philosopher
.AIR UNIVERSITY ISLAMABAD
 
PPTX
Semaphore
LakshmiSamivel
 
PPT
Chapter 7 - Deadlocks
Wayne Jones Jnr
 
Process synchronization
Ali Ahmad
 
BANKER'S ALGORITHM
Muhammad Baqar Kazmi
 
Deadlock
Rajandeep Gill
 
deadlock avoidance
wahab13
 
OS - Deadlock
vinay arora
 
Operating system - Deadlock
Shashank Yenurkar
 
Deadlock Avoidance - OS
MsAnita2
 
Deadlock Detection in Distributed Systems
DHIVYADEVAKI
 
GC in C#
Kamal1997
 
Overfitting and-tbl
Digvijay Singh
 
Deadlock Prevention
prachi mewara
 
Semophores and it's types
Nishant Joshi
 
Deadlock Avoidance in Operating System
Mohammad Hafiz-Al-Masud
 
Dead Lock in operating system
Ali Haider
 
deadlock handling
Suraj Kumar
 
VALIDATION BASED PROTOCOL AND MULTIPLE GRANULARITY.pptx
SamyakJain710491
 
Dining philosopher
.AIR UNIVERSITY ISLAMABAD
 
Semaphore
LakshmiSamivel
 
Chapter 7 - Deadlocks
Wayne Jones Jnr
 

Viewers also liked (20)

PPTX
Kerberos
Gichelle Amon
 
PDF
RBM、Deep Learningと学習(全脳アーキテクチャ若手の会 第3回DL勉強会発表資料)
Takuma Yagi
 
PPTX
Women in Tech: How to Build A Human Company
Luminary Labs
 
PDF
JAISTサマースクール2016「脳を知るための理論」講義02 Synaptic Learning rules
hirokazutanaka
 
PDF
JAISTサマースクール2016「脳を知るための理論」講義04 Neural Networks and Neuroscience
hirokazutanaka
 
PPT
Network security
Gichelle Amon
 
PDF
強化学習勉強会・論文紹介(Kulkarni et al., 2016)
Sotetsu KOYAMADA(小山田創哲)
 
PDF
Why dont you_create_new_spark_jl
Shintaro Fukushima
 
PDF
Probabilistic Graphical Models 輪読会 #1
Takuma Yagi
 
PDF
KDD2016論文読み会資料(DeepIntent)
Sotetsu KOYAMADA(小山田創哲)
 
PDF
最近のRのランダムフォレストパッケージ -ranger/Rborist-
Shintaro Fukushima
 
PDF
機械学習によるデータ分析 実践編
Ryota Kamoshida
 
PDF
Rユーザのためのspark入門
Shintaro Fukushima
 
PPTX
JAISTサマースクール2016「脳を知るための理論」講義03 Network Dynamics
hirokazutanaka
 
PDF
【強化学習】Montezuma's Revenge @ NIPS2016
Sotetsu KOYAMADA(小山田創哲)
 
PDF
論文紹介:Using the Forest to See the Trees: A Graphical. Model Relating Features,...
Takuma Yagi
 
PDF
機械学習によるデータ分析まわりのお話
Ryota Kamoshida
 
PPTX
What is the maker movement?
Luminary Labs
 
PDF
The Human Company Playbook, Version 1.0
Luminary Labs
 
PDF
Hype vs. Reality: The AI Explainer
Luminary Labs
 
Kerberos
Gichelle Amon
 
RBM、Deep Learningと学習(全脳アーキテクチャ若手の会 第3回DL勉強会発表資料)
Takuma Yagi
 
Women in Tech: How to Build A Human Company
Luminary Labs
 
JAISTサマースクール2016「脳を知るための理論」講義02 Synaptic Learning rules
hirokazutanaka
 
JAISTサマースクール2016「脳を知るための理論」講義04 Neural Networks and Neuroscience
hirokazutanaka
 
Network security
Gichelle Amon
 
強化学習勉強会・論文紹介(Kulkarni et al., 2016)
Sotetsu KOYAMADA(小山田創哲)
 
Why dont you_create_new_spark_jl
Shintaro Fukushima
 
Probabilistic Graphical Models 輪読会 #1
Takuma Yagi
 
KDD2016論文読み会資料(DeepIntent)
Sotetsu KOYAMADA(小山田創哲)
 
最近のRのランダムフォレストパッケージ -ranger/Rborist-
Shintaro Fukushima
 
機械学習によるデータ分析 実践編
Ryota Kamoshida
 
Rユーザのためのspark入門
Shintaro Fukushima
 
JAISTサマースクール2016「脳を知るための理論」講義03 Network Dynamics
hirokazutanaka
 
【強化学習】Montezuma's Revenge @ NIPS2016
Sotetsu KOYAMADA(小山田創哲)
 
論文紹介:Using the Forest to See the Trees: A Graphical. Model Relating Features,...
Takuma Yagi
 
機械学習によるデータ分析まわりのお話
Ryota Kamoshida
 
What is the maker movement?
Luminary Labs
 
The Human Company Playbook, Version 1.0
Luminary Labs
 
Hype vs. Reality: The AI Explainer
Luminary Labs
 
Ad

Similar to Os module 2 d (20)

PPTX
Module-3 Deadlocks.pptx BCS303 Operating system
ambikavenkatesh2
 
PPT
A ppt on deadlock in operating systems for the better explanation
AkashPundir2
 
PPT
Mch7 deadlock
wahab13
 
PPT
Principles of Operating system and types
dilipkumarcontact
 
PPTX
deadlock in OS.pptx
Monirul Islam
 
PPT
Deadlock.ppt
JeelBhanderi4
 
PPT
Process Synchronization And Deadlocks
tech2click
 
PPTX
Deadlock - An Operating System Concept.pptx
viceprincipalbfc
 
PDF
10. deadlock
Robbie AkaChopa
 
DOCX
Module3
dilshad begum
 
DOCX
Deadlocks prefinal
marangburu42
 
DOCX
Deadlocksprefinal 161014115456
marangburu42
 
PPTX
Gp1242 007 oer ppt
Nivedita Kasturi
 
DOCX
Deadlocks 160928121516-160928183232
marangburu42
 
DOCX
deadlock-operating-systems (1jjhkh).docx
Dpak Chavan
 
PPTX
Chapter 4
ushabarad142
 
PDF
Deadlocks
Shipra Swati
 
PPTX
Deadlock
Abhinaw Rai
 
DOC
Deadlock- System model, resource types, deadlock problem, deadlock characteri...
Wakil Kumar
 
PPTX
Deadlock in Operating System
AUST
 
Module-3 Deadlocks.pptx BCS303 Operating system
ambikavenkatesh2
 
A ppt on deadlock in operating systems for the better explanation
AkashPundir2
 
Mch7 deadlock
wahab13
 
Principles of Operating system and types
dilipkumarcontact
 
deadlock in OS.pptx
Monirul Islam
 
Deadlock.ppt
JeelBhanderi4
 
Process Synchronization And Deadlocks
tech2click
 
Deadlock - An Operating System Concept.pptx
viceprincipalbfc
 
10. deadlock
Robbie AkaChopa
 
Module3
dilshad begum
 
Deadlocks prefinal
marangburu42
 
Deadlocksprefinal 161014115456
marangburu42
 
Gp1242 007 oer ppt
Nivedita Kasturi
 
Deadlocks 160928121516-160928183232
marangburu42
 
deadlock-operating-systems (1jjhkh).docx
Dpak Chavan
 
Chapter 4
ushabarad142
 
Deadlocks
Shipra Swati
 
Deadlock
Abhinaw Rai
 
Deadlock- System model, resource types, deadlock problem, deadlock characteri...
Wakil Kumar
 
Deadlock in Operating System
AUST
 
Ad

More from Gichelle Amon (20)

PPT
Os module 2 c
Gichelle Amon
 
PPT
Image segmentation ppt
Gichelle Amon
 
PPT
Lec3 final
Gichelle Amon
 
PPTX
Lec 3
Gichelle Amon
 
PPT
Lec2 final
Gichelle Amon
 
PPTX
Lec 4
Gichelle Amon
 
PPT
Lec1 final
Gichelle Amon
 
DOC
Module 3 law of contracts
Gichelle Amon
 
PPT
Transport triggered architecture
Gichelle Amon
 
PPT
Time triggered arch.
Gichelle Amon
 
PPT
Subnetting
Gichelle Amon
 
PPT
Os module 2 c
Gichelle Amon
 
PPT
Os module 2 ba
Gichelle Amon
 
PPT
Lec5
Gichelle Amon
 
PPT
Delivery
Gichelle Amon
 
PPT
Addressing
Gichelle Amon
 
PPT
6 spatial filtering p2
Gichelle Amon
 
PPT
5 spatial filtering p1
Gichelle Amon
 
PPT
Medical image analysis
Gichelle Amon
 
PPTX
Presentation2
Gichelle Amon
 
Os module 2 c
Gichelle Amon
 
Image segmentation ppt
Gichelle Amon
 
Lec3 final
Gichelle Amon
 
Lec2 final
Gichelle Amon
 
Lec1 final
Gichelle Amon
 
Module 3 law of contracts
Gichelle Amon
 
Transport triggered architecture
Gichelle Amon
 
Time triggered arch.
Gichelle Amon
 
Subnetting
Gichelle Amon
 
Os module 2 c
Gichelle Amon
 
Os module 2 ba
Gichelle Amon
 
Delivery
Gichelle Amon
 
Addressing
Gichelle Amon
 
6 spatial filtering p2
Gichelle Amon
 
5 spatial filtering p1
Gichelle Amon
 
Medical image analysis
Gichelle Amon
 
Presentation2
Gichelle Amon
 

Recently uploaded (20)

PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
Learn Computer Forensics, Second Edition
AnuraShantha7
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PPTX
Q2 Leading a Tableau User Group - Onboarding
lward7
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
Learn Computer Forensics, Second Edition
AnuraShantha7
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Q2 Leading a Tableau User Group - Onboarding
lward7
 

Os module 2 d

  • 2. Objectives  To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks  To present a number of different methods for preventing or avoiding deadlocks in a computer system.
  • 3. Deadlock  Defined as the permanent blocking of a set of processes that compete for a system resource, including database records and communication lines  A situation wherein each of a collection of processes is waiting for something from other processes in the collection. Since all are waiting, none can provide any of the things being waited for.
  • 4. Deadlock  A situation wherein a process is requesting a resource which is withheld by another process and never get hold of the resource.
  • 5. Example  System has 2 tape drives  P1 and P2 each hold one tape drive and each needs another one.  Semaphores A and B initialized to 1  P0 P1  Wait(A); Wait(B);  Wait(B); Wait(A);
  • 6. Deadlock  Deadlock occurs when a set of processes are in a wait state, because each process is waiting for a resource that is held by some other waiting process. Therefore, all deadlocks involve conflicting resource that are needed by two or more processes.
  • 7. Bridge Crossing Example  Traffic only in one direction.  Each section of a bridge can be viewed as a resource.  If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback).  Several cars may have to be backed up if a deadlock occurs.  Starvation is possible.
  • 8. Deadlock  Unlike some other problems in multiprogramming systems, there is no efficient solution to the deadlock problem in the general case. It is one area where there is a strong theory but it is completely ignored in practice because solutions to deadlock are expensive.
  • 9. Resource  A hardware device (e.g. tape drive, memory)  A piece of information (e.g. a locked record in a database)
  • 10. General Categories of Resources  Reusable – something that can be safely used by one process at a time and is not depleted by that use. Processes obtain resources that they later release for reuse by others.  Example:  CPU, Memory, specific I/O devices or files.
  • 11. General Categories of Resources  Consumable – these can be created and destroyed. When a resource is acquired by a process, the resource ceases to exist.  Examples:  Interrupts, signals, messages.
  • 12. Types of Resources  Preemptable resource – a resource that can be taken away from the process owning it with no ill effect (needs save/restore)  Example: Memory or CPU
  • 13. Types of Resources  Non-preemptable – a resource that cannot be taken away from its current owner without causing the computation to fail.  Example: Printer or floppy disk.
  • 14. Note  Deadlocks occur when sharing reusable and non-preemptable resources
  • 15. Pop Exercise  Classify the following as either sharable or non-sharable  A. Hard disk  B. file  C. keyboard
  • 16. System Model  System consists of a finite number of resources and a finite number of processes competing for resources.  Resources are partitioned into several types, each of which consists of some number of identical instances
  • 17. System Model  When a process requests an instance of a resource type, the allocation of any instance of the type will satisfy the request (otherwise, resource types have not been defined properly).
  • 18. Protocol for resource utilization  Request – the process will request for a certain 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 the resource  Release – the process relinquishes the resources.
  • 19. Note  A set of processes is deadlocked when every process in the set is waiting for an event that can be caused only by another process in the set.
  • 20. Deadlock Characterization 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.  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 P0 is waiting for a resource that is held by P0.
  • 21. Additional characteristic  PROCESS IRREVERSIBLE – Unable to reset to an earlier state where resources are not held.
  • 22. Systems avoiding deadlocks  Systems involving only shared resources cannot deadlock. – negates mutual exclusion  System that aborts processes which is requesting a resource but currently being used. – negates hold and wait  Preemption may be possible if a process does not use its resources until it has acquired all it needs – negates no preemption
  • 23. Systems…  Transaction processing systems provide checkpoints so that processes may back out of a transaction. – negates irreversible process  System that detects or avoids deadlocks – prevents cycle.
  • 24. Resource-Allocation Graph (RAG)  Consists of a set of vertices V and a set of edges E. Devised by Holt in 1972.  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
  • 25. Resource-Allocation Graph (RAG)  Process  Resource Type with 4 instances  Pi requests instance of Rj Pi Rj  Pi is holding an instance of Rj Pi Rj
  • 26. Example  P = P1, P2, P3  R = R1, R2, R3, R4  Instances: R1=1, R2=2, R3=1, R4=3  E={ P1 -> R1, P2 -> R3, R1 -> P2, R2 -> P1, R2 -> P2, R3 -> P3,  Question: is there a deadlock?
  • 28. Resource Allocation Graph With A Deadlock
  • 29. Graph With A Cycle But No Deadlock
  • 30. Basic Facts  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.
  • 31. Methods for handling deadlocks  3 strategies: 1. Ostrich Solution: Ignore the problem and pretend deadlocks never occur in the system. Used by most OS, including Unix
  • 32. Methods for handling deadlocks 2. Ensure that the system will never enter a deadlock state.  Deadlock prevention – Design a system in such a way that the 4 conditions leading to deadlock will never occur.  Deadlock avoidance – make a decision dynamically checking whether the request will, if granted, potentially lead to a deadlock or not.
  • 33. Methods for handling deadlocks 3. Allow the system to enter a deadlock state and recover  Deadlock detection and recovery
  • 34. Deadlock Prevention  Preventing the occurrence of one of the necessary conditions leading to deadlock. Deadlock prevention strategy is very conservative. It solves the problem of deadlock by limiting access to resources and by imposing restrictions on processes.
  • 35. Mutual Exclusion  In general, this condition cannot be required for non-sharable resources but is a must for sharable resources.
  • 36. Hold and Wait  Must guarantee that whenever a process requests a resource, it does not hold any other resources. A process is blocked until all requests can be granted simultaneously.  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
  • 37. No preemption  If a process that is holding some resource requests another resource that cannot be immediately allocated to it, then all resources currently being held are released.  Preempted resources are added to the list of resources for which the process is waiting.  Process will be restarted only when it can regain its old resources, as well as new ones that it is requesting.
  • 38. Circular Wait  Impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.
  • 39. 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.
  • 40. Ways to avoid deadlock by careful resource allocation 1. Resource trajectories 2. Safe/Unsafe states 3. Dijkstra’s Banker’s Algorithm
  • 41. 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.
  • 42. Safe, Unsafe , Deadlock State
  • 43. Avoidance algorithms  Single instance of a resource type. Use a resource-allocation graph  Multiple instances of a resource type. Use the banker’s algorithm
  • 44. Resource-Allocation Graph Scheme  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.
  • 46. Unsafe State In Resource-Allocation Graph
  • 47. Sample Algorithm  Given the RAG of the system (Initially on a safe state, grant a resource allocation request if and only if doing so will not introduce a cycle in the graph. (taking into consideration all assignment, request and claim edges). Otherwise, the requesting process has to wait.
  • 48. Sample Algorithm 1. Operation: P2 requests R3 Action: Request granted System State: SAFE 2. Operation: P3 requests R2 Action: REQUEST DENIED!!! System State: Leads to deadlock
  • 49. Sample 2  Consider a system with 15 identical instances of a resource Process Max Need Curr Allo Remaining P0 14 5 P1 10 5 P2 3 2  Currently available resource: 15 – (5+5+2) = 3  Is it safe?  Yes with a safe sequence of <P2, P1, P0>  What if P0 requests two more resources, will it be granted?  No, OS cannot satisfy remaining needs of all processes. Unsafe.
  • 50. Sample 3  Total instances: 8 Process Max Need Curr Allo Remaining P0 5 3 P1 8 1 P2 7 2  Is it safe or unsafe? What is the safe sequence?
  • 51. Sample 4  Total instances: 8 Process Max Need Curr Allo Remaining P0 5 3 P1 8 1 P2 7 2  Is it safe or unsafe? What is the safe sequence?
  • 52. 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.
  • 53. Data Structures for the Banker’s Algorithm  Let n = number of processes, and m = number of resources types.  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. Need [i,j] = Max[i,j] – Allocation [i,j].
  • 54. 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- 1. 2. Find and i such that both: (a) Finish [i] = false (b) Needi ≤ Work If no such i exists, go to step 4. 3. 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.
  • 55. Example of Banker’s Algorithm  5 processes P0 through P4; 3 resource types: A (10 instances), B (5instances), and C (7 instances).  Snapshot at time T0: Allocation Max Available ABC ABC ABC P0 010 753 332 P1 200 322 P2 302 902 P3 211 222 P4 002 433
  • 56. Example (Cont.)  The content of the matrix Need is defined to be Max – Allocation. Need ABC P0 743 P1 122 P2 600 P3 011 P4 431  The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria.
  • 57. Example: P1 Request (1,0,2)  Check that Request ≤ Available (that is, (1,0,2) ≤ (3,3,2) ⇒ true. Allocation Need Available ABC ABC ABC P0 010 743 230 P1 302 020 P2 302 600 P3 211 011 P4 002 431  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?
  • 58. Deadlock detection and recovery  Implies no conscious effort to prevent/avoid deadlocks  At certain times during processing, the system executes an algorithm to determine whether or not a deadlock has occurred.  If so, an attempt to recover from the deadlock is made.
  • 59. Detection Algorithm 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. 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.
  • 60. Resource-Allocation Graph and Wait-for Graph Resource-Allocation Graph Corresponding wait-for graph
  • 61. Several Instances of a Resource Type  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.
  • 62. Detection Algorithm 1. Let Work and Finish be vectors of length m and n, respectively Initialize: (a) Work = Available n 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. 3. 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.
  • 63. Note: The algorithm requires an order of O(m x n2) operations to detect whether the system is in deadlocked state.
  • 64. Example of Detection Algorithm  Five processes P0 through P4; three resource types A (7 instances), B (2 instances), and C (6 instances).  Snapshot at time T0: Allocation Request Available ABC ABC ABC P0 010 000 000 P1 200 202 P2 303 000 P3 211 100 P4 002 002  Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i. Therefore, no deadlock
  • 65. Example (cont.)  P2 requests an additional instance of type C. Request ABC P0 000 P1 202 P2 001 P3 100 P4 002  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.
  • 66. Detection Algorithm Usage  When and how often, to invoke deadlock detection?  Factors to consider:  How often a deadlock is likely to occur  How many process will need to be rolled back  Approaches  Execute the algorithm at predetermined time interval  Execute the algorithm whenever a resource request cannot be granted immediatetly
  • 67. Recovery from deadlock: Process Termination  Who’s responsibility?  Inform the operator and let him deal with the deadlock manually  Allow the system to recover from deadlock automatically.
  • 68. Approaches: I. Process Termination  Abort all deadlocked processes  Abort one process at a time until the deadlock cycle is eliminated
  • 69. In which order should we choose to abort?  Priority of process  How long process has computed and how much longer to completion  Resources the process has used  Resources process needs to complete  How many process will need to be terminated?  Is process interactive or batch?
  • 70. II. Process preemption  Selecting a victim – minimize cost  Rollback – return to some safe state, restart process for that state.  Starvation – same process may always be picked as victim, include number of rollback in cost factor
  • 71. Quiz Consider the following and answer the questions that follow: P = P1, P2, P3, P4, P5 R = R1, R2, R3, R4 Instances: R1=2, R2=3, R3=1, R4=1 E={ R3 -> P1, P1 -> R2, R2 -> P2, R2 -> P4, R1 -> P2, R1 -> P3, P4 -> R4} 2 points each:  Question 1: Is there a cycle? (Y/N)  Question 2: Is there a deadlock? (Y/N)  Question 3: Will P1’s request for R2 be granted? (Y/N)  Question 4: Will P4’s request for R4 be granted? (Y/N)  Question 5: How many available instances are there for R2?
  • 72. Quiz  8 processes P0 through P7; 4 resource types: A (10 instances), B (5), C (11) and D (6).  Snapshot at time T0: Allocation Max ABCD ABCD P0 0121 7492 P1 2100 3220 P2 0020 9072 P3 2102 2243 P4 0020 4363 P5 2020 2132 P6 2111 5221 P7 1020 7485 Question 6: Is the system safe? Question 7: If yes, what is the safe sequence? If no, what