Thoughts on deadlocks [1]/thinking in deadlocks

Source: Internet
Author: User

Opening

Deadlock is an important concept in the operating system. Like other important concepts in the operating system, correct understanding of it will help you write more efficient and excellentProgram.

There is no obscure definition here. There are only easy-to-understand descriptions and vivid examples. I believe you will gain some benefits after reading it.

What is deadlock?

To put it simply, a deadlock means that the hardware or software resources in the computer are insufficient.

Why is this happening? For example, a occupies resource m and requests resource n. B occupies resource n request resource m. Both process a and process B will sleep because they cannot request their own resources.

The following is a further introduction.

More vividly-deadlock Modeling:

To better analyze deadlocks, we create a deadlock model. A directed graph is used to indicate the usage of processes and resources. circular nodes represent processes and square nodes represent resources.

The resource points to the edge of the process, indicating that the process occupies the resource, and the process points to the resource, indicating that the process requests to allocate resources.

 

 

 

A): process a occupies resource R. B) process B applies for resource s.

C): Process D occupies the resource T. Apply for the resource U.

Process C occupies resource U and applies for resource T.

(C) A deadlock occurs. Process C waits for resource T, resource T is occupied by process D, and process D has the resource U occupied by process C. In this way, the two processes will wait.

Deadlock Definition

With the above foundation, we can see the standard definition of deadlocks:

A set of processes is deadlocked if each process in the set is waiting for
Event that only another process in the set can cause.

Each process in a process set is waiting for events that can be triggered only by the process in the process set.

Conditions for deadlock:

1. mutex condition: each resource is either allocated to a process or available.

2. Occupancy and waiting conditions: other resources can be applied for by a process that already occupies resources.

3. Non-preemption condition: resources that have been occupied cannot be preemptible by other processes. Only processes that possess it can be displayed and released.

4. Loop waiting condition: When a deadlock occurs, two or more processes in the system have one loop. Each process in the loop is waiting for the resources occupied by the next process.

When a deadlock occurs, the above four conditions must be met at the same time. If one of the conditions is not met, the deadlock will not occur.

So, can the deadlock be avoided if the above conditions are not met? Yes, this will be discussed in the following deadlock prevention.

Deadlock Detection

Deadlock Detection for each type of resource:

Through the above description, we should have some knowledge about deadlocks. So how can we check whether a deadlock exists in a process set?

With the above experience, as long as every process in the process set and the resources occupied and requested are represented by the deadlock model just now, we can judge that if there is a circle,

The deadlock exists.

The instance can better illustrate the problem: there are seven processes from A to G and resources from R to W;

1. process a occupies resource R and requests resource s

2. process B does not possess any resources and requests resource T.

3. Process C does not occupy any resources and requests resource s.

4. d occupies resource U, request resource S and T

5. E has resource T and requests resource v

6. F has resources W and requests resources s

7.G has resource V and requires resource u

The preceding process set and resource relationship can be expressed as follows:

Intuitively, we can see that there is a circle in the figure, so there is a deadlock.

Let's take a simple analysis: starting with the D point, d requires T, T is occupied by E, E requires V, V is occupied by G, G requires U, and U is occupied by D. Every process in the circle is waiting for his next

All processes are waiting for resources without limit. Apparently, the above process set has a deadlock.

In this case, you can record the relationship between processes and resources in the process set and check whether there is a deadlock by judging whether there is a circle in the graph.

To determine whether a circle exists in the graph, you can use depth-first search for the graph. For more information, see the previous blog post: Deep-first search/depth-first search/C ++

You may have noticed that the resources we have discussed since the beginning have only one feature, that is, each type of resources has only one. In the preceding example, only one resource R is available,

S also has only one. Some computer resources may have only one, such as printers and scanners. There are many types of resources that can exist. Such as memory space and registers.

I analyzed the situation that only one deadlock exists for each type of resources. The following shows that there are multiple types of resources:

Deadlock Detection for multiple resources of each type:

In this case, another representation is required. For any problem analysis, we always need to express it first.

The following is a separate discussion of resources and processes;

Resource:

A resource can be either occupied by a process or occupied by an application. Assume that the resource type is 1, 2, and M.

The total number of resources is expressed by E1, E2, and em.

The remaining resources are represented by A1, A2, and am.

For example, if the resource type E2 represents the scanner, then e2 = 1 indicates that there is a scanner in the system. A2 = 0 indicates that the remaining scanner is 0;

Process: Assume that there are processes numbered 1, 2, and N in the process set.

We consider deadlocks. For a process, we need to record the resources it occupies and the resources it needs.

Here we use two matrices: the value of CIJ indicates the number of resource J occupied by process I. Rij indicates the number of resource j required by process I;

See:

Total number of resources = number of resources occupied by processes + number of remaining resources, expressed:

For any process I, if Ri1, ri2, and rim are smaller than the corresponding A1, A2, and am, this process can be executed, therefore, you can check whether there are any processes in the process set that can be executed, so that they can execute the processes and release the occupied resources. At this time, the remaining resources will increase. when determining whether there are any processes that can be executed in the process set, execute the processes and release the resources. Repeat the preceding process. If all the processes in the final process set are executed, there is no deadlock. Otherwise, if there are other processes that cannot be executed in the final process set, there is a deadlock in the process set. The theory is too boring and hard to understand. Use an instance to speak;

Let's first look at the resource and Process status diagrams:

There are four types of resources in the figure. The total number and the remaining number are represented by E and A respectively.

There are three processes in total. The resources they possess and the resources they need are represented by the matrix C and R respectively.

Process 1: two resources are required for the first type, one for the fourth type, and no for the fourth type. Therefore, process 1 cannot be executed first.

Process 2: one resource is required, and one resource is required. The remaining resources cannot meet the requirements, so they cannot be executed.

Process 3: two resources are required for the first type and one resource for the second type. The remaining resources can meet the request, so process 3 is executed.

After process 3 is executed, resources are occupied. Update A: (,), process 2 can be executed, and process 1 can also be executed, so there is no deadlock in the process set.

In the false state, the situation changes. process 2 requires one of the four types of resources, resource 2 and one or two. All requests are not met and the system enters a deadlock.

 

This blog post focuses on the definition of deadlocks and the detection of deadlocks,

The recovery of deadlocks, the avoidance of deadlocks, and the prevention of deadlocks will be described in the next blog.

 

If any reprint is available, indicate the source:

A fish @ blog garden http://www.cnblogs.com/yanlingyin/

2011-12-29

 

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.