Deadlock and hunger

Source: Internet
Author: User
In a multi-channel Program System, multiple processes run concurrently to share system resources, which improves the utilization of system resources and the processing capability of the system. However, improper management, allocation, and use of resources may lead to deadlocks or hunger. The so-called deadlock refers to the resources in a multi-channel program system where every process army in a group of processes waits for another process in the group to occupy and never release. Hunger means that the system cannot guarantee the upper limit of the wait time of a process, so that the process can wait for a long time. When the wait time has a significant impact on the process progress and response, it is said that the process has become hungry. When the task assigned by a process from hunger to a certain degree is no longer practical, it is said that the process is starved to death. Next we will discuss the characteristics of deadlocks and hunger respectively.

 

First, deadlock.

There are two major causes of deadlocks: competition for resources, limited number of resources provided by the system, not meeting the needs of each process; and unreasonable process Promotion order when multiple programs run. It can be seen that there are at least two deadlocked processes in the case of a deadlock. We can give a simple example to learn about deadlocks:

 

P1

P2

...

...

Request ()

Request (B)

Request (B)

Request ()

Request (B)

Request ()

Request ()

Request (B)

 

If both parties have part of the resources (P1 owns A, P2 owns B, and A and B both have only one), but P1 also needs B, P2 also needs, so P1 and P2 will be in an infinite wait state, and a deadlock occurs.

In this example, we can summarize the necessary conditions for producing the Life and Death locks:

(1) mutex Conditions

Resources are exclusive and exclusive. That is, a resource can only be used by one process at any time. Other applicants can only wait until the resource is occupied and released. For example, resource A and resource B.

(2) conditions of deprivation

The resources obtained by the process cannot be forcibly deprived by other processes before they are used up. Instead, the resources can only be voluntarily released by the process that owns the resources. In this example, P2 cannot occupy the resources owned by P1, and P1 cannot occupy the B resources owned by P2.

(3) request and retention conditions

Each time a process applies for a part of the resources it needs, it continues to occupy allocated resources while applying for new resources. In this example, P1 continues to occupy resources A when applying for resources B, and P2 continues to occupy Resources B when applying for resources.

(4) Cyclic waiting Conditions

(5) When a deadlock occurs, there must be a process wait loop. The resources occupied by each process in the loop are applied by another process at the same time. In this example, P1 and P2 are simple wait loops.

Deadlock in the system not only wastes a lot of system resources, but may even cause the entire system to crash. Therefore, how to solve the deadlock problem is a key point in the operating system design. Currently, there are two main methods: one is to prevent deadlocks; the other is to prevent deadlocks and then solve them.

Deadlock Prevention is designed to prevent deadlocks from occurring. It aims to achieve the goal by destroying the "irrevocable" conditions, or the "request persistence" conditions, or the "cyclic waiting" conditions, however, this method imposes strong restrictions on the system, seriously affecting the system performance. To avoid deadlock, the system dynamically checks the resource applications that can be met by each system issued by the process and determines whether to allocate resources based on the test results, if the system may experience a deadlock after allocation, it will not be allocated; otherwise, it will be allocated.

This method is complex and consumes a lot of system time.

The detection and removal of deadlocks are the methods for preventing deadlocks and then solving them. The operating system constantly monitors the progress of the process. Once a deadlock is detected, special measures are adopted to lift the deadlock and restore the entire system to normal at the minimum cost.

The above describes the features of deadlocks and some solutions for deadlocks. Let's take a look at hunger:

 

The main cause of hunger is: in a dynamic system, for each type of system resources, the operating system needs to determine a allocation policy. When multiple processes simultaneously apply for a certain type of resources, the allocation policy determines the order in which resources are allocated to processes. Sometimes the resource allocation policy may be unfair, that is, the existence of the upper limit of the wait time cannot be guaranteed. In this case, some processes may wait for a long time even if the system does not have a deadlock. when the wait time has a significant impact on the process progress and response, it is said that the process is hungry, when the task assigned by a process from hunger to a certain degree is no longer practical, it is said that the process is starved to death. For example, if multiple processes need to print files, the printing task of long files will be postponed indefinitely due to the continuous arrival of short files, resulting in the final hunger and even starvation.

Hunger does not have the necessary conditions and is random. In addition, hunger can be eliminated, so the hunger that occurs during busy waiting is also called a live lock.

As hunger and hunger are related to resource allocation policies, to address hunger and hunger, we can consider the fairness of resource allocation policies to ensure that all processes are not ignored. Such as the time slice Rotation Algorithm (RR ). It divides the CPU processing time into time slices, and the processes in the ready queue run one time slice in turn. When the time slice ends, it forces the running program to let the CPU out, and the process enters the ready queue, wait for the next scheduling. At the same time, process scheduling selects a process in the ready queue and assigns it a time slice to be put into operation. Scheduling in turn in this way. In this way, the hunger problem can be solved without considering other system overhead.

 

Finally, let's compare the deadlock and hunger.

Deadlocks have some similarities with starvation: both are caused by competition for resources. However, there are significant differences:

(1) considering the process status, all deadlocked processes are in the waiting status, while busy waiting (in the running or ready status) processes are not in the waiting status, but may be starved to death;

(2) The deadlock process waits for resources that will never be released. The starved process waits for resources that will be released but will not be allocated to itself, there is no upper limit on the waiting time limit (waiting in queue or waiting in busy mode );

(3) A deadlock must have occurred in a loop wait, but not starved to death. This also indicates that the resource distribution graph can detect whether a deadlock exists, but cannot detect whether a process is starved to death;

(4) deadlocks must involve multiple processes, while hunger or starvation may have only one process.

(5) In the case of hunger, at least one process in the system can run normally, but the hunger process cannot be executed. The deadlock may eventually cause the entire system to be deadlocked and crashed.

 

In short, deadlock and hunger are important problems to be solved in the operating system. It is tempting and never outdated to invent a satisfactory and perfect solution.

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.