Analyze the conditions and causes of deadlock in detail, and analyze the deadlock Conditions

Source: Internet
Author: User

Analyze the conditions and causes of deadlock in detail, and analyze the deadlock Conditions

I. Definition

Deadlock: every process in the set is waiting for events that can only be triggered by other processes in the set, so the process in this group is deadlocked.

Because resource usage is mutually exclusive, after a process applies for resources, the relevant process will never be allocated with necessary resources and cannot continue to run without external assistance, this produces a special deadlock phenomenon.

Ii. Conditions for deadlock

1) mutex conditions:A process schedules the allocated resources. That is, a resource is occupied by only one process within a period of time. If there are other processes requesting resources at this time, the requester can only wait until the process occupies the resources

Release after use.

2) request and retention conditions:This means that a process has maintained at least one resource, but has made new resource requests, and the resource has been occupied by other processes. At this time, the request process is blocked, however, you cannot release other resources that you have obtained.

3) Non-deprivation conditions:Resources obtained by a process cannot be deprived before they are used up. They can only be released by themselves when they are used up.

4) loop wait conditions:When a deadlock occurs, there must be a process-a circular chain of resources, that is, the process set {P0, P1, P2 ,..., p0 in Pn} is waiting for resources occupied by P1; P1 is waiting for resources occupied by P2 ,......, Pn is waiting for resources occupied by P0.

Iii. Causes of deadlocks

1. resource Competition leads to process deadlocks. When the number of resources in the system shared by multiple processes, such as printers and public queues, is insufficient to meet the needs of various processes, it will lead to resource competition by various processes and lead to deadlocks. 1) Resource deprivation and Resource deprivation

Resources in the system can be divided into two categories. One is the deprivation of resources, which means that a process can be deprived by other processes or systems after obtaining such resources. For example, high-priority processes can be deprived of the processor of low-priority processes. Another example is that a process can be moved from one storage area to another by a memory management program, which denies the original storage area occupied by the process, you can even transfer a process from memory to external memory. It can be seen that both the CPU and main memory are detachable resources. Another type of PU resources are non-deprived resources. When the system allocates such resources to a process, it cannot be forcibly withdrawn and can only be released after the process is used up, such as a tape drive or printer.

2) competition cannot deprive resourcesResources configured in the system cannot be deprived. because the number of resources cannot meet the running needs of various processes, the process will be in a deadlock due to competition for these resources. For example, there is only one printer R1 and one tape drive R2 in the system, which can be shared by processes P1 and P2. If PI occupies the printer R1 and P2 occupies the drive R2, P2 will be blocked if P2 continues to require the printer R1. If P1 requires the drive, P1 will also be blocked. As a result, there is a deadlock between P1 and P2. Both processes are waiting for the other side to release the resources they need, however, they cannot continue to advance because they cannot continue to obtain the resources they need, and thus cannot release the resources they own, resulting in a deadlock. 3) Temporary Resource CompetitionThe printer resources mentioned above are sequential and reusable resources, which are called permanent resources. There is also a so-called temporary resource, which is a resource generated by a process and used by another process, which is useless in a short time. It is also called a consumable resource, such as hardware interruptions, signals, messages, messages in the buffer zone, and so on, it may also cause deadlocks. For example, S1, S2, and S3 are temporary resources. Process P1 generates the message S1 and needs to receive the message S3. process P3 generates the message S3, it also needs to receive the message S2 from process P2; process P2 generates the message S2, and it also needs to receive the generated message S1 from process P1. If the message communication is performed in the following order: P1 :... relese (S1); Request (S3 );... p2 :... relese (S2); Request (S1 );... p3 :... relese (S3); Request (S2 );... A deadlock is not possible. But if it is changed to the following running sequence: P1 :... request (S3); Relese (S1 );... p2 :... request (S1); Relese (S2 );... p3 :... request (S2); Relese (S3 );... A deadlock may occur. 2. A deadlock is caused by improper process Promotion order because the process is asynchronous during operation, which may push the P1 and P2 processes forward in the following two order. 1)The process Promotion order is valid when processes P1 and P2 are concurrently executed, if they are pushed in the following order: P1: Request (R1); P1: Request (R2); P1: Relese (R1 ); p1: Relese (R2); P2: Request (R2); P2: Request (R1); P2: Relese (R2); P2: Relese (R1 ); the two processes can be successfully completed. it is legal to push the process in a sequence that does not cause deadlocks. 2)The process Promotion order is invalid. If P1 retains resource R1, P2 retains resource R2, and the system is insecure, because the two processes are pushed forward, a deadlock may occur. For example, when P1 runs to P1: Request (R2), it is blocked because R2 is occupied by P2. When P2 runs to P2: Request (R1, it will also be blocked because R1 is occupied by P1, so a process deadlock occurs. Iv. Handling Methods

After a deadlock occurs in the system, the deadlock should be detected in time and appropriate measures should be taken to remove the deadlock. 1) prevent deadlocks.This is a relatively simple and intuitive method of pre-prevention. The method is to set certain conditions to destroy one or more of the four conditions necessary to generate a deadlock to prevent deadlock. Deadlock Prevention is an easy-to-implement method and has been widely used. However, due to the strict restrictions imposed, system resource utilization and system throughput may be reduced. 2) avoid deadlocks.This method is also a pre-prevention policy, but it does not need to take various restrictions in advance to damage the four necessary conditions for deadlock, but in the process of dynamic resource allocation, some method is used to prevent the system from entering an insecure state, so as to avoid deadlocks. 3) detect and remove deadlocks.First check: This method does not have to take any restrictive measures in advance, and does not have to check whether the system has entered the unsafe zone. This method allows the system to issue a life-and-death lock during operation. However, the detection mechanism set up by the system can detect the occurrence of deadlocks in a timely manner and accurately determine the processes and resources related to deadlocks. Detection methods include timed detection, low-efficiency detection, and process-waiting detection. Then remove the deadlock: Take appropriate measures to clear the deadlock from the system. This is a complementary measure to the Deadlock Detection. When a dead or dead lock is detected in the system, the process must be freed from the deadlock state. A common implementation method is to undo or suspend some processes to recycle some resources, and then allocate these resources to the blocked processes so that they are ready to continue running. Deadlock Detection and removal measures may make the system obtain better resource utilization and throughput, but the implementation is also the most difficult. What is the difference between A shared lock and an exclusive lock: If transaction T adds A shared lock to data A, other transactions can only add A shared lock to A, but cannot apply an exclusive lock. Transactions authorized to share locks can only read data and cannot modify data. Exclusive lock: If transaction T adds an exclusive lock to data a, other transactions cannot add any type of blocking to data. Transactions authorized to exclusive locks can read and modify data. Other users under the shared lock can read and query data concurrently. But it cannot be modified, added, or deleted.

