Process Deadlock and workaround

Source: Internet
Author: User

process deadlock and workaround

First, the point of note

(1) grasp the concept of deadlock and the root cause of the birth and Death lock .

(2) understanding the necessary conditions for the creation of deadlocks-the following four conditions are also available: mutually exclusive conditions, non-preemption conditions, possession and application conditions, cyclic waiting conditions .

(3) remembering the general method of resolving deadlocks, mastering the basic idea of both the prevention of deadlocks and the avoidance of deadlocks.

(4) grasp the strategy of resource orderly allocation in the prevention strategy of deadlock .

(5) understand the concept of process safety sequence and understand the relationship between deadlock and safe sequence .

(6) understand the banker algorithm .

(7) understand the resource allocation diagram .

(8) understand the idea of deadlock detection and recovery .

 

Ii. Introduction of the content

There are many resources in a computer system that can be used by only one process at a time, such as printers, tape drives, I nodes of a file, and so on. In a multi-channel programming environment, several processes tend to share such resources, and a process requires more than one resource. In this way, there will be a number of processes competing for limited resources, and pushing the wrong order, thus constituting an indefinite cycle of waiting. This state is a deadlock. The deadlock phenomenon in the system not only wastes a lot of system resources, but also causes the whole system to crash, which brings catastrophic consequences. Therefore, the deadlock problem must be given high priority both theoretically and technically.

8.1 Concept of the deadlock

The deadlock is the short name of the process deadlock, which was first proposed by Dijkstra when he studied the banker algorithm in 1965. It is one of the most difficult problems to be dealt with in computer operating system and even concurrent programming. In fact, the deadlock problem exists not only in the computer system, but also in our daily life.

1. What is a deadlock

