SlideShare a Scribd company logo
Part II
Process Management
 Chapter 7: Deadlocks
System Model
qSystem resources are utilized in the following way:
  vRequest: If a process makes a request to use a system
   resource which cannot be granted immediately, then the
   requesting process blocks until it can acquire the
   resource.
  vUse:The process can operate on the resource.
  vRelease: The process releases the resource.
qDeadlock: A set of process is in a deadlock state
 when every process in the set is waiting for an
 event that can only be caused by another process in
 the set.
Deadlock: Necessary Conditions
qFor a deadlock to occur, each of the following four
 conditions must hold.
  vMutual Exclusion: At least one resource must
   be held in a non-sharable way.
  vHold and Wait: A process must be holding a
   resource and waiting for another.
  vNo Preemption: Resource cannot be preempted.
  vCircular Wait: A waits for B, B waits for C, C
   waits for A.
Handling Deadlocks
qDeadlock Prevention and Avoidance: Make
 sure deadlock can never happen.
  vPrevention: Ensure one of the four conditions fails.
  vAvoidance: The OS needs more information so that
   it can determine if the current request can be
   satisfied or delayed.
qDeadlock : Allow a system to enter a deadlock
 situation, detect it, and recover.
qIgnore Deadlock: Pretend deadlocks never
 occur in the system.
Deadlock Prevention: 1/4
         Mutual Exclusion

qBy ensuring that at least one of the four
 conditions cannot hold, we can prevent the
 occurrence of a deadlock.
qMutual Exclusion: Some sharable resources
 must be accessed exclusively (e.g., printer),
 which means we cannot deny the mutual
 exclusion condition.
Deadlock Prevention: 2/4
                 Hold and Wait
qNo process can hold some resources and then
 request for other resources.
qTwo strategies are possible:
  vA process must acquire all resources before it runs.
  vWhen a process requests for resources, it must hold
   none (i.e., returning resources before requesting for
   more).
qResource utilization may be low, since many
 resources will be held and unused for a long time.
qStarvation is possible. A process that needs some
 popular resources my have to wait indefinitely.
Deadlock Prevention: 3/4
             No Preemption
qResources that are being held by the requesting
 process are preempted. There are two strategies:
  vIf a process is holding some resources and requesting for
   some others that are being held by other processes, the
   resources of the requesting process are preempted. The
   preempted resources become available.
  vIf the requested resources are not available:
     ØIf they are being held by processes that are waiting for
       additional resources, these resources are preempted
       and given to the requesting process.
     ØOtherwise, the requesting process waits until the
       requested resources become available. While it is
       waiting, its resources may be preempted.
     ØThis works only if the state of the process and resources
       can be saved and restored easily (e.g., CPU & memory).
Deadlock Prevention: 4/4
            Circular Waiting
qTo break the circular waiting condition, we can
 order all resource types (e.g., tapes, printers).
qA process can only request resources higher than the
 resource types it holds.
qSuppose the ordering of tapes, disks, and printers
 are 1, 4, and 8. If a process holds a disk (4), it can
 only ask a printer (8) and cannot request a tape (1).
qA process must release some lower order resources
 to request a lower order resource. To get tapes (1), a
 process must release its disk (4).
qIn this way, no deadlock is possible. Why?
Deadlock Avoidance: 1/5
qEach process provides the maximum number of
 resources of each type it needs.
qWith these information, there are algorithms that
 can ensure the system will never enter a deadlock
 state. This is deadlock avoidance.
qA sequence of processes <P1, P2, …, Pn> is a safe
 sequence if for each process Pi in the sequence, its
 resource requests can be satisfied by the remaining
 resources and the sum of all resources that are
 being held by P1, P2, …, Pi-1. This means we can
 suspend Pi and run P1, P2, …, Pi-1 until they
 complete. Then, Pi will have all resources to run.
Deadlock Avoidance: 2/5
q A state is safe if the system can
  allocate resources to each
  process (up to its maximum, of
  course) in some order and still       deadlock
  avoid a deadlock.
q In other word, a state is safe if
  there is a safe sequence.             unsafe
  Otherwise, if no safe sequence
  exists, the system state is unsafe.
q An unsafe state is not
  necessarily a deadlock state.
  On the other hand, a deadlock
                                         safe
  state is an unsafe state.
Deadlock Avoidance: 3/5
qA system has 12 tapes and three processes A, B, C.
 At time t0, we have:
            Max needs   Current holding   Will need
        A      10             5              5
        B       4             2              2
        C       9             2              7

qThen, <B, A, C> is a safe sequence (safe state).
qThe system has 12-(5+2+2)=3 free tapes.
qSince B needs 2 tapes, it can take 2, run, and
 return 4. Then, the system has (3-2)+4=5 tapes. A
 now can take all 5 tapes and run. Finally, A
 returns 10 tapes for C to take 7 of them.
Deadlock Avoidance: 4/5
qA system has 12 tapes and three processes A, B, C. At
 time t1, C has one more tape:
             Max needs   Current holding   Will need
         A      10             5              5
         B       4             2              2
         C       9             3              6
qThe system has 12-(5+2+3)=2 free tapes.
qAt this point, only B can take these 2 and run. It
 returns 4, making 4 free tapes available.
qBut, none of A and C can run, and a deadlock occurs.
qThe problem is due to granting C one more tape.
Deadlock Avoidance: 5/5
qA deadlock avoidance algorithm ensures that
 the system is always in a safe state. Therefore,
 no deadlock can occur.
