What is deadlock, briefly describe the four necessary conditions for deadlock to occur, and how to avoid and prevent deadlock

what is deadlock

Deadlock refers to a deadlock (waiting for each other) caused by multiple processes competing for resources. Without external force, these processes will not be able to move forward. For example, in a certain computer system, there is only one printer and one input device, the process P1 is occupying the input device, and at the same time requests to use the printer, but at this time the printer is being occupied by the process P2, and P2 is not releasing the printer. Previously, another request was made to use the input device that was being occupied by P1. In this way, the two processes wait endlessly for each other, and neither can continue to execute. At this time, the two processes fall into a deadlock state.

Cause of deadlock

1. Competition for system resources

Competition for system resources leads to insufficient system resources and improper resource allocation, resulting in deadlocks.

2. The process is running in an inappropriate order of advancement

During the running process of the process, the order of requesting and releasing resources is not correct, which will lead to deadlock.

There are four necessary conditions for a deadlock to occur:

Mutual exclusion condition: A resource can only be used by one process at a time, that is, a resource is occupied by only one process for a period of time. At this time, if another process requests the resource, the requesting process can only wait.

Request and hold conditions: The process has held at least one resource, but a new resource request is made, and the resource has been occupied by other processes. At this time, the requesting process is blocked, but the resource that it has obtained is kept.

Inalienable condition: The resource obtained by a process cannot be forcibly taken away by other processes before it is used up, that is, it can only be released by the process that obtained the resource itself (it can only be released actively).

Circular waiting condition: The relationship between several processes forming an end-to-end cyclic waiting for resources

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

Deadlock avoidance and prevention:

The basic idea of ​​deadlock avoidance:

The system dynamically checks each resource application that the process can satisfy, and decides whether to allocate resources according to the check results. If the system may deadlock after allocation, it will not be allocated, otherwise it will be allocated. This is a dynamic strategy to ensure that the system does not enter a deadlock state.

By understanding the causes of deadlocks, especially the four necessary conditions for deadlocks, you can avoid, prevent and remove deadlocks to the greatest extent possible. Therefore, in terms of system design, process scheduling, etc., pay attention to how to make these four necessary conditions invalid, and how to determine a reasonable allocation algorithm for resources to prevent processes from permanently occupying system resources. Also, prevent processes from consuming resources while they are in a wait state. Therefore, reasonable planning should be given to the allocation of resources.

The difference between deadlock avoidance and deadlock prevention:

Deadlock prevention is to try to destroy at least one of the four necessary conditions for deadlock, strictly prevent the occurrence of deadlock, and deadlock avoidance is less strict to limit the existence of necessary conditions for deadlock, because even deadlock If the necessary conditions exist, a deadlock does not necessarily occur. Deadlock avoidance is to avoid the eventual occurrence of deadlock during system operation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326523835&siteId=291194637