当前位置:网站首页>Deadlock problem summary
Deadlock problem summary
2022-06-13 00:59:00 【-LM-】
What is a deadlock
Thread deadlocks are described like this ⼀ In this case : Multiple threads are blocked at the same time , Of them ⼀ One or all of them are waiting for a resource to be released . Because the thread is ⽆ Block up for a limited period of time , So it's impossible for the program to work properly ⽌. As shown in the figure below , Threads A Resources held 2, Threads B Resources held 1, They all want to apply at the same time for ⽅ Resources for , So these two threads will wait for each other ⽽ Into the ⼊ Deadlock state .
Cause of deadlock
1、 Compete for resources
There are two types of resources in the system :
- Deprivable resources , It refers to a process after obtaining such resources , This resource can be taken away by other processes or systems ,CPU Both main memory and main memory belong to the deprivable resources ;
- The other type of resource is an inalienable resource , When the system allocates such resources to a process , You can't take it back by force , It can only be released after the process is used up , Like a tape drive 、 Printers, etc .
One of the competing resources in deadlock refers to the competitive and undeniable resources ( for example : There is only one printer in the system , Available for process P1 Use , Assume P1 The printer has been occupied , if P2 Continuing to ask the printer to print will block )
Competing resources in deadlock generation. Another type of resource is competing temporary resources ( Temporary resources include hardware interrupts 、 The signal 、 news 、 Messages in the buffer, etc ), Usually, the order of message communication is improper , A deadlock occurs
2、 The order of advancement between processes is illegal
if P1 Keep the resources R1,P2 Keep the resources R2, The system is in an unsafe state , Because these two processes move forward , A deadlock may occur
for example , When P1 Run to the P1:Request(R2) when , Will cause R2 Has been P2 Occupy and block ; When P2 Run to the P2:Request(R1) when , It will also be due to R1 Has been P1 Occupy and block , A process deadlock occurs
The necessary conditions for deadlock
- mutual exclusion : The process is exclusive to the required resources , If another process requests the resource , The requesting process can only wait .
- Conditions of non deprivation : The process has not released the obtained resources , It can't be taken away by other processes , I can only release myself .
- Request and hold conditions : The resources currently owned by the process when the process requests other new resources , The process continues to occupy .
- Loop wait condition : There is a process resource cycle waiting chain , The resources obtained by each process in the chain are simultaneously used by the next process in the chain
request .
Deadlock solution
Ostrich strategy
Deadlock detection
Deadlock prevention
Deadlock avoidance
Deadlock Relieving
Ostrich strategy
Bury your head in the sand , Pretend there's no problem at all .
Because the cost of solving the deadlock problem is very high , So ostrich strategy, which does not take task measures, will get higher performance .
When a deadlock occurs, it will not affect the user much , Or the probability of deadlock is very low , You can use the ostrich strategy .
Most operating systems , Include Unix,Linux and Windows, The way to deal with deadlock is simply to ignore it .
Deadlock detection
1、 Deadlock detection of one resource of each type 
The figure above shows the resource allocation diagram , Where the box represents the resource , A circle indicates a process . A resource pointing to a process indicates that the resource has been assigned to the process , A process pointing to a resource indicates that the process requests to get the resource .
chart a You can extract the ring , Pictured b, It satisfies the loop waiting condition , So deadlocks happen .
The deadlock detection algorithm for each type of resource is realized by detecting whether there is a ring in the digraph , Depth first search from a node , Mark visited nodes , If you access a marked node , It means that there are rings in digraphs , That is to say, deadlock is detected .
2、 Deadlock detection of multiple resources of each type 
Above picture , There are three processes and four resources , Each data represents the following :
- E vector : Total resources
- A vector : Resource surplus
- C matrix : The number of resources each process has , Each line represents the amount of resources a process has
- R matrix : The number of resources requested per process
process P1 and P2 The requested resources are not satisfied , Only process P3 Sure , Give Way P3 perform , After the release of P3 Resources we have , here A = (2 2 2 0).P2 It can be executed , Release after execution P2 Resources we have ,A = (4 2 2 1) .P1 It can also be executed . All processes can be carried out smoothly , There is no deadlock .
The algorithm is summarized as follows :
Each process is not marked at the beginning , The execution process may be marked . When the algorithm ends , Any process that is not marked is a deadlock process .
- Looking for a process without a tag Pi, It requests resources less than or equal to A.
- If such a process is found , It will be C The order of the matrix i The row vector is added to A in , Mark the process , And turn back 1.
- If there is no such process , Algorithm was terminated .
Deadlock prevention
- Destroy mutually exclusive condition : We can't break this condition , Because we ⽤ Locks are meant to make them mutually exclusive ( Critical resources require exclusive access ).
- Break request and hold condition :⼀ Apply for all the resources for the second time .
- Destroy the condition of non deprivation : Occupy ⽤ Part of the resources thread into ⼀ When applying for other resources , If the application does not arrive , Can actively release the resources it occupies .
- Break cycle waiting condition : Prevention by applying resources in sequence . Click on ⼀ Apply for resources in order , Releasing resources releases resources in reverse order . Break cycle waiting condition .
Deadlock avoidance
Banker Algorithm
In the deadlock avoidance method, processes are allowed to request resources dynamically , But before the system allocates resources , The security of this allocation should be calculated first , If the allocation does not cause the system to go into an unsafe state , Then assign , Or wait for . To implement the banker's algorithm , The system must set up several data structures .
Deadlock Relieving
Once a deadlock is detected , We should take corresponding measures immediately , To unlock .
The main methods of deadlock release are :
- Law of deprivation of resources . Suspend some deadlock processes , And seize its resources , Assign these resources to other deadlock processes . But you should prevent a suspended process from getting resources for a long time , And in a state of scarcity .
- Repeal process law . Compulsory revocation part 、 Even deadlocks all processes and deprives them of resources . The principle of revocation can be based on the priority of the process and the cost of the revocation process .
- Process fallback . Let one ( many ) Processes are backed back enough to avoid deadlocks , Voluntary release of resources rather than deprivation when the process goes back . Ask the system to keep the historical information of the process , Set restore point .
边栏推荐
- The seventh finals of the Blue Bridge Cup
- How to handle different types of data
- Influence of higher order poles on waveform
- Opencv desaturation
- [003] embedded learning: creating project templates - using stm32cubemx
- Breadth first search for node editor runtime traversal
- Sequence table - find main element
- Illustrator tutorial, how to add dashes and arrows in illustrator?
- 五篇经典好文,值得一看
- ImportError: cannot import name ' get_ ora_ doc' from partially initialized module
猜你喜欢
![[JS component] floating text](/img/e5/7faad5422bba919bed34e3dbcf7ba0.jpg)
[JS component] floating text

