Exclusive lock Deadlock Status detailed

Source: Internet
Author: User
Tags mutex

In programming, the concept of object mutex is introduced to ensure the integrity of the shared data operation. Each object corresponds to a token that can be called a "mutex", which is used to guarantee that only one thread can access the object at any one time. Then what kind of situation will create a deadlock?
There are two typical deadlock scenarios:
(a) The thread itself locks itself
In general, if the same thread calls lock two times, when the second ⽤ is used, because the lock is already occupied, the thread hangs the thread that waits for the lock to release the lock, but the lock is occupied by itself, and the thread is suspended without the opportunity to release the lock, so it is always in a pending state. Thus a deadlock (deadlock) is formed.
(ii) multithread preemption lock Resource trapped
And as thread A gets the lock 1, thread B gets the lock 2, when thread A calls lock to get lock 2, the result is to suspend wait thread B to release lock 2, while thread B calls lock to try to get lock 1, the result is to suspend wait thread A to release the lock 1, and both threads A and B are always in a pending state. The deadlock is formed again.

Ii. Deadlock in computer systems

1. Resource classification
(i) Reusable resources and expendable resources
Reusable resources: Resources that users can reuse multiple times.
Properties:
1 The units in each reusable resource can only be allocated to a single process (or thread) and not multiple processes (or threads) are allowed to be shared.
2 The use order of reusable resources:
Request Resource--> Use resource--> to release resources
3 The number of units in each type of reusable resource in the system is relatively fixed, and the process (or thread) cannot be created or deleted during run time.

Expendable resources: Also known as temporary resources, are created and consumed dynamically by processes (or threads) during the runtime.
Properties:
1 The number of units per category of expendable resources can be constantly changing during process (or thread) runs, sometimes 0.
2 process, or thread, in the course of running, you can constantly create units of expendable resources and put them into the buffer of the resource class to increase the number of cells in the resource class.
3 a process (or thread) can request several expendable resources during the run, and the process (or thread) 's own consumption no longer returns them to the resource class.
A consumable resource is typically created by a producer process (or thread) and consumed by a consumer process (or thread).

(ii) preemption resources and non-preemptive resources
preemption resource: ☞ a process (or thread) that can be preempted by another process (or thread) or system after the resource is obtained.
Both CPU and main memory belong to the resource of preemption.
Non-preemptive resources: ☞ System Once a resource is assigned to the process (or thread), it cannot be forcibly retracted and can only be freed after the process (or thread) is exhausted.
Tape drives, printers, and so on are all non preemptive resources.

2, cause of deadlock
(i) Competition cannot preempt resources cause deadlock
such as: Cause deadlock when sharing files

The system has two processes P1 and P2, all of which are ready to write two files F1 and F2. Both of these are reusable and not preemptive resources. If the process P1 open the F1, the P2 process opens the F2 file, and when P1 wants to open the F2 because the F2 is occupied and blocked, when P2 wants to open 1 o'clock because the F1 has been occupied and blocked, then the wireless wait for a deadlock.

(ii) Competition for expendable resources leads to deadlock
Such as: deadlock caused by process communication

The system has three processes P1, P2 and P3,m1, M2, and M3 are 3 consumable resources. The process P1 the message M1, sends it to the P2, and receives the message M3 from the P3 on the other hand. And the process P2 on the one hand produces the message M2, sends it to the P3, on the other hand receives the message M1 from the P1. Similarly, the process P3 the message M3, sends it to the P1, and receives the message m2 from the P2 on the other hand.
If all three processes send their own messages and receive messages from others, they can run smoothly without a deadlock, but if three processes receive messages from others without generating messages, they will wait forever, creating deadlocks.

(iii) Improper progression of process causes deadlock


In the above diagram, if the order of 1 is advanced, two processes can be successfully completed. If the sequence of 2 is advanced, two processes can be successfully completed, and if the sequence of 3 is advanced, two processes can be successfully completed; If you advance in the order of Curve 4, two processes will enter unsafe zone D, at which point P1 maintains resource R1, P2 keeps the resource R2, the system is in an unsafe state, and if you continue to push forward, you may have a deadlock.

Definition, necessary condition and treatment method of deadlock

1, definition of deadlock: if each process (or thread) in a group of processes (or threads) waits for an event to be raised only by another process (or thread) in that group of processes, the group process (or thread) is deadlocked (deadlock).
2, the production of life and death lock requirements
(1) mutually exclusive conditions. The resource requested by a process (thread) can only be occupied by a process (thread) lock for a period of time.
(2) Request and maintain conditions. A process (thread) already has at least one resource, but a new resource request, which is occupied by another process (thread).
(3) Non-preemptive conditions (inalienable conditions). A process (thread) has acquired a resource that cannot be preempted before it is used.
(4) Loop wait condition (loop wait condition). In the event of a deadlock, there must be a process (thread)--a circular chain of resources.
3, the method of dealing with deadlock
(1) Prevent deadlock. Destroys one or more of the necessary conditions for the deadlock to occur. Note that mutually exclusive conditions cannot be broken, otherwise the result is not reproducible.
(2) Avoid deadlock. In the process of resource matching, the system is prevented from entering the unsafe zone.
(3) Detect deadlock. The detection mechanism detects the occurrence of deadlocks and then takes appropriate measures to unlock the deadlock.
(4) Unlock the deadlock. After the detection mechanism detects the deadlock, take appropriate measures to remove the deadlock.

Iv. using Banker's algorithm to avoid deadlock

1, the banker algorithm in the data structure
(1) available resource vector available[m]. M is the number of resource types in the system, if the vector available[j] = k, then the RJ-type resource in the system is represented by K.
(2) Maximum demand matrix max[n][m]. M is the number of resource types in the system, n is the number of processes (threads) running in the system, if max[i][j] = k, then process I requires the maximum number of RJ-class resources to be K.
(3) allocation matrix Allocation[n][m]. M is the number of resource types in the system, n is the number of processes (threads) running in the system, and if allocation[i][j] = k, the process I currently has the number of RJ-type resources as K.
(4) demand matrix Need[n][m]. M is the number of resource types in the system, n is the number of processes (threads) running in the system, if need[i][j] = k, then process I also needs the RJ class resource K.
The relationship between the above three matrices:
NEED[I][J] = max[i][j]-ALLOCATION[I][J]

2. Banker algorithm
The request (i) is the requested vector of the process pi, if request (i) [j] = k, which indicates that process PI requires a K-RJ type of resource.
(1) If Request (i) [j] <= Need[i][j], the steering step (2).
(2) If request (i) [j] <= Available[j], the steering step (3).
(3) The system tries to divide the resources into the process PI.
AVAILABLE[J] = available[j]-Request (i) [j];
ALLOCATION[I][J] = Allocation[i][j] + Request (i) [j];
NEED[I][J] = need[i][j]-Request (i) [j];
(4) The system executes the security algorithm to check whether the system is in a safe state after this resource allocation.

3, Security algorithm
(1) sets two vectors:
1 work Vector work[m], which indicates the number of resources the system can provide to the process to continue running, the initial value work = Available.
2 Finish: It indicates whether the system has enough resources allocated to the process to run it. Finish[i at start] = False, finish[i] = True when sufficient resources are allocated to the process. The
(2) finds a process (thread) that satisfies the following criteria from the process (thread) collection.
1 Finish[i] = False
2 need[i][j] <= work[j], if you find go to step 3, you can't find go to step 4.
3 "work[j] = Work[j] + allocation[i][j];
Finish[i] = true;
Go to step 2;
4 If all processes (threads) of finish[i] = True are satisfied, the system is in a safe state, whereas the system is unsafe.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.