SlideShare a Scribd company logo
1 of 22
Deadlocks
7.2 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
The Deadlock Problem
 A deadlock consists of a set of blocked processes, each
holding a resource and waiting to acquire a resource held by
another process in the set
 Example #1
 A system has 2 disk drives
 P1 and P2 each hold one disk drive and each needs the other one
 Example #2
 Semaphores A and B, initialized to 1
P0 P1
wait (A); wait(B)
wait (B); wait(A)
7.3 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Bridge Crossing Example
 Traffic only in one direction
 The resource is a one-lane bridge
 If a deadlock occurs, it can be resolved if one car backs up
(preempt resources and rollback)
 Several cars may have to be backed up if a deadlock
occurs
 Starvation is possible
7.4 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
System Model
 Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
 Each resource type Ri has 1 or more instances
 Each process utilizes a resource as follows:
 request
 use
 release
7.5 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock Characterization
 Mutual exclusion: only one process at a time can use a
resource
 Hold and wait: a process holding at least one resource is
waiting to acquire additional resources held by other
processes
 No preemption: a resource can be released only
voluntarily by the process holding it after that process has
completed its task
 Circular wait: there exists a set {P0, P1, …, P0} of waiting
processes such that P0 is waiting for a resource that is held
by P1, P1 is waiting for a resource that is held by
P2, …, Pn–1 is waiting for a resource that is held by
Pn, and Pn is waiting for a resource that is held by P0
Deadlock can arise if four conditions hold simultaneously.
7.6 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Resource-Allocation Graph
 V is partitioned into two types:
 P = {P1, P2, …, Pn}, the set consisting of all
the processes in the system
 R = {R1, R2, …, Rm}, the set consisting of all
resource types in the system
 request edge – directed edge P1  Rj
 assignment edge – directed edge Rj  Pi
A set of vertices V and a set of edges E.
7.7 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Resource-Allocation Graph (Cont.)
 Process
 Resource Type with 4 instances
 Pi requests instance of Rj
 Pi is holding an instance of Rj
Pi
Pi
Rj
Rj
7.8 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Resource Allocation Graph With A Deadlock
Before P3 requested an
instance of R2
After P3 requested an
instance of R2
7.9 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Graph With A Cycle But No Deadlock
Process P4 may release its instance of resource type R2. That
resource can then be allocated to P3, thereby breaking the cycle.
7.10 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Relationship of cycles to deadlocks
 If a resource allocation graph contains no cycles  no deadlock
 If a resource allocation graph contains a cycle and if only one instance exists
per resource type  deadlock
 If a resource allocation graph contains a cycle and and if several instances
exists per resource type  possibility of deadlock
7.11 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Methods for Handling Deadlocks
 Prevention
 Ensure that the system will never enter a
deadlock state
 Avoidance
 Ensure that the system will never enter an unsafe
state
 Detection
 Allow the system to enter a deadlock state and
then recover
 Do Nothing
 Ignore the problem and let the user or system
administrator respond to the problem; used by
most operating systems, including Windows and
UNIX
7.12 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock Prevention
 Mutual Exclusion – The mutual-exclusion condition must
hold for non-sharable resources
 Hold and Wait – we must guarantee that whenever a
process requests a resource, it does not hold any other
resources
 Require a process to request and be allocated all its resources
before it begins execution, or allow a process to request
resources only when the process has none
 Result: Low resource utilization; starvation possible
To prevent deadlock, we can restrain the ways that a request can be made
7.13 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock Prevention (Cont.)
 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
 A 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. For example:
F(tape drive) = 1
F(disk drive) = 5
F(printer) = 12
7.14 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock Avoidance
 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
 A resource-allocation state is defined by the number of available
and allocated resources, and the maximum demands of the
processes
Requires that the system has some additional a priori information
available.
a priori: formed or conceived beforehand
7.15 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Safe State
 That is:
 If the 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