qResource requests are granted only if in doing
 so the system is still in a safe state.
qConsequently, resource utilization may be
 lower than those systems without using a
 deadlock avoidance algorithm.
Banker’s Algorithm: 1/2
qThe system has m resource types and n processes.
qEach process must declare its maximum needs.
qThe following arrays are used:
  vAvailable[1..m]: one entry for each resource. Available[i]=k
   means resource type i has k units available.
  vMax[1..n,1..m]: maximum demand of each process.
   Max[i,j]=k means process i needs k units of resource j.
  vAllocation[1..n,1..m]: resources allocated to each process.
   Allocation[i,j]=k means process i is currently allocated k
   units of resource j.
  vNeed[1..n,1..m]: the remaining resource need of each
   process. Need[i,j]=k means process i needs k more units of
   resource j.
Banker’s Algorithm: 2/2
qWe will use A[i,*] to indicate the i-th row of
 matrix A.
qGiven two arrays A[1..m] and B[1..m], A ≤ B if A[i]
 ≤ B[i] for all i. Given two matrices A[1..n,1..m]
 and B[1..n,1..m], A[i,*] ≤B[i,*] if A[i,j] ≤B[i,j] for
 all j.
qWhen a resource request is made by process i, this
 algorithm calls the Resource-Request algorithm to
 determine if the request can be granted. The
 Resource-Request algorithm calls the Safety
 Algorithm to determine if a state is safe.
Safety Algorithm
1. Let Work[1..m] and Finish[1..n] be two working
   arrays.
2. Work := Available and Finish[i]=FALSE for all i
3. Find an i such that both
   v Finish[i] = FALSE    // process i is not yet done
   v Need[i,*] ≤ Work     // its need can be satisfied
   If no such i exists, go to Step 5
4. Work = Work + Allocation[i,*] // run it and reclaim
   Finish[i] = TRUE        // process i completes
   go to Step 3
5. If Finish[i] = TRUE for all i, the system is in a
   safe state.
Resource-Request Algorithm
1.   Let Request[1..n,1..m] be the request matrix. Request[i,j]=k
     means process i requests k units of resource j.
2.   If Request[i,*]≤Need[i,*], go to Step 3. Otherwise, it is an
     error.
3.   If Request[i,*]≤Available, go to Step 4. Otherwise, process i
     waits.
4.   Do the following:
        Available = Available – Request[i,*]
        Allocation[i,*] = Allocation[i,*]+Request[i,*]
        Need[i,*] = Need[i,*] – Request[i,*]
     If the result is a safe state (Safety Algorithm), the request is
     granted. Otherwise, process i waits and the
     resource-allocation tables are restored back to the original.
Example: 1/4
q Consider a system of 5 processes A, B, C, D and E, and 3
  resource types (X=10, Y=5, Z=7). At time t0, we have
          Allocation      Max Need=Max-Alloc Available
          X Y       Z   X Y Z   X   Y   Z    X Y     Z
      A   0   1    0    7   5   3   7   4    3   3   3   2
      B   2   0    0    3   2   2   1   2    2
      C   3   0    2    9   0   2   6   0    0
      D   2   1    1    2   2   2   0   1    1
      E   0   0    2    4   3   3   4   3    1
q A safe sequence is <B,D,E,C,A>. Since B’s [1,2,2]≤ Avail’s
  [3,3,2], B runs. Then, Avail=[2,0,0]+[3,3,2]=[5,3,2]. D runs
  next. After this, Avail=[5,3,2]+[2,1,1]=[7,4,3]. E runs next.
q Avail=[7,4,3]+[0,0,2]=[7,4,5]. Since C’s [6,0,0]≤Avail=[7,4,5], C
  runs. After this, Avail=[7,4,5]+[3,0,2]=[10,4,7] and A runs.
q There are other safe sequences: <D,E,B,A,C>, <D,B,A,E,C>, …
Example: 2/4
   q Now suppose process B asks for 1 X and 2 Zs. More
     precisely, RequestB = [1,0,2]. Is the system still in a safe state
     if this request is granted?
   q Since RequestB = [1,0,2] ≤ Available = [3,3,2], this request
     may be granted as long as the system is safe.
   q If this request is actually granted, we have the following:

             Allocation       Max Need=Max-Alloc Available
             X Y       Z    X Y Z   X   Y   Z    X Y     Z
        A    0    1   0     7   5    3    7   4      3     2    3   0
        B    3    0   2     3   2    2    0   2      0
        C    3    0   2     9   0    2    6   0      0
        D    2    1   1     2   2    2    0   1      1
        E    0    0   2     4   3    3    4   3      1

[3,0,2]=[2,0,0]+[1,0,2]    [0,2,0]=[1,2,2]-[1,0,2]       [2,3,0]=[3,3,2]-[1,0,2]
Example: 3/4
       Allocation      Max Need=Max-Alloc Available
       X Y       Z   X Y Z   X   Y   Z    X Y     Z
   A   0   1    0    7   5   3   7   4   3   2   3   0
   B   3   0    2    3   2   2   0   2   0
   C   3   0    2    9   0   2   6   0   0
   D   2   1    1    2   2   2   0   1   1
   E   0   0    2    4   3   3   4   3   1

qIs the system in a safe state after this allocation?
qYes, because the safety algorithm will provide a
 safe sequence <B,D,E,A,C>. Verify it by
 yourself.