Zookeeper

Zookeeper

Zookeeper
Which is the correct cause of the deadlock?

1) mutex condition: A resource can only be used by one process at a time.
(2) request and retention conditions: when a process is blocked by requesting resources, it will not release the obtained resources.
(3) Non-deprivation condition: the resources obtained by the process cannot be forcibly deprived before they are used.
(4) Cyclic waiting condition: a cyclic waiting resource relationship is formed between several processes that are connected at the beginning and end.

These four conditions are necessary for a deadlock. As long as a deadlock occurs in the system, these conditions must be met, and
No deadlock will occur if it is not met.

So the first four are correct.

What are the necessary conditions for a computer operating system to generate a deadlock?

The cause of the deadlock is as follows: (1) due to insufficient system resources. (2) The order in which the process is promoted is inappropriate. (3) improper resource allocation. If the system resources are sufficient, all process resource requests can be satisfied, and the possibility of deadlock is very low. Otherwise, a deadlock will occur due to competition for limited resources. Second, the process may run in different order and speed, and may also lead to deadlocks. Four Conditions for deadlock: (1) mutex: A resource can only be used by one process at a time. (2) request and retention conditions: when a process is blocked by requesting resources, it will not release the obtained resources. (3) Non-deprivation condition: the resources obtained by the process cannot be forcibly deprived before they are used. (4) Cyclic waiting condition: a cyclic waiting resource relationship is formed between several processes that are connected at the beginning and end. These four conditions are necessary for deadlocks. As long as a deadlock occurs in the system, these conditions must be true.

In the operating system, several processes run concurrently, and they continuously apply for, use, and release system resources.
Process coordination and communication institutions will control them, but several processes may wait for each other to release resources.
Continue to run, otherwise it will be blocked. At this time, no one can release resources without external factors, and no one can resolve them.
In addition to blocking status. According to this situation, the deadlock in the operating system is defined as two or more processes in the system for an indefinite period.
Wait for the conditions that will never happen, and the system is stuck. This is a deadlock.

Deadlock relief and prevention:
Understanding the cause of the deadlock, especially the four necessary conditions for deadlock, can avoid, prevent, and
Remove the deadlock. Therefore, in terms of system design and process scheduling, pay attention to how to prevent these four necessary conditions from being established and how to ensure
Set reasonable resource allocation algorithms to avoid permanent occupation of system resources by processes. In addition, the process should be prevented from waiting.
. Therefore, reasonable planning should be made for resource allocation.

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.