Let's take a look at one example of life: There is a bridge on a river, the bridge is narrow, can only hold a car through, can't let two cars parallel. If there are two cars A and b respectively by the bridge at both ends of the bridge, then for a car, it went through the road to the left side of the bridge (that is, a part of the resources), to cross the bridge must wait for the B car to make the right deck, at this time a car can not go forward; for B cars, It went through the bridge to the right of a section of the road (that is, a part of the bridge's resources), to the bridge must wait for a car to let out the left side of the bridge, at this time B car can not advance. Both sides of the car are not reversing, resulting in waiting for each other to let out the bridge deck, but who does not give way, will endlessly wait. This phenomenon is a deadlock. If the car compared to the process, the bridge as a resource, then the above problem is described as: Process a occupies resources R1, waiting for process B to occupy the resource RR; process B occupies the resource RR and waits for the resource R1 that process a occupies. and resource R1 and RRs allow only one process to occupy, that is, two processes are not allowed to occupy at the same time. As a result, two processes cannot continue to execute, and if no other measures are taken, the cycle waits indefinitely and a process deadlock occurs.

In a computer system, a deadlock may occur in hardware resources involving software. For example, there is only one CD-ROM drive and one printer in the system, one process occupies the CD-ROM drive, the printer is applied, the other process occupies the printer, and the CD-ROM is applied. As a result, two processes are blocked and can never be lifted on their own.

The so-called deadlock refers to the situation where multiple processes are waiting indefinitely for the resources they occupy. Obviously, if there is no external force, then the various processes involved in the deadlock will always be in a blocked state. As can be seen from the above example, the root cause of the deadlock is the limited resources and improper operation of the computer system. That is, one reason is that the resources provided by the system are too small to meet the requirements of the concurrent process for resources. The deadlock caused by this competitive resource is the core of our discussion. For example, a message is a temporary resource. At some point, process a waits for a message from process B, process B waits for a message from process C, and process C waits for a message from process a. The message was not reached, a,b,c three processes could not be pushed forward, and deadlock on process communication could occur. Another reason is that the sequence of process advancement is not appropriate for the resulting deadlock. Less resources may not necessarily create deadlocks. Just as two people cross the bridge, if two people have to first, the deadlock in the single-plank bridge will not retreat, it is inevitable that the competition resources to generate deadlock; But if two people go up front and see if there are any other people on the bridges, and when no one else is on the bridge, then the problem is solved. Therefore, if the programming is unreasonable, causing the process to advance the wrong order, there will be deadlock.

2. the necessary conditions for generating a deadlock

As can be seen from the above analysis, if there are four necessary conditions in the computer system at the same time, then there will be a deadlock. In other words, if one of the following four conditions is not present, the system will not have a deadlock.

〈1〉high mutually exclusive conditions. That is, a resource can only be occupied by one process for a period of time, and cannot be occupied by two or more than two processes simultaneously. Such an exclusive resource, such as a CD-ROM drive, a printer, and so on, must be actively released by the process that owns the resource before other processes can take possession of the resource. This is determined by the properties of the resource itself. If a single-plank bridge is an exclusive resource, the two sides can not simultaneously cross.

〈2〉 is not a preemptive condition. Before the resources obtained by the process are exhausted, the resource requester cannot forcibly seize the resources from the resource holders, but can only be released by the possessor process of the resource itself. If the person who has crossed the bridge can not force the other side to retreat, can not be illegal to push the other side, it must be the bridge of the people on their own bridges after the bridge (i.e. active release of possession of resources), the other person can cross the bridge.

〈3〉maximum occupancy and application conditions. The process already occupies at least one resource, but it also applies for new resources; Because the resource is already occupied by another process, the process is blocked at this time, but it continues to occupy the resources it has already occupied while waiting for new resources. Also take the cross-bridge as an example, a B two people met on the bridges. A through a section of the bridge (that is, the possession of some resources), but also need to take the rest of the bridge deck (to apply for new resources), but that part of the bridge deck was occupied by B (b through a bridge). A can not pass, forward cannot, and do not retreat, B is also in the same situation.

〈4〉maximum loop wait condition. There is a process waiting sequence {P1,P2,...,PN}, where P1 waits for a resource that the P2 occupies, P2 waits for a source that the P3 occupies, ..., while Pn waits for a certain resource that the P1 occupies, forming a process loop waiting loop. Like the front of the cross-bridge problem, a waiting for B to occupy the deck, and B and waiting for a occupy the bridge deck, so that each other to cycle waiting.

The four conditions we mentioned above will occur simultaneously at the time of deadlock. That is, a deadlock can be ruled out if one of the necessary conditions is not met.

8.2 Prevention of the deadlock

The four prerequisites for a deadlock are described earlier , and deadlocks do not occur as long as any one of these four prerequisites is destroyed. This provides the possibility for us to solve the deadlock problem. In general, the method of resolving deadlocks is divided into three types of deadlock prevention, avoidance, detection and recovery (note: Detection and recovery of deadlocks is a method). We will describe them separately below.

The prevention of deadlock is a strategy to ensure that the system does not enter the deadlock state. Its basic idea is to require the process to request resources to follow some kind of protocol, thus breaking the four necessary conditions to create a deadlock in one or several, to ensure that the system will not enter the deadlock state.

〈1〉high break mutually exclusive conditions. That allows the process to access some resources at the same time. However, some resources are not allowed to be accessed at the same time, like printers and so on, which is determined by the properties of the resource itself. Therefore, this method has no practical value.

〈2〉 break the non-preemptive condition. That allows a process to forcibly seize certain resources from the possessor. That is, when a process already occupies some resources and it applies for new resources, but cannot be satisfied immediately, it must release all the resources it occupies and reapply later. The resources it frees can be allocated to other processes. This is equivalent to the resources occupied by the process being covertly usurped. This method of preventing deadlocks is difficult to achieve and can degrade system performance.

〈3〉maximum to break possession and apply for conditions. Resource pre-allocation policies can be implemented. That is, the process requests the system all the resources it needs at once before it runs. If all the resources required by a process are not met, no resources are allocated and the process is not running. Only when the system is able to meet the full resource requirements of the current process will all of the requested resources be allocated to the process at once. Because a running process already occupies all of the resources it needs, it does not take possession of resources and request resources, so deadlocks do not occur. However, this strategy also has the following drawbacks:

(1) In many cases, it is impossible for a process to know all the resources it needs before it is implemented. This is due to the fact that the process is dynamic and unpredictable at the time of execution;

(2) Low resource utilization. No matter when the resource is used, a process can only be executed if it occupies all the resources it needs. Even though some resources are eventually used by the process, the process has been occupying them for the duration of its existence, resulting in a prolonged period of unused conditions. This is obviously a huge waste of resources;

(3) Reduce the concurrency of the process. Because of the limited resources, coupled with the existence of waste, the number of processes that can be allocated to all the required resources must be less.

(4) break the cyclic waiting condition and implement the strategy of allocating resources in an orderly manner. Using this strategy, the resources are pre-categorized, assigned by number, so that the process does not form a loop when it requests and consumes resources. All process requests for resources must be made strictly in the order in which they are incremented by the resource number. The process takes up a small amount of resources to request a large resource, and no loops are generated, thus preventing deadlocks. This strategy has a significant increase in resource utilization and system throughput compared to the previous strategy, but there are also the following drawbacks:

(1) Restricting the process to the resource request, at the same time to the system all resources reasonable number is also a difficult thing, and increase the system overhead;

(2) In order to follow the order of the number of applications, temporary unused resources also need to apply in advance, thus increasing the process of resource consumption time.

8.3 Avoidance of deadlocks

The above-mentioned deadlock prevention is a static policy that excludes deadlocks, which makes the four necessary conditions that generate deadlocks impossible to have at the same time, restricting the activity of a process requesting resources to ensure that deadlocks do not occur. Here we introduce the dynamic strategy of the exclusion of deadlocks-the avoidance of deadlock, which does not restrict the process's command of requesting resources, but rather dynamically checks each request Resource command issued by the process and determines whether to allocate resources based on the results of the check. That is to say, in the process of resource allocation, if the possibility of deadlock is predicted, it is avoided. The key to this approach is to determine the security of the resource allocation.

1. Safety Sequence

We first introduce the definition of a security sequence: the so-called system is secure, which means that all processes in the system can allocate resources in a certain order, and in turn, this process sequence {P1,P2,...,PN} is a safe sequence. If such a security sequence exists, the system is secure, and if the system does not have such a security sequence, the system is unsafe.

The security sequence {P1,P2,...,PN} is composed of: for each process pi, the additional resources it needs can be satisfied by the current available resources in the system plus all processes PJ currently occupies the sum of resources, then {P1,P2,...,PN} is a security sequence, then the system is in a secure state, Does not enter the deadlock state.

Although there must be no deadlock when there is a safe sequence, a deadlock may not occur when the system enters an unsafe state (the necessary conditions for four deadlocks occur simultaneously). Of course, after a deadlock occurs, the system must be in an unsafe state.

2. Banker Algorithm

This is a well-known algorithm for avoiding deadlocks, which is first proposed and solved by Dijstra.

[Background knowledge]

A banker's problem is how a banker can safely lend a certain amount of money to a number of clients so that they can borrow money to do what they want to do, and the banker will be able to recover all the money without going bankrupt. This problem is very similar to the resource allocation problem in the operating system: Bankers are like an operating system, clients are like running processes, and bankers ' money is the system's resources.

[Description of the problem]

A banker has a certain amount of money and a number of clients want to borrow. Each client is required to declare the total amount of the loan he needs at the outset. If the total amount of the client's loan does not exceed the banker's total funds, the banker can accept the client's request. Client loans are carried out in a single capital unit (such as 10,000 RMB, etc.), and the client may wait until the full amount of the required units is filled, but the banker must ensure that such wait is limited and complete.

For example: There are three customer c1,c2,c3, to the banker to borrow money, the banker's total funds of 10 units, of which C1 customers to borrow 9 units, C2 customers to borrow 3 capital units, C3 customers to borrow 8 capital units, a total of 20 capital units. A state of a moment.

C1 2 (7)
C2 2 (1)
C3 4 (4)
Balance 2
C1 2 (7)
C3 4 (4)

Balance 4

C1 2 (7)
Balance 8

Balance 10

(a)

(b)

(c)

(d)

Banker algorithm Schematic

For the state of a graph, according to the requirements of the safety sequence, the first customer that we choose should satisfy the loan that the customer needs is less than or equal to the current remaining money of the banker, it can be seen that only C2 customers can be satisfied: C2 customers need 1 capital units, small bankers in the hands of 2 units, So the banker lent the 1 units to the C2 client to complete the work and return the money borrowed by the 3 units into the B-chart. In the same vein, the banker loaned 4 units of money to C3 clients to get the job done, and in C, there was only one customer C1, which required 7 capital units, and the banker had 8 capital units, so C1 could borrow money and finish the job. Finally (see Figure D) Bankers recover all 10 capital units to ensure that they do not lose money. Then the customer sequence {C1,C2,C3} is a safe sequence, and the banker is safe in this sequence of loans. Otherwise, if in the state of Figure B, the banker to the hands of the 4 units lent to C1, then there is an unsafe state: then C1,C3 can not complete the work, and the banker hands and no money, the system into a stalemate, the bankers can not recover investment.

To sum up, the banker algorithm is starting from the current state, individually by the security sequence to check each customer who can complete their work, and then assume that they complete the work and return the full loan, and then check the next to complete the work of the customer, .... If all the customers are able to do their job, a security sequence is found and the banker is safe.

From the above analysis, we can see that the banker algorithm allows the mutually exclusive conditions of the deadlock condition, possession and application conditions, the existence of non-preemptive conditions, so that it is compared with several methods of preventing deadlock, the limited condition is less and the resource utilization degree is improved.

This is the advantage of the algorithm. The disadvantages are:

〈1〉high This algorithm requires that the number of customers remain fixed, which is difficult to do in a multi-channel program system.

〈2〉 This algorithm ensures that all customers are satisfied for a limited time, but the real-time customer demands a quick response, so consider this factor.

〈3〉maximum because of the need to find a safe sequence, it actually increases the overhead of the system.

 

8.4 Detection and recovery of deadlocks

Generally speaking, because the operating system has the characteristics of concurrency, sharing and randomness, it is difficult to eliminate the deadlock by means of prevention and avoidance. This requires significant overhead and does not make the best use of resources. A convenient way to do this is for the system to allocate resources for the process without taking any restrictive measures, but providing a means of detecting and unlocking deadlocks: The deadlock can be found and recovered from the deadlock state. Therefore, deadlock detection and recovery methods are often used in the actual operating system to eliminate deadlocks.

Deadlock detection and recovery refers to the system has a special mechanism, when the deadlock occurs, the mechanism can detect the location and cause of the deadlock, and can destroy the necessary conditions of the deadlock through external forces, so that the concurrent process from the deadlock state recovery.

1. enlarge watch >>)