Traditional machine learning classification model predicts the rise and fall of stock prices under more than 40 indicators
![[buglist] serial port programming does not read data](/img/bf/8e63f679bf139fbbf222878792ae21.jpg)
[buglist] serial port programming does not read data

Physical orbit simulation

Aunt learning code sequel: ability to sling a large number of programmers

pytorch和tensorflow有什么区别?

Et5.0 simply transform referencecollectorieditor

什么是 Meebits?一个简短的解释

Breadth first search for node editor runtime traversal

Antdpro - protable realizes the linkage effect of two selection boxes
随机推荐
Aunt learning code sequel: ability to sling a large number of programmers
Maybe we can figure out the essence of the Internet after the dust falls
Androi天氣
Build your own PE manually from winpe of ADK
[JS component] simulation framework
[JS component] customize the right-click menu
With a market value of more than trillion yuan and a sales volume of more than 100000 yuan for three consecutive months, will BYD become the strongest domestic brand?
Canvas random bubbling background
sort
Get preview of precast body
Matrix fast power
Higherhrnet pre training model -- download from network disk
深度学习模型剪枝
Common skills for quantitative investment - drawing 2: drawing the moving average
Oceanbase is the leader in the magic quadrant of China's database in 2021
[backtrader source code analysis 7] analysis of the functions for calculating mean value, variance and standard deviation in mathsupport in backtrader (with low gold content)
304. Merge two ordered arrays
Androi weather
[JS component] create a custom horizontal and vertical scroll bar following the steam style
Addition and modification of JPA