qTherefore, B’s request of [1,0,2] can safely be
 made.
Example: 4/4
       Allocation      Max Need=Max-Alloc Available
       X Y       Z   X Y Z   X   Y   Z    X Y     Z
   A   0   1    0    7   5   3   7   4   3   2   3   0
   B   3   0    2    3   2   2   0   2   0
   C   3   0    2    9   0   2   6   0   0
  D    2   1    1    2   2   2   0   1   1
   E   0   0    2    4   3   3   4   3   1

q After this allocation, E’s request RequestE=[3,3,0]
  cannot be granted since RequestE=[3,3,0] ≤[2,3,0] is
  false.
q A’s request RequestA=[0,2,0] cannot be granted because
  the system will be unsafe.
q If RequestA=[0,2,0] is granted, Available=[2,1,0].
q None of the five processes can finish and the system is
  unsafe.
Deadlock Detection
qIf a system does not use a deadlock prevention
 or a deadlock avoidance algorithm, then a
 deadlock situation may occur. Thus, we need
  vAn algorithm that can examine the system
    state to determine if a deadlock has
    occurred. This is a deadlock detection
    algorithm.
  vAn algorithm that can help recover from a
    deadlock. This is a recovery algorithm.
qA deadlock detection algorithm does not have
 to know the maximum need Max and the
 current need Need. It uses only Available,
 Allocation and Request.
Deadlock Detection Algorithm
1.  Let Work[1..m] and Finish[1..n] be two working arrays.
2.  Work := Available and Finish[i]=FALSE for all i
3.  Find an i such that both
   v Finish[i] = FALSE // process i is not yet done
   v Request[i,*] ≤ Work // its request can be satisfied
    If no such i exists, go to Step 5
4. Work = Work + Allocation[i,*] // run it and reclaim
   Finish[i] = TRUE        // process i completes
   go to Step 3
5. If Finish[i] = TRUE for all i, the system is in a safe state. If
    Finish[i] = FALSE, then process Pi is deadlocked.

     Use Request here rather than Need in the safety algorithm
Example: 1/2
             Allocation      Request      Available
             X Y Z         X Y Z         X Y Z
        A    0   1    0    0    0    0   0   0     0
        B    2   0    0    2    0    2
        C    3   0    3    0    0    0
        D    2   1    1    1    0    0
        E    0   0    2    0    0    2
q Suppose maximum available resource is [7,2,6] and the
  current state of resource allocation is shown above.
q Is the system deadlocked? No. We can run A first, making
  Available=[0,1,0].
q Then, we run C, making Available=[3,1,3]. This is followed
  by D, making Available=[5,2,4], and followed by B and E.
Example: 2/2
         Allocation     Request      Available
         X Y Z        X Y Z         X Y Z
     A   0   1    0   0    0    0   0   0     0
     B   2   0    0   2    0    2
     C   3   0    3   0    0    1
     D   2   1    1   1    0    0
     E   0   0    2   0    0    2

qSuppose C requests for one more resource Z.
qNow, A can run, making Available=[0,1,0].
qHowever, none of B, C, D and E can run.
 Therefore, B, C, D and E are deadlocked!
The Use of a Detection Algorithm
qFrequency
  vIf deadlocks occur frequently, then the
   detection algorithm should be invoked
   frequently.
  vOnce per hour or whenever CPU utilization
   becomes low (i.e., below 40%). Low CPU
   utilization means more processes are
   waiting.
How to Recover: 1/3
qWhen a detection algorithm determines a
 deadlock has occurred, the algorithm may
 inform the system administrator to deal with it.
 Of, allow the system to recover from a deadlock.
qThere are two options.
  vProcess Termination
  vResource Preemption
qThese two options are not mutually exclusive.
Recovery: Process Termination: 2/3
 qAbort all deadlocked processes
 qAbort one process at a time until the deadlock cycle
  is eliminated
 qProblems:
   vAborting a process may not be easy. What if a
     process is updating or printing a large file? The
     system must find some way to maintain the state
     of the file and printer before they can be reused.
   vThe termination may be determined by the
     priority/importance of a process.
Recovery: Resource Preemption: 3/3

qSelecting a victim: which resources and which
 processes are to be preempted?
qRollback: If we preempt a resource from a
 process, what should be done with that process?
  vTotal Rollback: abort the process and restart it
  vPartial Rollback: rollback the process only as far as
   necessary to break the deadlock.
qStarvation: We cannot always pick the same
 process as a victim. Some limit must be set.

More Related Content

What's hot

Operating System: Deadlock
Operating System: DeadlockOperating System: Deadlock
Operating System: Deadlock
InteX Research Lab
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
Ra'Fat Al-Msie'deen
 
Dead Lock in operating system
Dead Lock in operating systemDead Lock in operating system
Dead Lock in operating system
Ali Haider
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Ravindra Raju Kolahalam
 
Concurrency Control in Database Management System
Concurrency Control in Database Management SystemConcurrency Control in Database Management System
Concurrency Control in Database Management System
Janki Shah
 
Deadlock Presentation
Deadlock PresentationDeadlock Presentation
Deadlock Presentation
salmancreation
 
Methods for handling deadlock
Methods for handling deadlockMethods for handling deadlock
Methods for handling deadlock
sangrampatil81
 
Deadlock in Distributed Systems
Deadlock in Distributed SystemsDeadlock in Distributed Systems
Deadlock in Distributed Systems
Pritom Saha Akash
 