7.16 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Deadlock Detection
 For deadlock detection, the system must provide
 An algorithm that examines the state of the system to detect whether a
deadlock has occurred
 And an algorithm to recover from the deadlock
 A detection-and-recovery scheme requires various kinds of overhead
 Run-time costs of maintaining necessary information and executing the
detection algorithm
 Potential losses inherent in recovering from a deadlock
7.17 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Recovery from Deadlock
 Two Approaches
 Process termination
 Resource preemption
7.18 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Recovery from Deadlock: Process Termination
 Abort all deadlocked processes
 This approach will break the deadlock, but at great expense
 Abort one process at a time until the deadlock cycle is eliminated
 This approach incurs considerable overhead, since, after each process is
aborted, a deadlock-detection algorithm must be re-invoked to determine
whether any processes are still deadlocked
 Many factors may affect which process is chosen for termination
 What is the priority of the process?
 How long has the process run so far and how much longer will the process
need to run before completing its task?
 How many and what type of resources has the process used?
 How many more resources does the process need in order to finish its task?
 How many processes will need to be terminated?
 Is the process interactive or batch?
7.19 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Recovery from Deadlock: Resource Preemption
 With this approach, we successively preempt some resources from
processes and give these resources to other processes until the
deadlock cycle is broken
 When preemption is required to deal with deadlocks, then three
issues need to be addressed:
 Selecting a victim – Which resources and which processes are to be
preempted?
 Rollback – If we preempt a resource from a process, what should be
done with that process?
 Starvation – How do we ensure that starvation will not occur? That is,
how can we guarantee that resources will not always be preempted
from the same process?
7.20 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
7.21 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Data Structures used to implement
Banker’s Algorithm
 Available: It is a 1-D array that tells the number of each resource type
(instance of resource type) currently available. Example:
Available[R1]= A, means that there are A instances of R1 resources
are currently available.
 Max: It is a 2-D array that tells the maximum number of each resource
type required by a process for successful execution. Example:
Max[P1][R1] = A, specifies that the process P1 needs a maximum of A
instances of resource R1 for complete execution.
 Allocation: It is a 2-D array that tells the number of types of each
resource type that has been allocated to the process. Example:
Allocation[P1][R1] = A, means that A instances of resource type R1
have been allocated to the process P1.
 Need: It is a 2-D array that tells the number of remaining instances of
each resource type required for execution. Example: Need[P1][R1]= A
tells that A instances of R1 resource type are required for the execution
of process P1.
 Need[i][j]= Max[i][j] - Allocation[i][j], where i corresponds any
process P(i) and j corresponds to any resouce type R(j)
7.22 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts - 7th Edition, Feb 14, 2005
Implementation Types of Bankers
Algorithms
 Resource Request Algorithm
 Safety Algorithm
 Resource- Request Algorithm
Whenever a process makes a request of the resources then this
algorithm checks that if the resource can be allocated or not.
 Safety Algorithm
The safety algorithm is applied to check whether a state is in a safe
state or not.

More Related Content

Similar to Lecture # 10-11 for deadlock operating system.ppt

Similar to Lecture # 10-11 for deadlock operating system.ppt (20)

ch7Deadlock.ppt
ch7Deadlock.pptch7Deadlock.ppt
ch7Deadlock.ppt
 
ch7.ppt
ch7.pptch7.ppt
ch7.ppt
 
ch7- Deadlock.ppt
ch7- Deadlock.pptch7- Deadlock.ppt
ch7- Deadlock.ppt
 
Galvin-operating System(Ch8)
Galvin-operating System(Ch8)Galvin-operating System(Ch8)
Galvin-operating System(Ch8)
 
Chapter 7
Chapter 7Chapter 7
Chapter 7
 
Deadlock : operating system ( BTECH CSE )
Deadlock : operating system ( BTECH CSE )Deadlock : operating system ( BTECH CSE )
Deadlock : operating system ( BTECH CSE )
 
