The previous blog has mentioned the deadlock problem many times, so let's take a look at it first.
What is a deadlock?
In fact, a deadlock means that in a multi-channel program system, each process in a set of processes is waiting for a resource that is owned by another process in the group's process and is never freed, like a thread. This phenomenon says that the system is in a deadlock state, referred to as a deadlock. A process that is in a deadlock state is called a deadlock process.
The four necessary conditions for creating a deadlock:
In fact, the deadlock generated: 1. Resource competition, 2. The sequence of process advancement is unreasonable when the multi-channel program runs.
That is, 4 prerequisites:
(1) Mutually exclusive use of resources.
Resources are exclusive and exclusive use, the process/thread mutually exclusive use of resources, that is, at any time a resource can only be used by one process, the other process if the request for a resource, and the resource is led a process occupies, the applicant waits, know that the resource is released by the occupier
(2) Request and maintain resources
Each time the process requests a portion of the resources he needs, he continues to occupy the allocated resources while applying for new resources.
(3) Non-looting of resources
The resources obtained by the process cannot be forcibly deprived by other processes until it is exhausted and can only be released voluntarily by the process
(4) Cyclic waiting for resources.
Also known as loop waiting. In the event of a deadlock, there must be a process waiting for queue {P1,P2,P3,... Pn}, where P1 waits for P2 to occupy resources, P2 wait for P3 to occupy resources ... PN waits for P1 to occupy resources and form a process waiting loop. The resources that each of the processes in the loop have occupied are also requested by another process, that is, the previous process occupies the resources requested by the latter process.
Remember that these 4 conditions are necessary, as long as the system has a deadlock, then the above four conditions at least one is established.
Now that you have a deadlock problem, how do you solve a deadlock?
(1) Prevention of deadlocks: the necessary condition for destroying 4 deadlocks, (1) is the inherent nature of resource use and cannot be changed.
1. Destruction of "inalienable conditions".
2. Breach of "request and hold"
3. Destroy "Cycle Wait"
(2) Deadlock avoidance:
1. Set the State security.
2. Adopt the Banker algorithm.
(3) Deadlock detection and cancellation:
Deadlock Detection:
1. Specify a unique number for each process and each resource.
2. Set the Resource Allocation Status table, each containing the resource number and the process number that owns the resource, and the Resource Allocation table records that each resource is being occupied by that process.
3. Set up a process to wait for the allocation table, each containing the "process number" and the "resource number" that the process waits for.
4. Deadlock detection algorithm: When any process PJ requests a resource RI that has been occupied by another process, deadlock detection is performed by repeatedly locating the resource Allocation table and the process waiting table to determine if the process PJ's request for resource RI causes a loop, and if so, a deadlock is determined.
Deadlock release:
1. Deprivation of resources
(1) Restore algorithm
(2) Set up checkpoints.
2. The process of revocation.
This article is from the "egg-left" blog, please be sure to keep this source http://memory73.blog.51cto.com/10530560/1769677
Linux: Deadlock Problem