What is a deadlock?
If each process inside a process collection waits for an event that can only be raised by one of the other processes in the collection, including himself, this is a deadlock.
Resources A, B, Resource A and resource B are both inalienable resources,
Now that process C has applied for resource A, process D has also requested resource B,
process C will need resource b for the next operation, and process D happens to be requesting resource A,
process C, D is not getting the next resource, then it triggers a deadlock. The
then applies back to the definition: if each process (process C and Process D) of a process set inside (process C and Process D) is waiting for only one of the other processes in this collection (for process C, he is waiting for process D; For process D, he waits for process C) to raise the event (freeing the appropriate resource).
Resources here include soft resources (blocks of code) and hard resources (such as scanners). Resources can generally be divided into two types: the deprivation of resources (preemptable) and the Inalienable resources (nonpreemptable). In general, deadlocks caused by the deprivation of resources can be solved by the redistribution of resources in the system, so generally speaking, the deadlock is caused by the inalienable resources.
Four necessary conditions for a deadlock
Mutex condition (Mutual exclusion): A resource cannot be shared and can only be used by one process.
Request and hold condition (holds and wait): a process that has already received a resource can request a new resource again.
Non-deprivation condition (no pre-emption): The allocated resources cannot be forcibly stripped from the corresponding process.
Cyclic wait condition (Circular wait): Several processes in the system make up loops in which each process waits for resources that are being consumed by neighboring processes.
Policies to handle deadlocks
1. Ignore the problem. For example, the ostrich algorithm, the algorithm can be applied in the case of very few deadlocks occur. Why is called the ostrich algorithm, because the legend of the ostrich saw the danger of burying his head under the ground, perhaps the ostrich feel that there is no danger to see it. It's kind of like deceiving.
2. Detect the deadlock and recover.
3. Carefully allocate resources dynamically to avoid deadlocks.
4. Prevent deadlocks by breaking the deadlock by one of the four necessary conditions.
Four necessary conditions for deadlocks and strategies for dealing with deadlocks