SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Downloaden Sie, um offline zu lesen
Deadlocks
 System Model
 Deadlock Characterization
 Methods for Handling Deadlocks
Deadlock Prevention
 Deadlock Avoidance
 Deadlock Detection
 Recovery from Deadlock
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 disk drives.
   – P1 and P2 each hold one disk drive and each needs
     another one.
• Example
   – semaphores A and B, initialized to 1
               P0                P1
             wait (A);          wait(B)
             wait (B);          wait(A)


                         Loganathan R, CSE, HKBKCE       2
1. System Model
• A system consists of a finite number of resources to be distributed among a
   number of competing processes
• Resource types R1, R2, . . ., Rm
        CPU cycles, memory space, files, I/O devices(Printers,DVDs,
• Each resource type Ri may have Wi instances.
• Each process utilizes a resource in only the following sequence
1. Request : If the request cannot be granted immediately, then the
    requesting process must wait until it can acquire the resource
2. Use :The process can operate on the resource
3. Release :The process releases the resource
• The request and release of resources are system calls, for example
    – request () and release() device,
    – open() and close () file,
    – allocate () and free () memory system calls
• Multithreaded programs are good candidates for deadlock because multiple
   threads can compete for shared resources


                            Loganathan R, CSE, HKBKCE                      3
2. Deadlock Characterization
2.1 Necessary Conditions
• Deadlock can arise if four conditions hold simultaneously.
1. Mutual exclusion: only one process at a time can use a
   resource.
2. Hold and wait: a process holding at least one resource is
   waiting to acquire additional resources held by other
   processes.
3. No preemption: a resource can be released only voluntarily by
   the process holding it, after that process has completed its
   task.
4. 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.
                         Loganathan R, CSE, HKBKCE               4
2. Deadlock Characterization                                   Contd…


2.2 Resource-Allocation Graph
• Deadlocks can be described more precisely in terms of a directed graph
  called a system resource-allocation graph
• Consists of 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.
• Request edge – directed edge Pi  Rj
• Assignment edge – directed edge Rj  Pi
• Symbols Used in the graph
                                                            Pi
                                                                 Rj
         Process                                Pi requests instance of Rj

                                                            Pi
                                                                 Rj
Resource Type with 4 instances                  Pi is holding an instance of Rj
                                Loganathan R, CSE, HKBKCE                           5
2. Deadlock Characterization                               Contd…

Example of a Resource Allocation Graph
• The sets P, R, and £ :
    P={P1,P2,P3}
    R= {R1,R2,R3,R4}
    £ = {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
• 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 R2 and is waiting for an instance of R3
  • Process P3 is holding an instance of R3.
• 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 only one instance per resource type, then deadlock.
     if several instances per resource type, possibility of deadlock.
                                Loganathan R, CSE, HKBKCE                        6
2. Deadlock Characterization                                Contd…

Graph With Deadlock
Cycles: P1 → R1 → P2 → R3 → P3 → R2 → P1            Graph With A Cycle But No Deadlock
           P2 → R3 → P3 → R2 → P2
                                                    Cycle : P1→R1 →P3 →R2 →P1
Processes P1, P2, and P3 are deadlocked. Process
P2 is waiting for resource R3, which is held by P3.
                                                    P4 May Release R2 So No Deadlock
Process P3 is waiting for P1 or P2 to release R2
Process P1 is waiting for process P2 to release
resource R1




                               Loganathan R, CSE, HKBKCE                         7
3. Methods for Handling Deadlocks
• Three Ways to Deal deadlock problem
1. Use a protocol to prevent or avoid deadlocks, ensuring
   that the system will never enter a deadlock state.
   – Deadlock prevention provides a set of methods for ensuring
     that at least one of the necessary cannot hold
   – Deadlock avoidance requires the information of which
     resources a process will request and use during its lifetime, the
     resources currently available, the resources currently allocated
     to each process, the future requests and releases of each
     process then it can decide for each request whether or not the
     process should wait
2. Allow the system to enter a deadlock state, detect it and
   recover.
3. Ignore the problem and pretend that deadlocks never
   occur in the system; used by most operating systems,
   including UNIX.

                         Loganathan R, CSE, HKBKCE                       8
4. Deadlock Prevention
Restrain the ways request can be made
• Mutual Exclusion – not required for sharable resources; must hold for non
  sharable resources.
• 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.
• No Preemption
 – If a process that is holding some resources 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 the
   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.


                                 Loganathan R, CSE, HKBKCE                          9
5. 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.
5.1 Safe State
• System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the
  processes in 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.
• That is:
   – If Pi resource needs are not immediately available, then Pi can wait until
       all Pj have 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.

                              Loganathan R, CSE, HKBKCE                      10
5. Deadlock Avoidance                            Contd…

•   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.
•   Example
    A system with 12 magnetic tape drives and three processes: P0,P1 and P2
  Process Max Needs Current Needs
     P0         10            5
     P1          4            2
     P2          9            2
  At time t0 (3 remaining) the system with
  sequence <P1, P0, P2> is in a safe state
  At time t0,P2 requests 1 more drive and
  its allocated ( remaining 2), system can
  go from a safe state to an unsafe state
For deadlock avoidance :
1. Single instance of a resource type use a
    resource-allocation graph
2. Multiple instances of a resource type.
    Use the banker’s algorithm
                                               Safe, unsafe, and deadlock state spaces
                              Loganathan R, CSE, HKBKCE                            11
5. Deadlock Avoidance                          Contd…


5.2 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.
• The request can be granted only if converting the request edge Pi  Rj to an
  assignment edge Rj —> Pi does not result in the formation of a cycle
• Example
                                      An unsafe state in a
                                      resource-allocation
                                      graph




                     Resource-allocation
                     graph for deadlock
                     avoidance
                             Loganathan R, CSE, HKBKCE                      12
5. Deadlock Avoidance                       Contd…


5.3 Banker’s Algorithm
• Multiple instances of a resource
• 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.
• Let n = number of processes, and m = number of resources types.
• Data structures
  – 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].
                           Loganathan R, CSE, HKBKCE                   13
5. Deadlock Avoidance                      Contd…


5.3.1 Safety Algorithm
• To find whether or not a system is in a safe state
  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.



                           Loganathan R, CSE, HKBKCE                  14
5. Deadlock Avoidance                          Contd…


5.3.2 Resource-Request Algorithm
• Determines if requests can be safely granted
• Let Requesti is request vector for process Pi.
• If Requesti [j] = k then process Pi wants k instances of resource
  type Rj then following actions are taken
  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 – 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
                               Loganathan R, CSE, HKBKCE                       15
5. Deadlock Avoidance                               Contd…


• Example : 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
•   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.              Loganathan R, CSE, HKBKCE                            16
5. Deadlock Avoidance                          Contd…



Can request for (1,0,2) by P1 be granted?
• Check that Request  Available i.e. (1,0,2)  (3,3,2)  true.
                  Allocation      Need      Available
                     ABC          ABC          ABC
              P0     010          743          230
              P1     302          020
              P2     301          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?
   P4 cannot be granted, since the resources are not available
• Can request for (0,2,0) by P0 be granted?
   P0 cannot be granted, since the resulting state is unsafe



                              Loganathan R, CSE, HKBKCE                       17
6.Deadlock Detection
6.1 Single Instance of Each Resource Type
• All resources have only a single instance
• 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.


     Corresponding wait-for graph
                                                           Resource-Allocation Graph




                               Loganathan R, CSE, HKBKCE                           18
6.Deadlock Detection                         Contd…

6.2 Several Instances of a Resource Type
• Deadlock detection algorithm employs several time-varying data structures
  – 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.
• 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.
                              Loganathan R, CSE, HKBKCE                       19
6.Deadlock Detection                      Contd…

  3. Work = Work + Allocationi
     Finish[i] = true go to step 2.
  4. If Finish[i] == false, for some i, 0  i  n, then the system is in
     deadlock state. Moreover, if Finish[i] == false, then Pi is deadlocked.
• Example : 5 processes P0 through P4
  – 3 resource types A (7 instances), B (2 instances), and C (6 instances).
  – Snapshot at time T0:
                  Allocation Request Available
                    ABC         ABC         ABC
              P0 0 1 0          000         000
              P1 2 0 0          202
              P2 3 0 3          000
              P3 2 1 1          100
              P4 0 0 2          002
• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.


                           Loganathan R, CSE, HKBKCE                     20
6.Deadlock Detection                        Contd…
• P2 requests an additional instance of type C.
                                  Request
                                   ABC
                             P0    000
                             P1    201
                             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.
6.3 Detection-Algorithm Usage
• When to invoke depends on:
     – How often a deadlock is likely to occur?
     – How many processes will need to be rolled back?
• 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.
                            Loganathan R, CSE, HKBKCE                       21
7. Recovery from Deadlock
There is 2 methods
  Process Termination and Resource Preemption
7.1 Process Termination
• Abort all deadlocked processes
  – Expensive- Computed for a long time and results of partial computations
• Abort one process at a time until the deadlock cycle is
  eliminated
  – After each process is aborted, a deadlock-detection algorithm must be
    invoked
  – In which order should we choose to abort?
   1. Priority of the process.
   2. How long process has computed, and how much longer to
        completion.
   3. Resources the process has used.
   4. Resources process needs to complete.
   5. How many processes will need to be terminated.
   6. Is process interactive or batch?
                           Loganathan R, CSE, HKBKCE                        22
7. Recovery from Deadlock
7.2 Resource Preemption
• Preempt some resources from processes and give
  these resources to other processes until the deadlock
  cycle is broken
• Issues
  1. Selecting a victim
      minimize cost - number of resources held and amount of
     time consumed
  2. Rollback – return to some safe state, restart process for
     that state.
  3. Starvation – same process may always be picked as victim,
     include number of rollback in cost factor.

                      Loganathan R, CSE, HKBKCE            23

Weitere ähnliche Inhalte

Was ist angesagt?

Free space managment46
Free space managment46Free space managment46
Free space managment46
myrajendra
 
Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock Galvin
Sonali Chauhan
 
Deadlock in distribute system by saeed siddik
Deadlock in distribute system by saeed siddikDeadlock in distribute system by saeed siddik
Deadlock in distribute system by saeed siddik
Saeed Siddik
 

Was ist angesagt? (20)

Bankers algorithm
Bankers algorithmBankers algorithm
Bankers algorithm
 
deadlock handling
deadlock handlingdeadlock handling
deadlock handling
 
CPU Scheduling in OS Presentation
CPU Scheduling in OS  PresentationCPU Scheduling in OS  Presentation
CPU Scheduling in OS Presentation
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.
 
Process management os concept
Process management os conceptProcess management os concept
Process management os concept
 
Dead Lock in operating system
Dead Lock in operating systemDead Lock in operating system
Dead Lock in operating system
 
Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)
 