Chapter 8 Operating Systems silberschatz : deadlocks
Chapter 8 Operating Systems silberschatz : deadlocksChapter 8 Operating Systems silberschatz : deadlocks
Chapter 8 Operating Systems silberschatz : deadlocks
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
7.Dead Locks
7.Dead Locks7.Dead Locks
7.Dead Locks
 
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
 
Ch8: Deadlocks
Ch8: DeadlocksCh8: Deadlocks
Ch8: Deadlocks
 
Ch7
Ch7Ch7
Ch7
 
Ch7
Ch7Ch7
Ch7
 
Os module 2 d
Os module 2 dOs module 2 d
Os module 2 d
 
Ch8
Ch8Ch8
Ch8
 
Process Synchronization And Deadlocks
Process Synchronization And DeadlocksProcess Synchronization And Deadlocks
Process Synchronization And Deadlocks
 
ch8.pptx
ch8.pptxch8.pptx
ch8.pptx
 
Deadlock_SVVSDM_DWD
Deadlock_SVVSDM_DWDDeadlock_SVVSDM_DWD
Deadlock_SVVSDM_DWD
 
O ssvv62015
O ssvv62015O ssvv62015
O ssvv62015
 
Deadlocks 160928121516-160928183232
Deadlocks 160928121516-160928183232Deadlocks 160928121516-160928183232
Deadlocks 160928121516-160928183232
 

Recently uploaded

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 

Recently uploaded (20)

9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 

