Deadlock Prevention & Avoidance Department of Computer Science Southern Illinois University Edwardsville Spring, 2016 Dr. Hiroshi Fujinoki CS 314 Operating Systems Deadlock_Detection/000
CS 314 Operating Systems Deadlock_Avoidance/001 Deadlock Prevention Try to prevent any one of the four necessary conditions for a deadlock Mutual Exclusion Hold & Wait Circular wait Non preemptive resources
CS 314 Operating Systems Deadlock_Avoidance/002 Attacking mutual exclusion Some resources inherently require mutual exclusion Mutual Exclusion = While a process is using a resource no other process can use the same resource Example Problem Printers Files for updates CD-Rs
CS 314 Operating Systems Deadlock_Avoidance/003 Attacking hold & wait Require all processes to acquire all the needed resources at once (a) Resource request does not have to be at the beginning of a process (b) If any of the required resource is not available, drop everything (then try it again later) (c) Any process can not make a request for any new resource, while the process currently has any resource A process does NOT have to request all the resources at once (a) Whenever an additional resource is needed, drop all what are possessed by this process and request everything at once (b) If any process steals your resource(s) when you temporarily have to wait until all resources become available
CS 314 Operating Systems Deadlock_Avoidance/004 Attacking hold & wait Possible “Hold & Wait” 1-(b) “Hold & Wait” 1-(a) 1-(c) Time # of resources acquired Time
CS 314 Operating Systems Deadlock_Avoidance/005 Attacking hold & wait Time # of resources acquired 2-(a) Another Process If any of resources is missing, drop everything Drop all what you possess first Then you try to get all you need # of resources acquired Time 2-(b)
CS 314 Operating Systems Deadlock_Avoidance/006 Attacking non-preemption The same problem we had for “mutual exclusion” “Attacking Non-Preemption” Problem = Allow preemption = You process can loose your resource at any time No one will be happy in loosing a CD-R drive in the middle of burning a CD
CS 314 Operating Systems Deadlock_Avoidance/007 Circular-Wait Assign unique ID for every non-sharable resource in a system Every process must start requesting resources in either ascending or descending order of resource IDs A B X1X1 Y2Y2 Z3Z3 Request (X, Y, Z) (Y, Z)
CS 314 Operating Systems Deadlock_Avoidance/008 Circular-Wait Assign unique ID for every non-sharable resource in a system Every process must start requesting resources in either ascending or descending order of resource IDs A B X1X1 Y2Y2 Z3Z3 Assigned Request (X, Y, Z) (Y, Z)
CS 314 Operating Systems Deadlock_Avoidance/009 Circular-Wait Assign unique ID for every non-sharable resource in a system Every process must start requesting resources in either ascending or descending order of resource IDs A B X1X1 Y2Y2 Z3Z3 Assigned Request (X, Y, Z) (Y, Z)
CS 314 Operating Systems Deadlock_Avoidance/010 Circular-Wait Assign unique ID for every non-sharable resource in a system Every process must start requesting resources in either ascending or descending order of resource IDs A B X1X1 Y2Y2 Z3Z3 Assigned (X, Y, Z) (Y, Z) Assigned Requested Deadlock
CS 314 Operating Systems Deadlock_Avoidance/011 Circular-Wait Assign unique ID for every non-sharable resource in a system Every process must start requesting resources in either ascending or descending order of resource IDs A B X1X1 Y2Y2 Z3Z3 Assigned (X, Y, Z) (Y, Z) Request Process B blocks Request
CS 314 Operating Systems Deadlock_Avoidance/012 Circular-Wait Assign unique ID for every non-sharable resource in a system Every process must start requesting resources in either ascending or descending order of resource IDs A B X1X1 Y2Y2 Z3Z3 Assigned (X, Y, Z) (Y, Z) Request Process B blocks Assigned Process A Completes
CS 314 Operating Systems Deadlock_Avoidance/013 Circular-Wait Assign unique ID for every non-sharable resource in a system Every process must start requesting resources in either ascending or descending order of resource IDs B X1X1 Y2Y2 Z3Z3 (Y, Z) Request
CS 314 Operating Systems Deadlock_Avoidance/014 Circular-Wait Assign unique ID for every non-sharable resource in a system Every process must start requesting resources in either ascending or descending order of resource IDs B X1X1 Y2Y2 Z3Z3 (Y, Z) Assigned Requested
CS 314 Operating Systems Deadlock_Avoidance/015 Circular-Wait Assign unique ID for every non-sharable resource in a system Every process must start requesting resources in either ascending or descending order of resource IDs B X1X1 Y2Y2 Z3Z3 (Y, Z) Assigned Process B completes
CS 314 Operating Systems Deadlock_Avoidance/016 Deadlock Avoidance When a process makes a request to a resource, the OS simulates if it will cause a deadlock in the future if no one releases their currently-held resource: If it will cause a deadlock If it will not cause a deadlock the request is denied (“unsafe state”) the requested resource is granted (“safe state”)
CS 314 Operating Systems Deadlock_Avoidance/016 The two states defined in Deadlock Avoidance A system is in “safe state” if there is at least one assignment schedule to complete all the processes without deadlock and without any process releasing their resources Safe State Unsafe State If a system is not in safe state, the system must be in “unsafe state” If a system is in safe state, deadlock can always be avoided If a system is in unsafe state, deadlock may not be avoided (deadlock will never happen even in the worst case) (deadlock can happen in the worst case)
CS 314 Operating Systems Deadlock_Avoidance/017 ProcessAssigned MAX. NeedRequests Pending Process Status ProcessAssigned MAX. Need B A C B A C The number of requests a process made, but not granted because OS does not have the requested resources Requests Pending Process Status If a process has at least one pending request, process is BLOCKED Otherwise a process is RUNNING If a process gets all required resources then, the process becomes COMPLTED RUNNING
CS 314 Operating Systems ProcessAssigned MAX. NeedRequests Pending Process Status B A C RUNNING Available 4 Assume: 11 instances of the resource R exist Deadlock_Avoidance/018
CS 314 Operating Systems ProcessAssigned MAX. NeedRequests Pending Process Status B A C RUNNING Available 4 Assume: 11 instances of the resource R exist If Process A requests 4 more instances of this resource ProcessAssigned MAX. NeedRequests Pending Process Status B A C RUNNING Available This is “unsafe state” But Not Deadlock yet Deadlock_Avoidance/018
CS 314 Operating Systems ProcessAssigned MAX. NeedRequests Pending Process Status B A C RUNNING Available 4 Assume: 11 instances of the resource R exist If Process A requests 1 more instances of this resource ProcessAssigned MAX. NeedRequests Pending Process Status B A C RUNNING Available BLOCKED Deadlock_Avoidance/019
CS 314 Operating Systems ProcessAssigned MAX. NeedRequests Pending Process Status B A C RUNNING Available 4 Assume: 11 instances of the resource R exist If Process B requests 2 more instances of this resource ProcessAssigned MAX. NeedRequests Pending Process Status B A C RUNNING Available 0 7BLOCKED 2 Deadlock_Avoidance/020
CS 314 Operating Systems ProcessAssigned MAX. NeedRequests Pending Process Status B A C RUNNING Available 4 Assume: 11 instances of the resource R exist If Process C requests 2 more instances of this resource ProcessAssigned MAX. NeedRequests Pending Process Status B A C RUNNING Available 0 7BLOCKED Now Deadlock 2 BLOCKED 2 Deadlock_Avoidance/021
CS 314 Operating Systems ProcessAssigned MAX. NeedRequests Pending Process Status B A C RUNNING Available 4 Assume: 11 instances of the resource R exist If Process B requests 2 more instances of this resource ProcessAssigned MAX. NeedRequests Pending Process Status B A C RUNNING Available This is “safe state” 0 Deadlock_Avoidance/022
CS 314 Operating Systems ProcessAssigned MAX. NeedRequests Pending Process Status B A C RUNNING Available 4 Assume: 11 instances of the resource R exist If Process B requests 2 more instances of this resource ProcessAssigned MAX. NeedRequests Pending Process Status B A C RUNNING Available 6 0 This is “safe state” COMPLETED Deadlock_Avoidance/023