Operating System Exam Review—Chapter 3 Priority Inversion Deadlock Problem

The current OS widely uses priority scheduling algorithms and preemption methods. However, there are resources in the system that affect the running of the process, which may cause "priority inversion" .

The specific explanation is: In the original scheduling algorithm design, high-priority processes can seize low-priority CPU resources and execute high-priority tasks first. However, there may be situations where a high-priority process cannot be completed and needs to wait for a low-priority process to complete before it can be executed. This phenomenon is very dangerous and harmful in real-time systems, so we must avoid it.

How does this situation arise? Suppose there are three completely independent processes P1, P2, and P3. P1 has the highest priority and decreases in sequence , and P3 has the lowest priority. Both P1 and P3 share a critical resource . When P3 is executed first, only the P operation is performed but the V operation is not performed to release critical resources. However, P2 preempts the processor of P3 at this time, and P3 cannot be executed. When P1 wants to execute, although the processor occupied by P2 can be preempted, the critical resources occupied by P3 cannot be obtained. Therefore, you need to wait for the execution of P2 to end and then the execution of P3 to release critical resources. Then P1 can be executed.

In layman’s terms:

The worst case of priority inversion is that a high-priority task is urgent, but its resources are already occupied by a low-priority task, and there are many long-running medium-priority tasks before the low-priority task. level tasks.

For the priority scheduling mechanism designed for the urgent importance of tasks, the situation caused by priority inversion is a trampling on the priority scheduling itself.

It's fair, error-free, and by the rules, but the consequences are bad.
Methods to solve priority inversion include: priority inheritance method .

The priority inheritance method is an effective method to solve the priority inversion problem, and its "inheritance" idea is actually not complicated. In short, priority inheritance is to temporarily increase the priority of a low-priority task, allowing the CPU to take priority and execute the low-priority task as soon as possible. When the execution is completed, its priority is lowered to the original priority and resources are released. , after the original high-priority task obtains the resources, it can seize the CPU and execute it.

 deadlock problem

Definition of deadlock problem: When a group of processes deadlocks, each process in the group of deadlock processes is waiting for the resources occupied by another deadlock process. Or the event that each process is waiting for. It is other processes in the group that release the resources they occupy.

A group of processes is deadlocked if each process in the group is waiting for an event that can only be raised by other processes in the group.

Four necessary conditions for deadlock to occur:

1. Mutual exclusion condition: A process uses the allocated resource exclusively, that is, within a period of time, a certain resource can only be occupied by one process. If there are other processes requesting resources at this time, the requesting process can only wait until the process of using the resource is released.

2. Request retention condition: The process has maintained at least one resource, but has made a new resource request, and the resource has been occupied by another process. At this time, the requesting process is blocked, but it will not let go of the resources it has obtained.

3. Non-preemption condition: The resources obtained by the process cannot be preempted before they are used up, and can only be released after the process has finished using them.

4. Circular waiting condition: When a deadlock occurs, there must be a process-resource circular chain.

For example: Process P1 has been allocated two R1 resources and requested an R2 resource; Process P2 has been allocated an R1 and an R2 resource and requested an R1 resource.

Deadlock detection

In order to detect whether a deadlock has occurred in the system, it is necessary

1. Use some kind of data structure to save resource request and allocation information;

2. Provide an algorithm that uses the above information to detect whether the system has entered a deadlock state.

Data structure: The resource allocation graph consists of a set of nodes and a set of edges .

Nodes are divided into resource nodes (corresponding to a type of resource, and a type of resource may include multiple) and process nodes (corresponding to a process).

The edges are divided into allocation edges (resource node->process node) and application edges (process node->resource node).

If the number of available resources remaining in the system is sufficient to meet the needs of the process, then the process will not be blocked for the time being and can be executed smoothly.

If the execution of this process ends and the resources are returned to the system, some processes that are waiting for resources may be activated and execute smoothly.

Correspondingly, these activated processes will return some resources after execution, which may activate other blocked processes.

If all edges can eventually be eliminated by analyzing according to the above process, the graph is said to be completely simplifiable. There must be no deadlock at this time (equivalent to finding a safe sequence)

If all edges cannot be eliminated in the end, then a deadlock occurs.

Algorithm for detecting deadlock:

1) In the resource allocation graph, find the process Pi that is neither blocked nor an orphan point (that is, find a directed edge connected to it, and the number of resource applications corresponding to the directed edge is less than or equal to the existing idle resources in the system Quantity. If all the edges connecting the process meet the above conditions, the process can continue to run until it is completed, and then release all the resources it occupies). Eliminate all its request edges and allocation edges to make it an isolated node.

2) The resources released by process Pi can be used to wake up some processes that are blocked waiting for these resources. The original blocked process may become a non-blocking process. After performing a series of simplifications according to the method in 1, if all the edges in the graph can be eliminated, the graph is said to be completely simplified. Then there is no deadlock

For the following resource allocation diagram:

 For P1, it requests resources from R2. Since there are two resources in R2 and only one is allocated, P1 is available to proceed. So we think that P1 can be released after running, so we delete the allocation edge and request edge of P1. Then we found that P2 has moved from the blocking state to the executable state, so P2 can also release its allocation edge and request edge. In the end, they are all isolated nodes, so it can be considered that there is no deadlock.

 Deadlock release

Basic methods to relieve deadlock:

1. Resource preemption method: suspend (temporarily put on external memory) certain deadlock processes, seize their resources, and allocate such resources to other deadlock processes. However, you should prevent the suspended process from starving without resources for a long time.

2. Cancel process method (or terminate process method):

One situation is to terminate all deadlock processes and the deadlock will be released. The advantage of this approach is that it is simple to implement, but the price paid may be high. Because some processes may have been running for a long time and are close to the end. Once terminated, it will be a failure and you will have to start all over again.

Another situation is to terminate the processes one by one in a certain order until there are enough resources to release the system from the deadlock state. However, the cost of this method is also very high, because every time a process is released, a detection algorithm must be used to detect whether it is released from the deadlock state. In addition, you also need to think about what strategy to use to select a process to terminate.

3. Process rollback method. Let one or more deadlocked processes back off enough to avoid deadlock. This requires the system to record historical information of the process and set restore points.
 

Guess you like

Origin blog.csdn.net/m0_53345417/article/details/130499782