What are the causes and necessary conditions for deadlocks?
Deadlock: a deadlock occurs when multiple processes compete for resources.
Cause of deadlock
L competitive resources: when multiple processes in the system use shared resources and the resources are insufficient to meet their needs, the process may compete for resources and lead to deadlocks.
L The Order of inter-process promotion is invalid: improper order of request and resource release may also lead to process deadlocks.
Conditions for deadlock
- Mutex: A process schedules the allocated resources and occupies only one process for a period of time.
- Request and persistence Conditions: Indicates that a process maintains at least one resource, but initiates a new resource request, which is occupied by other resources. In this case, the request process is blocked, however, you must keep the resources you have occupied.
- Non-deprivation conditions: The process has obtained resources and cannot be deprived before it is used up. It can only be released by the process itself when it is used up.
- Loop wait Condition: When a deadlock occurs, there must be a process-resource ring chain.
How to handle deadlocks
LDeadlock Prevention
One of the four necessary conditions for Deadlock Prevention in advance.
- Discard the "request and persistence" condition:
The process is one-time when applying for resources.
How to discard the "request": when a process comes, allocate all resources at a time (if the system meets the requirements) so that there will be no more "requests.
How to discard "persistence": as long as one resource is not allocated, other resources are not allocated to the process.
- Discard the "do not deprive" condition: In this method, the process puts forward resource requirements one by one. If a process obtains some resources but does not obtain other resources, it releases the resources it occupies.
- Discard the "loop wait" condition: Sort resources and apply in order when the process applies for resources.
LAvoid deadlocks
Prevention in advance is not necessary to damage the four conditions for deadlock, but to prevent the system from entering an insecure state in some way. Currently, this method is commonly used in a well-developed system.BankersAlgorithm
Security Status: Indicates that the system can follow certain Process Order (P1, P2 ,... (PN) to allocate the required resources to each process PI until the maximum resource requirements of each process are met, so that each process can be completed smoothly. If the system cannot find such a security sequence, it is said that the system is in an insecure state.
LDeadlock Detection
You do not have to take any restrictive measures in advance or check whether the system is in the unsafe zone. This method allows deadlocks. The key is,When a deadlock occurs, the system can detect the deadlock through the detection mechanism and precisely determine the processes and resources related to the deadlock,Then, take appropriate measures to clear existing deadlocks from the system
LRemove deadlock
This is used together with Deadlock Detection. When a deadlock has been detected in the system, the process must be freed from the deadlock state. A common method is to undo or suspend some processes to recycle some resources, and then allocate these resources to the blocked processes so that they are ready to continue running.