Causes and lifting of deadlocks

Source: Internet
Author: User
Tags mutex

definition of deadlock

If each process in a group of processes waits for a time that can only be raised by another process in the group process, the group process is deadlocked.

the necessary conditions for creating a deadlock Mutual exclusion: Simply put, the resource that a process robs must be a critical resource, and within a period of time, the resource can only be held by a process for both request and retention conditions : When a process holds a (or more) resource, When you apply for additional resources, you find that the requested resource is held by another process and the current process is blocked, but it is not a resource that is not preempted by its own resources : The resources that the process has acquired cannot be preempted by other processes when they are not used : When a deadlock occurs, there must be a process-resource cycle chain

The resources mentioned here not only include hardware resources or other resources, also includes locks, locks are also a resource, lock contention can also lead to deadlock deadlock in several examples (for good description, here with the lock as a resource to describe the deadlock, where the lock to replace the resources are completely no problem)

Self-deadlock, simply saying that a process holds a lock and then applies the lock within the critical section, it will have to wait for the lock to be released, but because it is waiting for the lock itself, there will never be a chance to release the lock and get the lock, and the end result is a deadlock. Because many locks are not recursive locks, do not attempt to request the same lock multiple times within a thread.

Abba deadlock (damned locks often occur in multiple processes), simply put, each process holds locks that other processes want to acquire, pictured above

This most classic example when the philosopher dining problem (no more said, the basic principle is above and above the ABBA deadlock, you can Baidu)

The method of deadlock is divided into several major categories to prevent deadlock

The way to prevent deadlocks is to destroy the four necessary conditions of deadlock, as long as the condition is broken, the deadlock will not produce, simply describe the idea of destroying four conditions

breach of request and retention conditions:

1. All processes must have access to all resources at once and, if not fully, to release the resources already obtained, pending the commencement of operations;

2. All processes begin running with only the resources needed for initial operations, and then continually request new resources while they are running, freeing up resources that have been exhausted.

In contrast to the first, the second approach would be more resource-saving and not wasteful (since the first might have a resource that is only a small one at the end of the process, but it is consumed from start to finish, is inefficient to use), and reduces hunger in the process.

destroy the non-preemption conditions:

Simple to say, as long as a process requests a resource, but the application is not available, you must release all the resources that have been requested. But it's complicated, and it costs a lot to join the process already holding a printer like this (or some other necessary continuous work), failing to apply for other resources, and having to release the printer resources, but it has been used for a while. Releasing the printer resource at this time is likely to result in discontinuous information that is running twice with the printer (not getting the right results)

To destroy a looping wait condition:

Set up a rule to allow the process to obtain resources in a certain sequence of applications, can not violate the rules of this order. Must be in order to apply and release, you want to apply the resources must first be the resources before the resource full request, want to request the resources before the resource must first the resources (if obtained) all released

breach of mutex conditions:

Can not be destroyed, is the nature of the resource itself caused by the avoidance of deadlock

The most commonly heard algorithm to attack. The banker algorithm is coming.

The data structure required by the banker algorithm: 1. Available resource vectors (Available); 2. Maximum Demand matrix (max); 3. Allocation matrix (allocation); 4. Requirements Matrix (Need).

The above three matrices have the following relationship need[i,j]=max[i,j]-allocation[i,j];

Said seemingly difficult to understand the appearance of the following detailed understanding of the very good.


Available resource vectors (Available): So to speak, for example, there are currently three resources a,b,c, the available resource vector is an array (in order to use in the program), we understand that ABC each have how many resources, examples

A B C

7 2 5

Here 7,2,5 the number of three resources together is an array (vector)

Max requirement matrix (max): The matrix is designed to be easy to use when programming (is a two-dimensional array), we understand that the process is the work of the total number of resources required, examples

A B C

P1 1 1 1

In this way, it is clear that the process P1 need three kinds of resources each (this is not a two-dimensional array ah, is one-dimensional.) How could there be only one process? This is a solution to a process-thinking deadlock. and add a few more processes to the two-dimensional array.

Allocation matrix (Allocation): similar to the requirements matrix, only the meaning of the value has become, now to the process So-and-so (row value i), the allocation of the number of So-and-SO (column value J) resources

Requirements Matrix (Need): According to the formula above, this is how much resources are currently required to run the process, and the largest demand is not the same, the maximum demand is a total number of requirements.

With these we can use the banker algorithm to do what we use the algorithm for. is to avoid deadlocks, which means that we use these data structures to infer whether it is safe to execute a process queue in some order (whether it causes a deadlock)

The diagram above is a simple diagram example, for the computer program to run conveniently, we generally assume that from the P0 process to start running, then give P0 allocate enough resources to run (that is, need are given, if the current number of available resources enough to the process need), Then compute the Available (new) =available (old) +allocation (P), where allocation is the assigned resource before the execution of the process is completed and naturally receives Available.

Topics are generally given to the table above, and then let you write a security sequence, with the current available to see which process to meet the need and then execute it, after the completion of the recovery (add to the available) the process of allocation can be, This step-by-step count to the end if the available has been counted down there is no loss is not enough to prove that the sequence is a security queue ~.

detection and release of deadlock

A simple way to detect deadlocks is by using a banker-like algorithm

Deadlock Release:

1. Terminate the process (simple and rough), literally, you die, I will kill you together, the disadvantage is that if a process runs for a long time, but was killed, still have to start from the beginning.

2. Terminate the process one after another, in some order, kill the process, each kill a process to see the deadlock has not been lifted (every kill a process will release some resources, if the release of good rough resources to solve the deadlock problem, there is no need to kill innocent again), did not lift to continue to kill.

The second way is obviously a lot of humanization, but in a certain order is very hazy, here is a sort of order is the deadlock cancellation algorithm, there are many, here no longer repeat.


Attention:

When writing a program, you should try to avoid getting multiple locks at the same time, if it is necessary to do so, there is a principle: if all threads need more than one lock in the same order (usually in the order of the address of the mutex variable) to get the lock, there will be no death. If a program uses the lock 1, lock 2, lock 3, their corresponding mutex variable address is the lock 1< lock 2< Lock 3, then all threads need to obtain 2 or 3 locks at the same time should be the lock 1, lock 2, lock 3 of the order obtained. If it is difficult to determine a sequence for all the locks, you should try to use the Pthread_mutex_trylock call instead of the Pthread_mutex_lock call to avoid deadlocks

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.