Free space managment46
Free space managment46Free space managment46
Free space managment46
 
Chapter 7 - Deadlocks
Chapter 7 - DeadlocksChapter 7 - Deadlocks
Chapter 7 - Deadlocks
 
Operating system - Deadlock
Operating system - DeadlockOperating system - Deadlock
Operating system - Deadlock
 
Threads .ppt
Threads .pptThreads .ppt
Threads .ppt
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 
Deadlock Detection in Distributed Systems
Deadlock Detection in Distributed SystemsDeadlock Detection in Distributed Systems
Deadlock Detection in Distributed Systems
 
Methods for handling deadlocks
Methods for handling deadlocksMethods for handling deadlocks
Methods for handling deadlocks
 
Distributed file system
Distributed file systemDistributed file system
Distributed file system
 
Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock Galvin
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
 
Deadlock in distribute system by saeed siddik
Deadlock in distribute system by saeed siddikDeadlock in distribute system by saeed siddik
Deadlock in distribute system by saeed siddik
 
Distributed Operating Systems
Distributed Operating SystemsDistributed Operating Systems
Distributed Operating Systems
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-Systems
 

Ähnlich wie 7 Deadlocks

Module 3 Deadlocks.pptx
Module 3 Deadlocks.pptxModule 3 Deadlocks.pptx
Module 3 Deadlocks.pptx
shreesha16
 