A small example of a deadlock is shown in the figure. At this time the process P1 occupy the resources R1 and apply for resources R2, the process P2 possession of resources R2 and apply for resources R1, according to the cycle of waiting conditions, processes and resources to form a loop, so the system is a deadlock state. Process P1,P2 is a process that participates in deadlocks.

Now let's look at a look dead lock detection algorithm. The data structures used by the algorithm are as follows:

Occupy Matrix A:n*m Order, where n represents the number of concurrent processes, m represents the number of resources of the system, and this matrix records the current number of resources in each resource class that each process occupies.

Application matrix R:n*m Order, where n represents the number of concurrent processes, m represents the number of resources of the system, and this matrix records the number of resources in each resource class that each process currently needs to complete its work.

Idle vector t: records the number of idle resources in the current M resource class.

Completion vector F: The Boolean vector value is True (True) or False (false), recording whether the current n concurrent processes can be completed. To be true can be done, for the false will not be finished.

Temporary vector w: w:=t at the beginning.

Algorithm steps:

(1) W:=t,

For all the I=1,2,...,n,

If a[i]=0, then f[i]:=true; otherwise, f[i]:=false

(2) To find the following conditions to meet the subscript I:

F[i]:=false and R[i]〈=w

If the above condition I is not present, go to step (4).

