What is a process deadlock? What are the conditions? How to avoid deadlock?

The concept of deadlock: refers to a system state in which two or more processes wait endlessly for a resource.

 The main reasons for the deadlock are: ( 1 ) insufficient system resources; ( 2 ) improper order of process running; ( 3 ) improper resource allocation.

 

Necessary conditions for deadlock:

( 1 ) Mutual exclusion , a resource can only be used by one process at a time;

( 2 ) Hold and wait ( hold and wait ), when a process is blocked due to requesting resources, keep the resources it has acquired;

( 3 ) No preemption ( no preemption ), the resources that the process has obtained, cannot be forcibly deprived before being released;

( 4 ) Circular wait ( circular wait ), a process of forming a circular waiting resource relationship between several processes.

These four conditions are necessary for deadlock. As long as the system has a deadlock, these conditions must be established, and as long as one of the above conditions is not met, no deadlock will occur.

 

Deadlock processing strategies: ostrich strategy, prevention strategy, avoidance strategy, detection and removal of deadlock

Ostrich strategy: turn a blind eye

 

Prevention strategy: Static resource allocation method: All resources are allocated at once, so that there will be no more requests

Ordered resource allocation method: the system assigns a number to each type of resource, each process requests resources in the order of increasing number, and the release is the opposite (breaking the loop waiting condition)

There is also the possibility of destroying possession and waiting conditions. When a process acquires some resources but cannot obtain other resources, it releases the occupied resources (destroying possession and waiting conditions)

 

 

Avoidance strategy: Allow processes to apply for resources dynamically. Therefore, the system calculates the security of resource allocation in advance before performing resource allocation. If the allocation does not cause the system to enter an unsafe state, the resources are allocated to the process; otherwise, the process waits. The most representative algorithm for avoiding deadlock is the banker's algorithm.

Banker's algorithm: First, the concepts of state and security state need to be defined. The state of the system is the current allocation of resources to the process. The state includes two vectors Resource (the total amount of each resource in the system) and Available ( the total amount of each resource not allocated to the process) and two matrix Claim (representing the process's resource requirements) and Allocation (representing the current allocation Resources to the process). Safe state means that at least one resource allocation sequence will not cause a deadlock. When a process requests a set of resources, it is assumed that the request is agreed, thereby changing the state of the system, and then determining whether the result is still in a safe state. If yes, agree to the request; if not, block the process until the system state is still safe after agreeing to the request.

 

Detect deadlock

1. Assign a unique number to each process and each resource

2. Then set up a resource allocation table to record the relationship between each process and its occupied resources

3. Set up a process waiting table to record the relationship between each process and the resource to be applied for

By checking these two tables to determine whether the deadlock really happened

 

Remove the deadlock :

When a process deadlock is discovered, it should be immediately released from the deadlock state. The methods commonly used are:

Deprivation of resources: Deprive sufficient amount of resources from other processes to the deadlock process, so that he can lift the deadlock state;

Undo process: You can directly cancel the deadlock process or the process with the lowest cost until the deadlock state is eliminated; the so-called cost refers to priority, running cost, process importance and value, etc.

Guess you like

Origin www.cnblogs.com/wl889490/p/12748239.html