Bankers algorithm
Bankers algorithmBankers algorithm
Bankers algorithm
AAQIB PARREY
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
LECO9
 
7 Deadlocks
7 Deadlocks7 Deadlocks
7 Deadlocks
Dr. Loganathan R
 
Chapter 12 transactions and concurrency control
Chapter 12 transactions and concurrency controlChapter 12 transactions and concurrency control
Chapter 12 transactions and concurrency controlAbDul ThaYyal
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-Systems
Venkata Sreeram
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.
Ravi Kumar Patel
 
Deadlock Avoidance - OS
Deadlock Avoidance - OSDeadlock Avoidance - OS
Deadlock Avoidance - OS
MsAnita2
 
Dead Lock In Operating Systems
Dead Lock In Operating SystemsDead Lock In Operating Systems
Dead Lock In Operating Systems
totallooser
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 

What's hot (20)

Operating System: Deadlock
Operating System: DeadlockOperating System: Deadlock
Operating System: Deadlock
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
 
Dead Lock in operating system
Dead Lock in operating systemDead Lock in operating system
Dead Lock in operating system
 
Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]
 
Concurrency Control in Database Management System
Concurrency Control in Database Management SystemConcurrency Control in Database Management System
Concurrency Control in Database Management System
 
Deadlock Presentation
Deadlock PresentationDeadlock Presentation
Deadlock Presentation
 
Methods for handling deadlock
Methods for handling deadlockMethods for handling deadlock
Methods for handling deadlock
 
Deadlock in Distributed Systems
Deadlock in Distributed SystemsDeadlock in Distributed Systems
Deadlock in Distributed Systems
 
Bankers algorithm
Bankers algorithmBankers algorithm
Bankers algorithm
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
 
Deadlock
DeadlockDeadlock
Deadlock
 
7 Deadlocks
7 Deadlocks7 Deadlocks
7 Deadlocks
 
Chapter 12 transactions and concurrency control
Chapter 12 transactions and concurrency controlChapter 12 transactions and concurrency control
Chapter 12 transactions and concurrency control
 
OS - Deadlock
OS - DeadlockOS - Deadlock
OS - Deadlock
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-Systems
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.
 
Deadlock Avoidance - OS
Deadlock Avoidance - OSDeadlock Avoidance - OS
Deadlock Avoidance - OS
 
Dead Lock In Operating Systems
Dead Lock In Operating SystemsDead Lock In Operating Systems
Dead Lock In Operating Systems
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
Deadlock ppt
Deadlock ppt Deadlock ppt
Deadlock ppt
 

Viewers also liked

Deadlock
DeadlockDeadlock
Deadlock
Abhinaw Rai
 
Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock GalvinSonali Chauhan
 
O ssvv62015
O ssvv62015O ssvv62015
Filepermissions in linux
Filepermissions in linuxFilepermissions in linux
Filepermissions in linux
Subashini Pandiarajan
 
Handling Page Fault
Handling Page FaultHandling Page Fault
Handling Page Fault
guestb7dc8e
 
File permissions
File permissionsFile permissions
File permissions
Varnnit Jain
 
linux file system
linux file systemlinux file system
linux file system
AryaTadbir Network Designers
 
37 segmentation
37 segmentation37 segmentation
37 segmentationmyrajendra
 
4. linux file systems
4. linux file systems4. linux file systems
4. linux file systems
Marian Marinov
 
Page Replacement
Page ReplacementPage Replacement
Page Replacement
chandinisanz
 
file system in operating system
file system in operating systemfile system in operating system
file system in operating systemtittuajay
 
File permission in linux
File permission in linuxFile permission in linux
File permission in linux
Prakash Poudel
 
Producer consumer
Producer consumerProducer consumer
Producer consumer
Mohd Tousif
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
Midhun Sankar
 
File System Hierarchy
File System HierarchyFile System Hierarchy
File System Hierarchy
sritolia
 
System calls
System callsSystem calls
System calls
Bernard Senam
 
Virtual Memory and Paging
Virtual Memory and PagingVirtual Memory and Paging
Virtual Memory and Paging
Emery Berger
 

Viewers also liked (20)

Deadlock
DeadlockDeadlock
Deadlock
 
Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock Galvin
 
O ssvv62015
O ssvv62015O ssvv62015
O ssvv62015
 
Filepermissions in linux
Filepermissions in linuxFilepermissions in linux
Filepermissions in linux
 
Handling Page Fault
Handling Page FaultHandling Page Fault
Handling Page Fault
 
File permissions
File permissionsFile permissions
File permissions
 
linux file system
linux file systemlinux file system
linux file system
 
Linux file system
Linux file systemLinux file system
Linux file system
 
37 segmentation
37 segmentation37 segmentation
37 segmentation
 
4. linux file systems
4. linux file systems4. linux file systems
4. linux file systems
 
Page Replacement
Page ReplacementPage Replacement
Page Replacement
 
file system in operating system
file system in operating systemfile system in operating system
file system in operating system
 
File permission in linux
File permission in linuxFile permission in linux
File permission in linux
 
Producer consumer
Producer consumerProducer consumer
Producer consumer
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
 
File system
File systemFile system
File system
 
File System Hierarchy
File System HierarchyFile System Hierarchy
File System Hierarchy
 
Kernel (computing)
Kernel (computing)Kernel (computing)
Kernel (computing)
 
System calls
System callsSystem calls
System calls
 