(3) W:=w+a[i]

F[i]:=true

Go to step (2)

(4) If I,f[i]:=false is present, the system is in a deadlock state, and the PI process participates in the deadlock. When a deadlock is detected depends on how often the deadlock occurs. If the deadlock occurs at a high frequency, then the frequency of deadlock detection should be correspondingly improved, so as to improve the utilization of system resources, on the one hand can avoid more processes involved in the deadlock. If the process request resources can not meet the immediate detection, then the deadlock when the formation can be found, which is similar to the algorithm of deadlock avoidance, but the system overhead. To reduce the overhead of deadlock detection, a deadlock detection is usually performed at intervals, or when the CPU utilization is lowered to a certain value.

2. Recovery of deadlocks

Once a deadlock is detected in deadlock detection, the deadlock is eliminated and the system recovers from the deadlock state.

(1) The simplest and most commonly used method is to perform a system reboot, but this method is expensive, which means that all the computations that have been done before that process are lost, including those involved in the deadlock, and the processes that are not involved in deadlocks.

(2) Revoke the process and deprive the resources. Terminates the process that participates in the deadlock, reclaims the resources that they occupy, and thus unlocks the deadlock. At this point there are two situations: one-time undo all the processes involved in the deadlock, deprive all resources, or gradually revoke the process involved in the deadlock, gradually recover the resources occupied by the deadlock process. In general, the process of phasing out is chosen in accordance with certain principles, with the aim of undoing the least costly processes, such as determining the cost of the process by the priority of the process, taking into account factors such as the cost of the process runtime and the cost of the external job associated with the process.

In addition, there is a process fallback policy that lets a process that participates in a deadlock fall back to a point where there is no deadlock, and continue execution at this point, so that a deadlock no longer occurs in order to execute again. Although this is a better approach, but the operation of the system overhead, it is necessary to have the stack such a mechanism to record every step of the process changes, so that the future fallback, sometimes this is not possible.

Process Deadlock and workaround

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.