Deadlock in Real Time operating Systempptx
Deadlock in Real Time operating SystempptxDeadlock in Real Time operating Systempptx
Deadlock in Real Time operating Systempptx
ssuserca5764
 
14th November - Deadlock Prevention, Avoidance.ppt
14th November - Deadlock Prevention, Avoidance.ppt14th November - Deadlock Prevention, Avoidance.ppt
14th November - Deadlock Prevention, Avoidance.ppt
Unknown664473
 
Mch7 deadlock
Mch7 deadlockMch7 deadlock
Mch7 deadlock
wahab13
 
DeadlockMar21.ppt
DeadlockMar21.pptDeadlockMar21.ppt
DeadlockMar21.ppt
hetrathod001
 
OS Module-3 (2).pptx
OS Module-3 (2).pptxOS Module-3 (2).pptx
OS Module-3 (2).pptx
KokilaK25
 

Ähnlich wie 7 Deadlocks (20)

Deadlock (1).ppt
Deadlock (1).pptDeadlock (1).ppt
Deadlock (1).ppt
 
Os5
Os5Os5
Os5
 
Module 3 Deadlocks.pptx
Module 3 Deadlocks.pptxModule 3 Deadlocks.pptx
Module 3 Deadlocks.pptx
 
6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx6. Deadlock_1640227623705.pptx
6. Deadlock_1640227623705.pptx
 
