deadlocks2
TRANSCRIPT
![Page 1: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/1.jpg)
Concurrency: Deadlock and Starvation
![Page 2: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/2.jpg)
Roadmap
• Principals of Deadlock– Deadlock prevention– Deadlock Avoidance– Deadlock detection
![Page 3: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/3.jpg)
Deadlock
• A set of processes is deadlocked when each process in the set is blocked awaiting an event that can only be triggered by another blocked process in the set– Typically involves processes competing for
the same set of resources
• No efficient solution
![Page 4: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/4.jpg)
Potential Deadlock
I need quad A and
B
I need quad B and
C
I need quad C and B
I need quad D and A
![Page 5: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/5.jpg)
Actual Deadlock
HALT until B is free
HALT until C is free
HALT until D is free
HALT until A is free
![Page 6: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/6.jpg)
Resource Categories
Two general categories of resources:• Reusable
– can be safely used by only one process at a time and is not depleted by that use.
• Consumable– one that can be created (produced) and
destroyed (consumed).
![Page 7: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/7.jpg)
Reusable Resources
• Such as:– Processors, I/O channels, main and
secondary memory, devices, and data structures such as files, databases, and semaphores
• Deadlock occurs if each process holds one resource and requests the other
![Page 8: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/8.jpg)
Example of Reuse Deadlock
• Consider two processes that compete for exclusive access to a disk file D and a tape drive T.
• Deadlock occurs if each process holds one resource and requests the other.
![Page 9: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/9.jpg)
Reusable Resources Example
Consider : p0 p1 q0 q1 p2 q2
![Page 10: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/10.jpg)
Example 2:Memory Request
• Space is available for allocation of 200Kbytes, and the following sequence of events occur
• Deadlock occurs if both processes progress to their second request
P1
. . .
. . .Request 80 Kbytes;
Request 60 Kbytes;
P2
. . .
. . .Request 70 Kbytes;
Request 80 Kbytes;
![Page 11: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/11.jpg)
Consumable Resources
• Such as Interrupts, signals, messages, and information in I/O buffers
• Deadlock may occur if a Receive message is blocking
• May take a rare combination of events to cause deadlock
![Page 12: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/12.jpg)
Example of Deadlock
• Consider a pair of processes, in which each process attempts to receive a message from the other process and then send a message to the other process
![Page 13: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/13.jpg)
Resource Allocation Graphs
• Directed graph that depicts a state of the system of resources and processes
![Page 14: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/14.jpg)
Conditions for possible Deadlock
• Mutual exclusion– Only one process may use a resource at a
time
• Hold-and-wait– A process may hold allocated resources while
awaiting assignment of others
• No pre-emption– No resource can be forcibly removed form a
process holding it
![Page 15: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/15.jpg)
Actual Deadlock Requires …
All previous 3 conditions plus:• Circular wait
– A closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain
![Page 16: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/16.jpg)
Resource Allocation Graphs of deadlock
![Page 17: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/17.jpg)
Resource Allocation Graphs
![Page 18: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/18.jpg)
Dealing with Deadlock
• Three general approaches exist for dealing with deadlock.– Prevent deadlock– Avoid deadlock– Detect Deadlock
![Page 19: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/19.jpg)
Roadmap
• Principals of Deadlock– Deadlock prevention– Deadlock Avoidance– Deadlock detection
![Page 20: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/20.jpg)
Deadlock Prevention Strategy
• Design a system in such a way that the possibility of deadlock is excluded.
• Two main methods– Indirect – prevent all three of the necessary
conditions occurring at once– Direct – prevent circular waits
![Page 21: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/21.jpg)
Deadlock Prevention Conditions 1 & 2
• Mutual Exclusion– Must be supported by the OS
• Hold and Wait– Require a process request all of its required
resources at one time• a process may be held up for a long time waiting • resources allocated to a process may remain
unused for a considerable period
![Page 22: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/22.jpg)
Deadlock PreventionConditions 3 & 4
• No Preemption– Process must release resource and request
again– OS may preempt a process to require it
releases its resources
• Circular Wait– Define a linear ordering of resource types
![Page 23: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/23.jpg)
Roadmap
• Principals of Deadlock– Deadlock prevention– Deadlock Avoidance– Deadlock detection
![Page 24: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/24.jpg)
Deadlock Avoidance
• A decision is made dynamically whether the current resource allocation request will, if granted, potentially lead to a deadlock
• Requires knowledge of future process requests
![Page 25: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/25.jpg)
Two Approaches to Deadlock Avoidance
• Process Initiation Denial– Do not start a process if its demands might
lead to deadlock
• Resource Allocation Denial– Do not grant an incremental resource request
to a process if this allocation might lead to deadlock
![Page 26: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/26.jpg)
Process Initiation Denial
• A process is only started if the maximum claim of all current processes plus those of the new process can be met.
• Not optimal, – Assumes the worst: that all processes will
make their maximum claims together.
![Page 27: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/27.jpg)
Resource Allocation Denial
• Referred to as the banker’s algorithm– A strategy of resource allocation denial
• Consider a system with fixed number of resources– State of the system is the current allocation of
resources to process– Safe state is where there is at least one
sequence that does not result in deadlock– Unsafe state is a state that is not safe
![Page 28: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/28.jpg)
Determination ofSafe State
• A system consisting of four processes and three resources.
• Allocations are made to processors• Is this a safe state?
Amount of Existing
Resources
Resources available
after allocation
![Page 29: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/29.jpg)
Process i
• Cij - Aij ≤ Vj, for all j
• This is not possible for P1, – which has only 1 unit of R1 and requires 2
more units of R1, 2 units of R2, and 2 units of R3.
• If we assign one unit of R3 to process P2, – Then P2 has its maximum required resources
allocated and can run to completion and return resources to ‘available’ pool
![Page 30: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/30.jpg)
After P2 runs to completion
• Can any of the remaining processes can be completed?
Note P2 is completed
![Page 31: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/31.jpg)
After P1 completes
![Page 32: Deadlocks2](https://reader033.vdocuments.site/reader033/viewer/2022052619/555aeab7d8b42a4c7d8b4bf9/html5/thumbnails/32.jpg)
P3 Completes
Thus, the state defined originally is a safe
state.