Deadlock definition, causes, requirements, methods to avoid deadlocks and unlock deadlocks

Source: Internet
Author: User
Tags mutex

1. Deadlock: If each process in a group of processes waits for an event that can only be raised by another process in the group process, the group process is deadlocked.

2. Causes of deadlock:

(1) Competition can not be preemptive resources.

(2) competition can consume resources.

When a system is shared by several processes such as printers, public queues, and so on, the number of resources is not sufficient to meet the needs of the process, it will cause the process of competition for resources to create deadlock.

(3) The sequence of process advancement is improper.

When a process is running, the order in which resources are requested and freed is not the same, resulting in a process deadlock.

If the system has sufficient resources, the process's resource requests can be met, the likelihood of deadlock is very low, otherwise, because of the contention for limited resources and fall into deadlock. Second, the process is running in a different order than the speed, may also produce deadlock.

A thread can also cause a deadlock.

3. Four necessary conditions for deadlock:

(1) Mutually exclusive condition: A resource can only be used by one process at a time.

(2) Request and retention conditions: a process that is blocked by requesting resources, keeps the resources that have been acquired.

(3) cannot preempt the condition: the resources which the process has obtained, can not be forcibly deprived before the end use, can only be released by itself when the process is finished using.

(4) Cyclic waiting condition: the process of forming a kind of end-to-end circular waiting resource relationship between several processes.

These four conditions are necessary for deadlock, as long as the system is deadlocked, these conditions must be set up, and as long as one of the above conditions is not satisfied, the deadlock will not occur. So you can write down the following methods to prevent deadlocks.

4. Methods to avoid deadlocks:

(1) Destroy the "mutually exclusive" condition: is in the system to eliminate mutual exclusion. If the resource is not used exclusively by a process, then the deadlock is definitely not going to happen. However, the general "mutually exclusive" conditions cannot be broken. Therefore, in deadlock prevention it is primarily to destroy the other three necessary conditions without involving the breach of the "mutually exclusive" condition.

(2) Break the "request and hold" condition: the process is not allowed in the system to request additional resources if a resource has been obtained. The idea is to devise a way to prevent the process from requesting additional resources while holding resources.

Method One: All processes must request all the resources required throughout the run before running. As a result, the process will no longer request resource requests during the entire run, destroying the request condition. When allocating resources, the system waits for a resource that does not meet the requirements of the process, even if all other required resources are idle and not assigned to the process. Because the process did not occupy any resources during the wait period, the "keep" condition was broken.

Advantages of this method: simple, easy and safe.

Disadvantages: A. The resource is seriously wasted, which seriously worsens the utilization rate of resources.

B. Making the process of hunger often occurs.

Method Two: Before each process presents a new resource request, it releases the resources it occupies. Thus, when a process needs resource s, it must first release its previously owned resource R before it can make a request for S, even though it may soon have to use resource R.

(3) to destroy the "not preemption" conditions: to allow the implementation of resources to rob.
Method One: If a process that occupies some resources makes a request for further resources rejected, the process must release the resources it originally possessed and, if necessary, request those and additional resources again.
Method Two: If a process requests a resource currently occupied by another process, the operating system can preempt another process and require it to release resources. This method can prevent deadlocks only if the priority of any two processes is not the same.

(4) Break the "cycle wait" condition: all resources in the system are uniformly numbered, and the process can request resources at any time, but all applications must be submitted in the order of the resource's number (ascending). This will ensure that the system does not appear deadlocked.

Using banker algorithms to avoid deadlocks:

Banker algorithm:

Set process I to request request[j], the banker algorithm is judged according to the following rules.

(1) If REQUEST[J]≤NEED[I,J], the steering (2) is considered an error, because it requires more than the maximum number of resources it announces.

(2) If the request[j]≤available[j], then the steering (3), otherwise there is not enough resources, pi to wait.

(3) If the application for process I has been approved, the system status is modified:

Available[j]=available[j]-request[i]

ALLOCATION[I,J]=ALLOCATION[I,J]+REQUEST[J]

NEED[I,J]=NEED[I,J]-REQUEST[J]

(4) The system performs security checks, such as safety, then the allocation is set up; otherwise, the exploration allocation is void, the system is reinstated, and the process waits.

Security algorithm:

(1) Set two working vectors work=available;finish[i]=false

(2) A process is found from the collection of processes that satisfies the following conditions,

Finish [I]=false;

NEED[I,J]≤WORK[J];

If found, execute (3); otherwise, execute (4)

(3) The process obtains resources and can be implemented smoothly until completion, thereby releasing resources.

WORK[J]=WORK[J]+ALLOCATION[I,J];

Finish[i]=true;

Go to step 2;

(4) If all processes are finish[i]=true, the system is safe;

5. Termination of the deadlock:

Once the deadlock is detected, the appropriate measures should be taken immediately to unlock the deadlock. The main two ways to unlock a deadlock are:

1) preemption Resources. A sufficient number of resources are preempted from one or more processes to be assigned to the deadlock process to unlock the deadlock state.

2) Terminate (or revoke) the process. Terminates (or revokes) one or more deadlock processes in the system until the loop is broken, freeing the system from the deadlock state.

Summarize:

In general, if the same thread calls lock two times, at the second call, because the lock is already occupied, the thread suspends waiting for the other thread 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, which is called a deadlock ( Deadlock). Another ⼀ a typical deadlock scenario is this: thread A gets the lock 1, thread B obtains the lock 2, when thread a ⽤ uses lock to attempt to obtain the lock 2, the result is to suspend waits for thread B to release the lock 2, but the thread B also ⽤ the lock to attempt to obtain the locking 1, the result is to suspend waits for the thread A to release the lock 1, So threads A and B are always in a pending state.

Attention:

When writing a program, you should try to avoid acquiring 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 (most commonly in the address order of the mutex variable), there will be no deadlock. For example, a program used to lock 1, lock 2, lock 3, their corresponding mutex variable address is the lock 1< lock 2< Lock 3, then all the threads need to get 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 pthread_mutex_trylock the call instead of the Pthread_mutex_lock call to avoid deadlocks.

The cause and necessity of deadlock and the method of preventing deadlock and the detection and release of deadlock

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.