Ch7 deadlocks
Ch7   deadlocksCh7   deadlocks
Ch7 deadlocks
 
Deadlock
DeadlockDeadlock
Deadlock
 
deadlock part5 unit 2.ppt
deadlock part5 unit 2.pptdeadlock part5 unit 2.ppt
deadlock part5 unit 2.ppt
 
Ch07 deadlocks
Ch07 deadlocksCh07 deadlocks
Ch07 deadlocks
 
Deadlock in Real Time operating Systempptx
Deadlock in Real Time operating SystempptxDeadlock in Real Time operating Systempptx
Deadlock in Real Time operating Systempptx
 
14th November - Deadlock Prevention, Avoidance.ppt
14th November - Deadlock Prevention, Avoidance.ppt14th November - Deadlock Prevention, Avoidance.ppt
14th November - Deadlock Prevention, Avoidance.ppt
 
Chapter5_Deadlock.pptx
Chapter5_Deadlock.pptxChapter5_Deadlock.pptx
Chapter5_Deadlock.pptx
 
Mch7 deadlock
Mch7 deadlockMch7 deadlock
Mch7 deadlock
 
DeadlockMar21.ppt
DeadlockMar21.pptDeadlockMar21.ppt
DeadlockMar21.ppt
 
deadlocks.pptx
deadlocks.pptxdeadlocks.pptx
deadlocks.pptx
 
OS Module-3 (2).pptx
OS Module-3 (2).pptxOS Module-3 (2).pptx
OS Module-3 (2).pptx
 
OS deadlock.pptx
OS deadlock.pptxOS deadlock.pptx
OS deadlock.pptx
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
23 deadlock
23 deadlock23 deadlock
23 deadlock
 
Deadlock
DeadlockDeadlock
Deadlock
 
Deadlock
DeadlockDeadlock
Deadlock
 

Mehr von Dr. Loganathan R

Mehr von Dr. Loganathan R (20)

Ch 6 IoT Processing Topologies and Types.pdf
Ch 6 IoT Processing Topologies and Types.pdfCh 6 IoT Processing Topologies and Types.pdf
Ch 6 IoT Processing Topologies and Types.pdf
 
IoT Sensing and Actuation.pdf
 IoT Sensing and Actuation.pdf IoT Sensing and Actuation.pdf
IoT Sensing and Actuation.pdf
 
Ch 4 Emergence of IoT.pdf
Ch 4 Emergence of IoT.pdfCh 4 Emergence of IoT.pdf
Ch 4 Emergence of IoT.pdf
 
Program in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointersProgram in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointers
 
Implement a queue using two stacks.
Implement a queue using two stacks.Implement a queue using two stacks.
Implement a queue using two stacks.
 
Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3
 
Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2
 
Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3
 
Bcsl 033 data and file structures lab s4-2
Bcsl 033 data and file structures lab s4-2Bcsl 033 data and file structures lab s4-2
Bcsl 033 data and file structures lab s4-2
 
Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3
 
Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2
 
Bcsl 033 data and file structures lab s3-1
Bcsl 033 data and file structures lab s3-1Bcsl 033 data and file structures lab s3-1
Bcsl 033 data and file structures lab s3-1
 
Bcsl 033 data and file structures lab s2-3
Bcsl 033 data and file structures lab s2-3Bcsl 033 data and file structures lab s2-3
Bcsl 033 data and file structures lab s2-3
 
Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2
 
Bcsl 033 data and file structures lab s2-1
Bcsl 033 data and file structures lab s2-1Bcsl 033 data and file structures lab s2-1
Bcsl 033 data and file structures lab s2-1
 
Bcsl 033 data and file structures lab s1-4
Bcsl 033 data and file structures lab s1-4Bcsl 033 data and file structures lab s1-4
Bcsl 033 data and file structures lab s1-4
 
Bcsl 033 data and file structures lab s1-3
Bcsl 033 data and file structures lab s1-3Bcsl 033 data and file structures lab s1-3
Bcsl 033 data and file structures lab s1-3
 
Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2
 
Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1
 
Introduction to Information Security
Introduction to Information SecurityIntroduction to Information Security
Introduction to Information Security
 

