Operating system (5) The concept of deadlock Necessary conditions for deadlock Deadlock processing strategy Prevent deadlock Avoid deadlock Deadlock detection and release banker algorithm

Deadlock

One, the concept of deadlock

1. The difference between deadlock, starvation, and endless loop

(1) Deadlock: Each process waits for resources in each other's hands, causing each process to be blocked and unable to move forward
(2) Starvation: If the desired resource is not available for a long time, a process cannot move forward
(3) Dead Cycle: A certain cycle cannot be jumped out of a certain process during execution
Insert picture description here

2. Necessary conditions for deadlock

The following four conditions must be met at the same time to produce a deadlock. If one of them is not met, the deadlock will not occur

(1) Mutual exclusion conditions: The competition for mutually exclusive resources will lead to deadlock
(2) Non-deprivation conditions: the resources obtained by a process cannot be forcibly taken away by other processes before being used up, but can only be released actively
( 3) Request and retention conditions: The process has retained at least one resource, but has made a new resource request, and the resource is occupied by other processes. At this time, the requesting process is blocked, but it does not retain its existing resources. Put
(4) cyclic waiting condition: there is a cyclic waiting chain of process resources, and the resources obtained by each process in the chain are simultaneously requested by the next process

Note: There must be cyclic waiting when deadlock occurs, but it may not be deadlock when cyclic waiting occurs

3. Deadlock processing strategy

(1) Prevent deadlock: destroy one or several of the four necessary conditions for
deadlock (2) Avoid deadlock: use some method to prevent the system from entering an unsafe state, thereby avoiding deadlock
(3) Deadlock Detection and release: Allow deadlock to occur, after the operating system detects the deadlock, it will take measures to remove the deadlock

  1. Knowledge review

Insert picture description here

Two, deadlock processing strategy-to prevent deadlock

Insert picture description here

Three, deadlock processing strategy-avoid deadlock

1. Introduction to safety sequence

The so-called security sequence means that if the system allocates resources according to this sequence, each process can be completed smoothly. As long as a security sequence can be found, the system is in a safe state (there may be multiple security sequences)

If after the resources are allocated, no safe sequence can be found in the system, the system enters an unsafe state, which means that all processes may not be able to execute smoothly afterwards

note:

(1) If the system is in a safe state, there will be no deadlock
(2) If the system is in an unsafe state, a deadlock may occur
(3) In an unsafe state, a deadlock may not occur, but when a deadlock occurs, it must In an unsafe state

2. Banker's Algorithm

Idea: Before resource allocation, judge whether this allocation will cause the system to enter an insecure state, and then decide whether to agree to the resource allocation request

Insert picture description here
Insert picture description here

Note: If the banker algorithm is a security algorithm, it means that the system is in a safe state and no deadlock will occur.

4. Deadlock processing strategy-deadlock detection and removal

1. Deadlock detection

In order to detect whether the system has a deadlock, you must:

(1) Use resource allocation diagram to save resource request and allocation information
(2) Provide an algorithm that uses the above information to detect whether the system enters a deadlock state

Insert picture description here

The P2 process requests 1 resource, but the 3 resources of R1 have been allocated and cannot be executed; the P1 process requests 1 resource, R2 has 1 remaining resource, and P1 executes; after P1 is executed, it releases all its own resources, P1’s All three edges are eliminated; the remaining resources of R1 can satisfy the P2 process; the P2 process is executed, and all the three edges of P2 are eliminated

note:

(1) If all the edges can be eliminated according to the above process, it means that the graph can be completely simplified and no deadlock will occur.
(2) If all the edges cannot be eliminated, it means that a deadlock has occurred.
(3) Those that are still connected to the edges Process is deadlocked

2. Removal of deadlock

Once the deadlock is detected, the deadlock should be released immediately. The methods to remove the deadlock are as follows:

(1) Resource Deprivation Law

Suspend some deadlocked processes and seize its resources, and allocate these resources to other deadlocked processes; however, the suspended process should be prevented from starving because of lack of resources for a long time

(2) Revocation process law

Forcibly revoke some or all deadlocked processes and deprive these processes of resources

(3) Process rollback method

Let the deadlock process roll back enough to avoid deadlock (this requires the system to record historical information and set a restore point)

3. Knowledge review

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49343190/article/details/111773270