Home >Backend Development >PHP Problem >How to avoid file deadlock in PHP development

How to avoid file deadlock in PHP development

王林
王林Original
2019-09-26 17:40:042038browse

How to avoid file deadlock in PHP development

Deadlock

Deadlock is a state in which the operating system or software is running: under multitasking, when one or more When a process waits for system resources and the resources are occupied by the system itself or other processes, a deadlock is formed. The most common form of deadlock occurring is when two or more threads wait for a resource occupied by another thread:

How to avoid file deadlock in PHP development

If both sequences occur simultaneously, Thread 1 will never be able to Lock B is acquired because lock B is owned by thread 2. At the same time, thread 2 can never obtain lock A because lock A is owned by thread 1.

Conditions for deadlock

The following four conditions must be met for the occurrence of deadlock:

① Mutually exclusive conditions: refers to the exclusive use of allocated resources by a process, that is, a resource is only occupied by one process within a period of time. If there are other processes requesting resources at this time, the requester can only wait until the process occupying the resources is used up and released.

②Request and retention conditions: means that the process has maintained at least one resource, but has made a new resource request, and the resource has been occupied by other processes. At this time, the requesting process is blocked. But he still holds on to the resources he has obtained.

③ Non-deprivation condition: refers to the resource that the process has obtained. It cannot be deprived before the end of use, and can only be released after the end of use.

④Loop waiting condition: means that when a deadlock occurs, there must be a process-a circular chain of resources, that is, in the process set {P0, P1,...,Pn} P0 is waiting for a resource occupied by P1, P1 is waiting for a resource occupied by P2,..., Pn is waiting for a resource occupied by P0.

How to avoid and deal with deadlock

Prevent deadlock:The way to prevent deadlock is to make the second and third of the four conditions , one of the four conditions cannot be established to avoid deadlock.

① Locking sequence: Lock in the same order.

When multiple processes require the same locks and add locks in different orders, deadlocks can easily occur. If it can be ensured that all processes obtain locks in the same order, then deadlock will not occur.

②Lock time limit: Add a certain time limit when the process tries to acquire the lock.

That is to say, if the time limit is exceeded when applying for a lock, the process will give up the request for the lock and release all acquired locks. Then try again after a random amount of time. This random amount of time gives other threads a chance to try to acquire the same lock, and allows the application to continue without acquiring the lock. The problem is that if there are many processes competing for the same batch of resources at the same time, even if there is a timeout and rollback mechanism, there may still be a problem where some processes try repeatedly but never get the lock.

Avoid deadlock: This method is also a prevention strategy in advance, but it does not require taking various restrictive measures in advance to destroy the four necessary conditions for deadlock. It is to use some method to prevent the system from entering an unsafe state during the dynamic allocation of resources, thereby avoiding deadlock.

Deadlock detection: It is mainly aimed at those situations where sequential locking cannot be achieved and the locking time limit is not feasible.

Through the set detection mechanism, the occurrence of deadlock is detected in a timely manner and the processes and resources related to the deadlock are accurately determined. Then take appropriate measures to remove the deadlock that has occurred from the system.

Whenever a process obtains a lock, it will be recorded in the data structure related to the process and the lock. And, every time a process requests a lock, it will be recorded in this data structure. When a process fails to request a lock, the thread can traverse the process and lock data structures to determine whether a deadlock has occurred.

For example:

Process A requests lock 2, but the lock is 2 times occupied by process B, so process A waits for process B. In the same way, process B waits for process C, process C waits for process D, and process D waits for process A. In order for process A to detect deadlock, it needs to progressively detect all locks requested by B. Starting from where process B requested, process A found process C, and then found process D. It is found that the lock requested by process D is occupied by process A itself, so a deadlock is detected.

When process A detects a deadlock, a feasible method is for process A to release the lock it holds, roll back, and then try again after a random period of time. This is similar to the lock time limit, except that the deadlock has already occurred.

Deadlock relief: This is a measure that matches the detection of deadlock.

When a process is found to be deadlocked, they should be freed from the deadlock state immediately.

One way is to deprive resources. Deprive a sufficient amount of resources from other processes to the deadlock process to relieve the deadlock state.      

Another method is to cancel the process. The simplest way to cancel a process is to kill all deadlocked processes; a slightly milder method is to cancel the processes one by one in some order, which only has enough resources available.

Recommended tutorial: PHP video tutorial

The above is the detailed content of How to avoid file deadlock in PHP development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn