Download presentation
Presentation is loading. Please wait.
1
Deadlock Prevention & Avoidance
CS 314 Operating Systems Deadlock Prevention & Avoidance Department of Computer Science Southern Illinois University Edwardsville Spring, 2019 Dr. Hiroshi Fujinoki Deadlock_Detection/000
2
CS 314 Operating Systems Deadlock Prevention
Try to prevent any one of the four necessary conditions for a deadlock Mutual Exclusion Hold & Wait Circular wait Non preemptive resources Deadlock_Avoidance/001
3
CS 314 Operating Systems Attacking mutual exclusion
Mutual Exclusion = While a process is using a resource no other process can use the same resource Problem Some resources inherently require mutual exclusion Example Printers Files for updates CD-Rs Deadlock_Avoidance/002
4
CS 314 Operating Systems 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 holds some 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 Deadlock_Avoidance/003
5
CS 314 Operating Systems Attacking hold & wait Possible “Hold & Wait”
Time # of resources acquired 1-(a) 1-(c) # of resources acquired Time “Hold & Wait” 1-(b) Deadlock_Avoidance/004
6
missing, drop everything
CS 314 Operating Systems Attacking hold & wait Then you try to get all you need # of resources acquired 2-(a) Drop all what you possess first Time # of resources acquired Time 2-(b) Another Process If any of resources is missing, drop everything Deadlock_Avoidance/005
7
CS 314 Operating Systems Attacking non-preemption
= Allow preemption = You process can loose your resource at any time Problem The same problem we had for “mutual exclusion” No one will be happy in loosing a CD-R drive in the middle of burning a CD Deadlock_Avoidance/006
8
CS 314 Operating Systems 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 X1 Y2 Z3 Request (X, Y, Z) (Y, Z) A B Deadlock_Avoidance/007
9
CS 314 Operating Systems 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 X1 Y2 Z3 Assigned (X, Y, Z) A B Request (Y, Z) Deadlock_Avoidance/008
10
CS 314 Operating Systems 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 X1 Y2 Z3 Assigned (X, Y, Z) A B Assigned (Y, Z) Request Deadlock_Avoidance/009
11
CS 314 Operating Systems 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 X1 Y2 Z3 Assigned Deadlock A B Assigned Requested (X, Y, Z) (Y, Z) Requested Assigned Deadlock_Avoidance/010
12
CS 314 Operating Systems 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 X1 Y2 Z3 Assigned Process B blocks Assigned Request (X, Y, Z) A B (Y, Z) Request Deadlock_Avoidance/011
13
CS 314 Operating Systems 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 X1 Y2 Z3 Assigned Process B blocks Process A Completes A B Assigned Request (X, Y, Z) (Y, Z) Assigned Deadlock_Avoidance/012
14
CS 314 Operating Systems 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 X1 Y2 Z3 Request B (Y, Z) Deadlock_Avoidance/013
15
CS 314 Operating Systems 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 X1 Y2 Z3 Assigned B (Y, Z) Requested Deadlock_Avoidance/014
16
CS 314 Operating Systems 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 X1 Y2 Z3 Process B completes Assigned B (Y, Z) Assigned Deadlock_Avoidance/015
17
CS 314 Operating Systems 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”) Deadlock_Avoidance/016
18
CS 314 Operating Systems The two states defined in Deadlock Avoidance
Safe State Unsafe State 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 (deadlock will never happen even in the worst case) If a system is in safe state, deadlock can always be avoided If a system is not in safe state, the system must be in “unsafe state” (deadlock can happen in the worst case) If a system is in unsafe state, deadlock may not be avoided Deadlock_Avoidance/016
19
CS 314 Operating Systems Requests Pending Process Status Process
Assigned MAX. Need B A C 2 3 4 9 7 The number of requests a process made, but not granted because OS does not have the requested resources If a process has at least one pending request, process is BLOCKED If a process gets all required resources then, the process becomes COMPLTED Otherwise a process is RUNNING Process Assigned MAX. Need Requests Pending Process Status B A C 2 3 4 9 7 RUNNING Deadlock_Avoidance/017
20
CS 314 Operating Systems Assume: 11 instances of the resource R exist
Process Assigned MAX. Need Requests Pending Process Status Available B A C 2 3 4 9 7 RUNNING 4 RUNNING RUNNING Deadlock_Avoidance/018-A
21
CS 314 Operating Systems Assume: 11 instances of the resource R exist
Process Assigned MAX. Need Requests Pending Process Status Available B A C 2 3 4 9 7 RUNNING 4 RUNNING RUNNING But Not Deadlock yet If Process A requests 4 more instances of this resource Process Assigned MAX. Need Requests Pending Process Status Available B A C 3 4 9 7 4 BLOCKED RUNNING 4 2 RUNNING 2 RUNNING Deadlock_Avoidance/018-B
22
CS 314 Operating Systems Assume: 11 instances of the resource R exist
Deadlock = all (3) processes blocked waiting for additional resource Process Assigned MAX. Need Requests Pending Process Status Available B A C 2 3 4 9 7 RUNNING 4 RUNNING RUNNING Unsafe state = no one can complete if no one releases resource If Process A requests 4 more instances of this resource This is “unsafe state” Process Assigned MAX. Need Requests Pending Process Status Available B A C 7 3 4 9 7 4 RUNNING BLOCKED 4 2 RUNNING 2 RUNNING But Not Deadlock yet Deadlock_Avoidance/018-C
23
CS 314 Operating Systems Assume: 11 instances of the resource R exist
Process Assigned MAX. Need Requests Pending Process Status Available B A C 2 3 4 9 7 RUNNING 4 RUNNING RUNNING If Process A requests 1 more instances of this resource Process Assigned MAX. Need Requests Pending Process Status Available B A C 7 4 9 7 1 RUNNING BLOCKED 2 RUNNING 2 RUNNING Deadlock_Avoidance/019
24
CS 314 Operating Systems Assume: 11 instances of the resource R exist
Process Assigned MAX. Need Requests Pending Process Status Available B A C 2 3 4 9 7 RUNNING 4 RUNNING RUNNING If Process B requests 2 more instances of this resource Process Assigned MAX. Need Requests Pending Process Status Available B A C 7 4 9 7 1 BLOCKED 2 2 BLOCKED RUNNING 2 RUNNING Deadlock_Avoidance/020
25
CS 314 Operating Systems Assume: 11 instances of the resource R exist
Process Assigned MAX. Need Requests Pending Process Status Available B A C 2 3 4 9 7 RUNNING 4 RUNNING RUNNING Now Deadlock If Process C requests 2 more instances of this resource Process Assigned MAX. Need Requests Pending Process Status Available B A C 7 4 9 7 1 BLOCKED 2 2 BLOCKED 2 2 RUNNING BLOCKED Deadlock_Avoidance/021
26
CS 314 Operating Systems Assume: 11 instances of the resource R exist
Process Assigned MAX. Need Requests Pending Process Status Available B A C 2 3 4 9 7 RUNNING 4 RUNNING RUNNING This is “safe state” If Process B requests 2 more instances of this resource Process Assigned MAX. Need Requests Pending Process Status Available B A C 3 4 9 7 RUNNING 2 4 4 2 2 RUNNING 2 RUNNING Deadlock_Avoidance/022
27
CS 314 Operating Systems Assume: 11 instances of the resource R exist
Process Assigned MAX. Need Requests Pending Process Status Available B A C 2 3 4 9 7 RUNNING 4 RUNNING RUNNING This is “safe state” If Process B requests 2 more instances of this resource Process Assigned MAX. Need Requests Pending Process Status Available B A C 3 9 RUNNING 6 COMPLETED RUNNING 2 7 RUNNING Deadlock_Avoidance/023
28
Process Assigned MAX. Need Requests Pending Process Status B A C 2 4 7 14 5 RUNNING Available 6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.