Virtual Memory and Paging
Virtual Memory and PagingVirtual Memory and Paging
Virtual Memory and Paging
 

Similar to Deadlock

Gp1242 007 oer ppt
Gp1242 007 oer pptGp1242 007 oer ppt
Gp1242 007 oer ppt
Nivedita Kasturi
 
Ch 4 deadlock
Ch 4 deadlockCh 4 deadlock
Ch 4 deadlock
madhuributani
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
sheraz7288
 
CH07.pdf
CH07.pdfCH07.pdf
CH07.pdf
ImranKhan880955
 
Deadlocks Part- III.pdf
Deadlocks Part- III.pdfDeadlocks Part- III.pdf
Deadlocks Part- III.pdf
Harika Pudugosula
 
Section07-Deadlocks.pdf
Section07-Deadlocks.pdfSection07-Deadlocks.pdf
Section07-Deadlocks.pdf
MogilicharlaPavanKal
 
Os case study word
Os case study wordOs case study word
Os case study word
Dhol Yash
 
Deadlocks
Deadlocks Deadlocks
Deadlocks
Shijin Raj P
 
9 deadlock
9 deadlock9 deadlock
9 deadlock
GRajendra
 
Section07-Deadlocks (1).ppt
Section07-Deadlocks (1).pptSection07-Deadlocks (1).ppt
Section07-Deadlocks (1).ppt
amadayshwan
 
Section07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.pptSection07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.ppt
jbri1395
 
Deadlock
DeadlockDeadlock
Deadlock
Mahershi ACT
 
Mca ii os u-3 dead lock & io systems
Mca  ii  os u-3 dead lock & io systemsMca  ii  os u-3 dead lock & io systems
Mca ii os u-3 dead lock & io systems
Rai University
 
Deadlock
DeadlockDeadlock
Deadlock
Mayuri Verma
 
Dead Lock
Dead LockDead Lock
Dead Lock
Ramasubbu .P
 
Deadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networkingDeadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networking
TamannaSharma70
 
Bankers
BankersBankers
OS-Part-06.pdf
OS-Part-06.pdfOS-Part-06.pdf
OS-Part-06.pdf
NguyenTienDungK17HL
 

Similar to Deadlock (20)

Gp1242 007 oer ppt
Gp1242 007 oer pptGp1242 007 oer ppt
Gp1242 007 oer ppt
 
Ch 4 deadlock
Ch 4 deadlockCh 4 deadlock
Ch 4 deadlock
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
 
Deadlocks
 Deadlocks Deadlocks
Deadlocks
 
CH07.pdf
CH07.pdfCH07.pdf
CH07.pdf
 
Deadlocks Part- III.pdf
Deadlocks Part- III.pdfDeadlocks Part- III.pdf
Deadlocks Part- III.pdf
 
Section07-Deadlocks.pdf
Section07-Deadlocks.pdfSection07-Deadlocks.pdf
Section07-Deadlocks.pdf
 
Os case study word
Os case study wordOs case study word
Os case study word
 
Deadlocks
Deadlocks Deadlocks
Deadlocks
 
9 deadlock
9 deadlock9 deadlock
9 deadlock
 
Section07-Deadlocks (1).ppt
Section07-Deadlocks (1).pptSection07-Deadlocks (1).ppt
Section07-Deadlocks (1).ppt
 
Section07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.pptSection07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.ppt
 
Deadlock
DeadlockDeadlock
Deadlock
 
Mca ii os u-3 dead lock & io systems
Mca  ii  os u-3 dead lock & io systemsMca  ii  os u-3 dead lock & io systems
Mca ii os u-3 dead lock & io systems
 
Deadlock
DeadlockDeadlock
Deadlock
 
Dead Lock
Dead LockDead Lock
Dead Lock
 
Deadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networkingDeadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networking
 
Bankers
BankersBankers
Bankers
 
OS-Part-06.pdf
OS-Part-06.pdfOS-Part-06.pdf
OS-Part-06.pdf
 
Ice
IceIce
Ice
 

More from Mohd Arif

Bootp and dhcp
Bootp and dhcpBootp and dhcp
Bootp and dhcpMohd Arif
 
Arp and rarp
Arp and rarpArp and rarp
Arp and rarpMohd Arif
 
User datagram protocol
User datagram protocolUser datagram protocol
User datagram protocolMohd Arif
 
Project identification
Project identificationProject identification
Project identificationMohd Arif
 
Project evalaution techniques
Project evalaution techniquesProject evalaution techniques
Project evalaution techniquesMohd Arif
 
Presentation
PresentationPresentation
PresentationMohd Arif
 
Pointers in c
Pointers in cPointers in c
Pointers in cMohd Arif
 
Peer to-peer
Peer to-peerPeer to-peer
Peer to-peerMohd Arif
 
Overview of current communications systems
Overview of current communications systemsOverview of current communications systems
Overview of current communications systemsMohd Arif
 
Overall 23 11_2007_hdp
Overall 23 11_2007_hdpOverall 23 11_2007_hdp
Overall 23 11_2007_hdpMohd Arif
 
Objectives of budgeting
Objectives of budgetingObjectives of budgeting
Objectives of budgetingMohd Arif
 
Network management
Network managementNetwork management
Network managementMohd Arif
 
Networing basics
Networing basicsNetworing basics
Networing basicsMohd Arif
 
Iris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platformIris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platformMohd Arif
 