7 Deadlocks

  • 1. Deadlocks  System Model  Deadlock Characterization  Methods for Handling Deadlocks Deadlock Prevention  Deadlock Avoidance  Deadlock Detection  Recovery from Deadlock
  • 2. 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 disk drives. – P1 and P2 each hold one disk drive and each needs another one. • Example – semaphores A and B, initialized to 1 P0 P1 wait (A); wait(B) wait (B); wait(A) Loganathan R, CSE, HKBKCE 2
  • 3. 1. System Model • A system consists of a finite number of resources to be distributed among a number of competing processes • Resource types R1, R2, . . ., Rm CPU cycles, memory space, files, I/O devices(Printers,DVDs, • Each resource type Ri may have Wi instances. • Each process utilizes a resource in only the following sequence 1. Request : If the request cannot be granted immediately, then the requesting process must wait until it can acquire the resource 2. Use :The process can operate on the resource 3. Release :The process releases the resource • The request and release of resources are system calls, for example – request () and release() device, – open() and close () file, – allocate () and free () memory system calls • Multithreaded programs are good candidates for deadlock because multiple threads can compete for shared resources Loganathan R, CSE, HKBKCE 3
  • 4. 2. Deadlock Characterization 2.1 Necessary Conditions • Deadlock can arise if four conditions hold simultaneously. 1. Mutual exclusion: only one process at a time can use a resource. 2. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. 3. No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task. 4. 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. Loganathan R, CSE, HKBKCE 4
  • 5. 2. Deadlock Characterization Contd… 2.2 Resource-Allocation Graph • Deadlocks can be described more precisely in terms of a directed graph called a system resource-allocation graph • Consists of 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. • Request edge – directed edge Pi  Rj • Assignment edge – directed edge Rj  Pi • Symbols Used in the graph Pi Rj Process Pi requests instance of Rj Pi Rj Resource Type with 4 instances Pi is holding an instance of Rj Loganathan R, CSE, HKBKCE 5
  • 6. 2. Deadlock Characterization Contd… Example of a Resource Allocation Graph • The sets P, R, and £ : P={P1,P2,P3} R= {R1,R2,R3,R4} £ = {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 • 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 R2 and is waiting for an instance of R3 • Process P3 is holding an instance of R3. • 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 only one instance per resource type, then deadlock. if several instances per resource type, possibility of deadlock. Loganathan R, CSE, HKBKCE 6
  • 7. 2. Deadlock Characterization Contd… Graph With Deadlock Cycles: P1 → R1 → P2 → R3 → P3 → R2 → P1 Graph With A Cycle But No Deadlock P2 → R3 → P3 → R2 → P2 Cycle : P1→R1 →P3 →R2 →P1 Processes P1, P2, and P3 are deadlocked. Process P2 is waiting for resource R3, which is held by P3. P4 May Release R2 So No Deadlock Process P3 is waiting for P1 or P2 to release R2 Process P1 is waiting for process P2 to release resource R1 Loganathan R, CSE, HKBKCE 7
  • 8. 3. Methods for Handling Deadlocks • Three Ways to Deal deadlock problem 1. Use a protocol to prevent or avoid deadlocks, ensuring that the system will never enter a deadlock state. – Deadlock prevention provides a set of methods for ensuring that at least one of the necessary cannot hold – Deadlock avoidance requires the information of which resources a process will request and use during its lifetime, the resources currently available, the resources currently allocated to each process, the future requests and releases of each process then it can decide for each request whether or not the process should wait 2. Allow the system to enter a deadlock state, detect it and recover. 3. Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX. Loganathan R, CSE, HKBKCE 8
  • 9. 4. Deadlock Prevention Restrain the ways request can be made • Mutual Exclusion – not required for sharable resources; must hold for non sharable resources. • 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. • No Preemption – If a process that is holding some resources 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 the 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. Loganathan R, CSE, HKBKCE 9
  • 10. 5. 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. 5.1 Safe State • System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the processes in 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. • That is: – If Pi resource needs are not immediately available, then Pi can wait until all Pj have 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. Loganathan R, CSE, HKBKCE 10
  • 11. 5. Deadlock Avoidance Contd… • 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. • Example A system with 12 magnetic tape drives and three processes: P0,P1 and P2 Process Max Needs Current Needs P0 10 5 P1 4 2 P2 9 2 At time t0 (3 remaining) the system with sequence <P1, P0, P2> is in a safe state At time t0,P2 requests 1 more drive and its allocated ( remaining 2), system can go from a safe state to an unsafe state For deadlock avoidance : 1. Single instance of a resource type use a resource-allocation graph 2. Multiple instances of a resource type. Use the banker’s algorithm Safe, unsafe, and deadlock state spaces Loganathan R, CSE, HKBKCE 11
  • 12. 5. Deadlock Avoidance Contd… 5.2 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. • The request can be granted only if converting the request edge Pi  Rj to an assignment edge Rj —> Pi does not result in the formation of a cycle • Example An unsafe state in a resource-allocation graph Resource-allocation graph for deadlock avoidance Loganathan R, CSE, HKBKCE 12
  • 13. 5. Deadlock Avoidance Contd… 5.3 Banker’s Algorithm • Multiple instances of a resource • 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. • Let n = number of processes, and m = number of resources types. • Data structures – 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]. Loganathan R, CSE, HKBKCE 13
  • 14. 5. Deadlock Avoidance Contd… 5.3.1 Safety Algorithm • To find whether or not a system is in a safe state 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. Loganathan R, CSE, HKBKCE 14
  • 15. 5. Deadlock Avoidance Contd… 5.3.2 Resource-Request Algorithm • Determines if requests can be safely granted • Let Requesti is request vector for process Pi. • If Requesti [j] = k then process Pi wants k instances of resource type Rj then following actions are taken 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 – 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 Loganathan R, CSE, HKBKCE 15
  • 16. 5. Deadlock Avoidance Contd… • Example : 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 • 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. Loganathan R, CSE, HKBKCE 16
  • 17. 5. Deadlock Avoidance Contd… Can request for (1,0,2) by P1 be granted? • Check that Request  Available i.e. (1,0,2)  (3,3,2)  true. Allocation Need Available ABC ABC ABC P0 010 743 230 P1 302 020 P2 301 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? P4 cannot be granted, since the resources are not available • Can request for (0,2,0) by P0 be granted? P0 cannot be granted, since the resulting state is unsafe Loganathan R, CSE, HKBKCE 17
  • 18. 6.Deadlock Detection 6.1 Single Instance of Each Resource Type • All resources have only a single instance • 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. Corresponding wait-for graph Resource-Allocation Graph Loganathan R, CSE, HKBKCE 18
  • 19. 6.Deadlock Detection Contd… 6.2 Several Instances of a Resource Type • Deadlock detection algorithm employs several time-varying data structures – 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. • 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. Loganathan R, CSE, HKBKCE 19
  • 20. 6.Deadlock Detection Contd… 3. Work = Work + Allocationi Finish[i] = true go to step 2. 4. If Finish[i] == false, for some i, 0  i  n, then the system is in deadlock state. Moreover, if Finish[i] == false, then Pi is deadlocked. • Example : 5 processes P0 through P4 – 3 resource types A (7 instances), B (2 instances), and C (6 instances). – Snapshot at time T0: Allocation Request Available ABC ABC ABC P0 0 1 0 000 000 P1 2 0 0 202 P2 3 0 3 000 P3 2 1 1 100 P4 0 0 2 002 • Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i. Loganathan R, CSE, HKBKCE 20
  • 21. 6.Deadlock Detection Contd… • P2 requests an additional instance of type C. Request ABC P0 000 P1 201 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. 6.3 Detection-Algorithm Usage • When to invoke depends on: – How often a deadlock is likely to occur? – How many processes will need to be rolled back? • 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. Loganathan R, CSE, HKBKCE 21
  • 22. 7. Recovery from Deadlock There is 2 methods Process Termination and Resource Preemption 7.1 Process Termination • Abort all deadlocked processes – Expensive- Computed for a long time and results of partial computations • Abort one process at a time until the deadlock cycle is eliminated – After each process is aborted, a deadlock-detection algorithm must be invoked – In which order should we choose to abort? 1. Priority of the process. 2. How long process has computed, and how much longer to completion. 3. Resources the process has used. 4. Resources process needs to complete. 5. How many processes will need to be terminated. 6. Is process interactive or batch? Loganathan R, CSE, HKBKCE 22
  • 23. 7. Recovery from Deadlock 7.2 Resource Preemption • Preempt some resources from processes and give these resources to other processes until the deadlock cycle is broken • Issues 1. Selecting a victim minimize cost - number of resources held and amount of time consumed 2. Rollback – return to some safe state, restart process for that state. 3. Starvation – same process may always be picked as victim, include number of rollback in cost factor. Loganathan R, CSE, HKBKCE 23