Lecture # 10-11 for deadlock operating system.ppt

  • 2. 7.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 The Deadlock Problem  A deadlock consists of a set of blocked processes, each holding a resource and waiting to acquire a resource held by another process in the set  Example #1  A system has 2 disk drives  P1 and P2 each hold one disk drive and each needs the other one  Example #2  Semaphores A and B, initialized to 1 P0 P1 wait (A); wait(B) wait (B); wait(A)
  • 3. 7.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Bridge Crossing Example  Traffic only in one direction  The resource is a one-lane bridge  If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback)  Several cars may have to be backed up if a deadlock occurs  Starvation is possible
  • 4. 7.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 System Model  Resource types R1, R2, . . ., Rm CPU cycles, memory space, I/O devices  Each resource type Ri has 1 or more instances  Each process utilizes a resource as follows:  request  use  release
  • 5. 7.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Deadlock Characterization  Mutual exclusion: only one process at a time can use a resource  Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes  No preemption: a resource can be released only voluntarily by the process holding it after that process has completed its task  Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0 Deadlock can arise if four conditions hold simultaneously.
  • 6. 7.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Resource-Allocation Graph  V is partitioned into two types:  P = {P1, P2, …, Pn}, the set consisting of all the processes in the system  R = {R1, R2, …, Rm}, the set consisting of all resource types in the system  request edge – directed edge P1  Rj  assignment edge – directed edge Rj  Pi A set of vertices V and a set of edges E.
  • 7. 7.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Resource-Allocation Graph (Cont.)  Process  Resource Type with 4 instances  Pi requests instance of Rj  Pi is holding an instance of Rj Pi Pi Rj Rj
  • 8. 7.8 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Resource Allocation Graph With A Deadlock Before P3 requested an instance of R2 After P3 requested an instance of R2
  • 9. 7.9 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Graph With A Cycle But No Deadlock Process P4 may release its instance of resource type R2. That resource can then be allocated to P3, thereby breaking the cycle.
  • 10. 7.10 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Relationship of cycles to deadlocks  If a resource allocation graph contains no cycles  no deadlock  If a resource allocation graph contains a cycle and if only one instance exists per resource type  deadlock  If a resource allocation graph contains a cycle and and if several instances exists per resource type  possibility of deadlock
  • 11. 7.11 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Methods for Handling Deadlocks  Prevention  Ensure that the system will never enter a deadlock state  Avoidance  Ensure that the system will never enter an unsafe state  Detection  Allow the system to enter a deadlock state and then recover  Do Nothing  Ignore the problem and let the user or system administrator respond to the problem; used by most operating systems, including Windows and UNIX
  • 12. 7.12 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Deadlock Prevention  Mutual Exclusion – The mutual-exclusion condition must hold for non-sharable resources  Hold and Wait – we must guarantee that whenever a process requests a resource, it does not hold any other resources  Require a process to request and be allocated all its resources before it begins execution, or allow a process to request resources only when the process has none  Result: Low resource utilization; starvation possible To prevent deadlock, we can restrain the ways that a request can be made
  • 13. 7.13 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Deadlock Prevention (Cont.)  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  A 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. For example: F(tape drive) = 1 F(disk drive) = 5 F(printer) = 12
  • 14. 7.14 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Deadlock Avoidance  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  A resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes Requires that the system has some additional a priori information available. a priori: formed or conceived beforehand
  • 15. 7.15 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Safe State  That is:  If the 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
  • 16. 7.16 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Deadlock Detection  For deadlock detection, the system must provide  An algorithm that examines the state of the system to detect whether a deadlock has occurred  And an algorithm to recover from the deadlock  A detection-and-recovery scheme requires various kinds of overhead  Run-time costs of maintaining necessary information and executing the detection algorithm  Potential losses inherent in recovering from a deadlock
  • 17. 7.17 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Recovery from Deadlock  Two Approaches  Process termination  Resource preemption
  • 18. 7.18 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Recovery from Deadlock: Process Termination  Abort all deadlocked processes  This approach will break the deadlock, but at great expense  Abort one process at a time until the deadlock cycle is eliminated  This approach incurs considerable overhead, since, after each process is aborted, a deadlock-detection algorithm must be re-invoked to determine whether any processes are still deadlocked  Many factors may affect which process is chosen for termination  What is the priority of the process?  How long has the process run so far and how much longer will the process need to run before completing its task?  How many and what type of resources has the process used?  How many more resources does the process need in order to finish its task?  How many processes will need to be terminated?  Is the process interactive or batch?
  • 19. 7.19 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Recovery from Deadlock: Resource Preemption  With this approach, we successively preempt some resources from processes and give these resources to other processes until the deadlock cycle is broken  When preemption is required to deal with deadlocks, then three issues need to be addressed:  Selecting a victim – Which resources and which processes are to be preempted?  Rollback – If we preempt a resource from a process, what should be done with that process?  Starvation – How do we ensure that starvation will not occur? That is, how can we guarantee that resources will not always be preempted from the same process?
  • 20. 7.20 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005
  • 21. 7.21 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Data Structures used to implement Banker’s Algorithm  Available: It is a 1-D array that tells the number of each resource type (instance of resource type) currently available. Example: Available[R1]= A, means that there are A instances of R1 resources are currently available.  Max: It is a 2-D array that tells the maximum number of each resource type required by a process for successful execution. Example: Max[P1][R1] = A, specifies that the process P1 needs a maximum of A instances of resource R1 for complete execution.  Allocation: It is a 2-D array that tells the number of types of each resource type that has been allocated to the process. Example: Allocation[P1][R1] = A, means that A instances of resource type R1 have been allocated to the process P1.  Need: It is a 2-D array that tells the number of remaining instances of each resource type required for execution. Example: Need[P1][R1]= A tells that A instances of R1 resource type are required for the execution of process P1.  Need[i][j]= Max[i][j] - Allocation[i][j], where i corresponds any process P(i) and j corresponds to any resouce type R(j)
  • 22. 7.22 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7th Edition, Feb 14, 2005 Implementation Types of Bankers Algorithms  Resource Request Algorithm  Safety Algorithm  Resource- Request Algorithm Whenever a process makes a request of the resources then this algorithm checks that if the resource can be allocated or not.  Safety Algorithm The safety algorithm is applied to check whether a state is in a safe state or not.