Ip sec and ssl
Ip sec and  sslIp sec and  ssl
Ip sec and sslMohd Arif
 
Ip security in i psec
Ip security in i psecIp security in i psec
Ip security in i psecMohd Arif
 
Intro to comp. hardware
Intro to comp. hardwareIntro to comp. hardware
Intro to comp. hardwareMohd Arif
 

More from Mohd Arif (20)

Bootp and dhcp
Bootp and dhcpBootp and dhcp
Bootp and dhcp
 
Arp and rarp
Arp and rarpArp and rarp
Arp and rarp
 
User datagram protocol
User datagram protocolUser datagram protocol
User datagram protocol
 
Project identification
Project identificationProject identification
Project identification
 
Project evalaution techniques
Project evalaution techniquesProject evalaution techniques
Project evalaution techniques
 
Presentation
PresentationPresentation
Presentation
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Peer to-peer
Peer to-peerPeer to-peer
Peer to-peer
 
Overview of current communications systems
Overview of current communications systemsOverview of current communications systems
Overview of current communications systems
 
Overall 23 11_2007_hdp
Overall 23 11_2007_hdpOverall 23 11_2007_hdp
Overall 23 11_2007_hdp
 
Objectives of budgeting
Objectives of budgetingObjectives of budgeting
Objectives of budgeting
 
Network management
Network managementNetwork management
Network management
 
Networing basics
Networing basicsNetworing basics
Networing basics
 
Loaders
LoadersLoaders
Loaders
 
Lists
ListsLists
Lists
 
Iris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platformIris ngx next generation ip based switching platform
Iris ngx next generation ip based switching platform
 
Ip sec and ssl
Ip sec and  sslIp sec and  ssl
Ip sec and ssl
 
Ip security in i psec
Ip security in i psecIp security in i psec
Ip security in i psec
 
Intro to comp. hardware
Intro to comp. hardwareIntro to comp. hardware
Intro to comp. hardware
 
Heap sort
Heap sortHeap sort
Heap sort
 

Recently uploaded

Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 

Recently uploaded (20)

Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 

Deadlock

  • 1. Part II Process Management Chapter 7: Deadlocks
  • 2. System Model qSystem resources are utilized in the following way: vRequest: If a process makes a request to use a system resource which cannot be granted immediately, then the requesting process blocks until it can acquire the resource. vUse:The process can operate on the resource. vRelease: The process releases the resource. qDeadlock: A set of process is in a deadlock state when every process in the set is waiting for an event that can only be caused by another process in the set.
  • 3. Deadlock: Necessary Conditions qFor a deadlock to occur, each of the following four conditions must hold. vMutual Exclusion: At least one resource must be held in a non-sharable way. vHold and Wait: A process must be holding a resource and waiting for another. vNo Preemption: Resource cannot be preempted. vCircular Wait: A waits for B, B waits for C, C waits for A.
  • 4. Handling Deadlocks qDeadlock Prevention and Avoidance: Make sure deadlock can never happen. vPrevention: Ensure one of the four conditions fails. vAvoidance: The OS needs more information so that it can determine if the current request can be satisfied or delayed. qDeadlock : Allow a system to enter a deadlock situation, detect it, and recover. qIgnore Deadlock: Pretend deadlocks never occur in the system.
  • 5. Deadlock Prevention: 1/4 Mutual Exclusion qBy ensuring that at least one of the four conditions cannot hold, we can prevent the occurrence of a deadlock. qMutual Exclusion: Some sharable resources must be accessed exclusively (e.g., printer), which means we cannot deny the mutual exclusion condition.
  • 6. Deadlock Prevention: 2/4 Hold and Wait qNo process can hold some resources and then request for other resources. qTwo strategies are possible: vA process must acquire all resources before it runs. vWhen a process requests for resources, it must hold none (i.e., returning resources before requesting for more). qResource utilization may be low, since many resources will be held and unused for a long time. qStarvation is possible. A process that needs some popular resources my have to wait indefinitely.
  • 7. Deadlock Prevention: 3/4 No Preemption qResources that are being held by the requesting process are preempted. There are two strategies: vIf a process is holding some resources and requesting for some others that are being held by other processes, the resources of the requesting process are preempted. The preempted resources become available. vIf the requested resources are not available: ØIf they are being held by processes that are waiting for additional resources, these resources are preempted and given to the requesting process. ØOtherwise, the requesting process waits until the requested resources become available. While it is waiting, its resources may be preempted. ØThis works only if the state of the process and resources can be saved and restored easily (e.g., CPU & memory).
  • 8. Deadlock Prevention: 4/4 Circular Waiting qTo break the circular waiting condition, we can order all resource types (e.g., tapes, printers). qA process can only request resources higher than the resource types it holds. qSuppose the ordering of tapes, disks, and printers are 1, 4, and 8. If a process holds a disk (4), it can only ask a printer (8) and cannot request a tape (1). qA process must release some lower order resources to request a lower order resource. To get tapes (1), a process must release its disk (4). qIn this way, no deadlock is possible. Why?
  • 9. Deadlock Avoidance: 1/5 qEach process provides the maximum number of resources of each type it needs. qWith these information, there are algorithms that can ensure the system will never enter a deadlock state. This is deadlock avoidance. qA sequence of processes <P1, P2, …, Pn> is a safe sequence if for each process Pi in the sequence, its resource requests can be satisfied by the remaining resources and the sum of all resources that are being held by P1, P2, …, Pi-1. This means we can suspend Pi and run P1, P2, …, Pi-1 until they complete. Then, Pi will have all resources to run.
  • 10. Deadlock Avoidance: 2/5 q A state is safe if the system can allocate resources to each process (up to its maximum, of course) in some order and still deadlock avoid a deadlock. q In other word, a state is safe if there is a safe sequence. unsafe Otherwise, if no safe sequence exists, the system state is unsafe. q An unsafe state is not necessarily a deadlock state. On the other hand, a deadlock safe state is an unsafe state.
  • 11. Deadlock Avoidance: 3/5 qA system has 12 tapes and three processes A, B, C. At time t0, we have: Max needs Current holding Will need A 10 5 5 B 4 2 2 C 9 2 7 qThen, <B, A, C> is a safe sequence (safe state). qThe system has 12-(5+2+2)=3 free tapes. qSince B needs 2 tapes, it can take 2, run, and return 4. Then, the system has (3-2)+4=5 tapes. A now can take all 5 tapes and run. Finally, A returns 10 tapes for C to take 7 of them.
  • 12. Deadlock Avoidance: 4/5 qA system has 12 tapes and three processes A, B, C. At time t1, C has one more tape: Max needs Current holding Will need A 10 5 5 B 4 2 2 C 9 3 6 qThe system has 12-(5+2+3)=2 free tapes. qAt this point, only B can take these 2 and run. It returns 4, making 4 free tapes available. qBut, none of A and C can run, and a deadlock occurs. qThe problem is due to granting C one more tape.
  • 13. Deadlock Avoidance: 5/5 qA deadlock avoidance algorithm ensures that the system is always in a safe state. Therefore, no deadlock can occur. qResource requests are granted only if in doing so the system is still in a safe state. qConsequently, resource utilization may be lower than those systems without using a deadlock avoidance algorithm.
  • 14. Banker’s Algorithm: 1/2 qThe system has m resource types and n processes. qEach process must declare its maximum needs. qThe following arrays are used: vAvailable[1..m]: one entry for each resource. Available[i]=k means resource type i has k units available. vMax[1..n,1..m]: maximum demand of each process. Max[i,j]=k means process i needs k units of resource j. vAllocation[1..n,1..m]: resources allocated to each process. Allocation[i,j]=k means process i is currently allocated k units of resource j. vNeed[1..n,1..m]: the remaining resource need of each process. Need[i,j]=k means process i needs k more units of resource j.
  • 15. Banker’s Algorithm: 2/2 qWe will use A[i,*] to indicate the i-th row of matrix A. qGiven two arrays A[1..m] and B[1..m], A ≤ B if A[i] ≤ B[i] for all i. Given two matrices A[1..n,1..m] and B[1..n,1..m], A[i,*] ≤B[i,*] if A[i,j] ≤B[i,j] for all j. qWhen a resource request is made by process i, this algorithm calls the Resource-Request algorithm to determine if the request can be granted. The Resource-Request algorithm calls the Safety Algorithm to determine if a state is safe.
  • 16. Safety Algorithm 1. Let Work[1..m] and Finish[1..n] be two working arrays. 2. Work := Available and Finish[i]=FALSE for all i 3. Find an i such that both v Finish[i] = FALSE // process i is not yet done v Need[i,*] ≤ Work // its need can be satisfied If no such i exists, go to Step 5 4. Work = Work + Allocation[i,*] // run it and reclaim Finish[i] = TRUE // process i completes go to Step 3 5. If Finish[i] = TRUE for all i, the system is in a safe state.
  • 17. Resource-Request Algorithm 1. Let Request[1..n,1..m] be the request matrix. Request[i,j]=k means process i requests k units of resource j. 2. If Request[i,*]≤Need[i,*], go to Step 3. Otherwise, it is an error. 3. If Request[i,*]≤Available, go to Step 4. Otherwise, process i waits. 4. Do the following: Available = Available – Request[i,*] Allocation[i,*] = Allocation[i,*]+Request[i,*] Need[i,*] = Need[i,*] – Request[i,*] If the result is a safe state (Safety Algorithm), the request is granted. Otherwise, process i waits and the resource-allocation tables are restored back to the original.
  • 18. Example: 1/4 q Consider a system of 5 processes A, B, C, D and E, and 3 resource types (X=10, Y=5, Z=7). At time t0, we have Allocation Max Need=Max-Alloc Available X Y Z X Y Z X Y Z X Y Z A 0 1 0 7 5 3 7 4 3 3 3 2 B 2 0 0 3 2 2 1 2 2 C 3 0 2 9 0 2 6 0 0 D 2 1 1 2 2 2 0 1 1 E 0 0 2 4 3 3 4 3 1 q A safe sequence is <B,D,E,C,A>. Since B’s [1,2,2]≤ Avail’s [3,3,2], B runs. Then, Avail=[2,0,0]+[3,3,2]=[5,3,2]. D runs next. After this, Avail=[5,3,2]+[2,1,1]=[7,4,3]. E runs next. q Avail=[7,4,3]+[0,0,2]=[7,4,5]. Since C’s [6,0,0]≤Avail=[7,4,5], C runs. After this, Avail=[7,4,5]+[3,0,2]=[10,4,7] and A runs. q There are other safe sequences: <D,E,B,A,C>, <D,B,A,E,C>, …
  • 19. Example: 2/4 q Now suppose process B asks for 1 X and 2 Zs. More precisely, RequestB = [1,0,2]. Is the system still in a safe state if this request is granted? q Since RequestB = [1,0,2] ≤ Available = [3,3,2], this request may be granted as long as the system is safe. q If this request is actually granted, we have the following: Allocation Max Need=Max-Alloc Available X Y Z X Y Z X Y Z X Y Z A 0 1 0 7 5 3 7 4 3 2 3 0 B 3 0 2 3 2 2 0 2 0 C 3 0 2 9 0 2 6 0 0 D 2 1 1 2 2 2 0 1 1 E 0 0 2 4 3 3 4 3 1 [3,0,2]=[2,0,0]+[1,0,2] [0,2,0]=[1,2,2]-[1,0,2] [2,3,0]=[3,3,2]-[1,0,2]
  • 20. Example: 3/4 Allocation Max Need=Max-Alloc Available X Y Z X Y Z X Y Z X Y Z A 0 1 0 7 5 3 7 4 3 2 3 0 B 3 0 2 3 2 2 0 2 0 C 3 0 2 9 0 2 6 0 0 D 2 1 1 2 2 2 0 1 1 E 0 0 2 4 3 3 4 3 1 qIs the system in a safe state after this allocation? qYes, because the safety algorithm will provide a safe sequence <B,D,E,A,C>. Verify it by yourself. qTherefore, B’s request of [1,0,2] can safely be made.
  • 21. Example: 4/4 Allocation Max Need=Max-Alloc Available X Y Z X Y Z X Y Z X Y Z A 0 1 0 7 5 3 7 4 3 2 3 0 B 3 0 2 3 2 2 0 2 0 C 3 0 2 9 0 2 6 0 0 D 2 1 1 2 2 2 0 1 1 E 0 0 2 4 3 3 4 3 1 q After this allocation, E’s request RequestE=[3,3,0] cannot be granted since RequestE=[3,3,0] ≤[2,3,0] is false. q A’s request RequestA=[0,2,0] cannot be granted because the system will be unsafe. q If RequestA=[0,2,0] is granted, Available=[2,1,0]. q None of the five processes can finish and the system is unsafe.
  • 22. Deadlock Detection qIf a system does not use a deadlock prevention or a deadlock avoidance algorithm, then a deadlock situation may occur. Thus, we need vAn algorithm that can examine the system state to determine if a deadlock has occurred. This is a deadlock detection algorithm. vAn algorithm that can help recover from a deadlock. This is a recovery algorithm. qA deadlock detection algorithm does not have to know the maximum need Max and the current need Need. It uses only Available, Allocation and Request.
  • 23. Deadlock Detection Algorithm 1. Let Work[1..m] and Finish[1..n] be two working arrays. 2. Work := Available and Finish[i]=FALSE for all i 3. Find an i such that both v Finish[i] = FALSE // process i is not yet done v Request[i,*] ≤ Work // its request can be satisfied If no such i exists, go to Step 5 4. Work = Work + Allocation[i,*] // run it and reclaim Finish[i] = TRUE // process i completes go to Step 3 5. If Finish[i] = TRUE for all i, the system is in a safe state. If Finish[i] = FALSE, then process Pi is deadlocked. Use Request here rather than Need in the safety algorithm
  • 24. Example: 1/2 Allocation Request Available X Y Z X Y Z X Y Z A 0 1 0 0 0 0 0 0 0 B 2 0 0 2 0 2 C 3 0 3 0 0 0 D 2 1 1 1 0 0 E 0 0 2 0 0 2 q Suppose maximum available resource is [7,2,6] and the current state of resource allocation is shown above. q Is the system deadlocked? No. We can run A first, making Available=[0,1,0]. q Then, we run C, making Available=[3,1,3]. This is followed by D, making Available=[5,2,4], and followed by B and E.
  • 25. Example: 2/2 Allocation Request Available X Y Z X Y Z X Y Z A 0 1 0 0 0 0 0 0 0 B 2 0 0 2 0 2 C 3 0 3 0 0 1 D 2 1 1 1 0 0 E 0 0 2 0 0 2 qSuppose C requests for one more resource Z. qNow, A can run, making Available=[0,1,0]. qHowever, none of B, C, D and E can run. Therefore, B, C, D and E are deadlocked!
  • 26. The Use of a Detection Algorithm qFrequency vIf deadlocks occur frequently, then the detection algorithm should be invoked frequently. vOnce per hour or whenever CPU utilization becomes low (i.e., below 40%). Low CPU utilization means more processes are waiting.
  • 27. How to Recover: 1/3 qWhen a detection algorithm determines a deadlock has occurred, the algorithm may inform the system administrator to deal with it. Of, allow the system to recover from a deadlock. qThere are two options. vProcess Termination vResource Preemption qThese two options are not mutually exclusive.
  • 28. Recovery: Process Termination: 2/3 qAbort all deadlocked processes qAbort one process at a time until the deadlock cycle is eliminated qProblems: vAborting a process may not be easy. What if a process is updating or printing a large file? The system must find some way to maintain the state of the file and printer before they can be reused. vThe termination may be determined by the priority/importance of a process.
  • 29. Recovery: Resource Preemption: 3/3 qSelecting a victim: which resources and which processes are to be preempted? qRollback: If we preempt a resource from a process, what should be done with that process? vTotal Rollback: abort the process and restart it vPartial Rollback: rollback the process only as far as necessary to break the deadlock. qStarvation: We cannot always pick the same process as a victim. Some limit must be set.