死锁 预防死锁避免死锁_死锁和处理死锁的方法
死鎖 預(yù)防死鎖避免死鎖
僵局 (Deadlock)
In the multiprogramming operating system, there are a number of processing which fights for a finite number of resources and sometimes waiting process never gets a chance to change its state because the resources for which it is waiting are held by another waiting process. A set of a process is called deadlock when they are waiting for the happening of an event which is called by some another event in the same set.
在多程序操作系統(tǒng)中,存在許多爭奪有限數(shù)量資源的處理,并且有時等待進(jìn)程永遠(yuǎn)沒有機(jī)會更改其狀態(tài),因為它等待的資源由另一個等待進(jìn)程持有。 當(dāng)一組進(jìn)程等待同一組中另一個事件調(diào)用的事件發(fā)生時,它們稱為死鎖。
Here every process will follow the system model which means the process requests a resource if not allocated then wait otherwise it allocated will use the resources and release it after use.
在這里,每個進(jìn)程都將遵循系統(tǒng)模型,這意味著該進(jìn)程將請求資源(如果未分配),然后等待,否則分配的資源將使用該資源,并在使用后釋放資源。
處理死鎖的方法 (Methods for handling deadlock)
There are mainly four methods for handling deadlock.
主要有四種處理死鎖的方法。
1.死鎖無知 (1. Deadlock ignorance)
It is the most popular method and it acts as if no deadlock and the user will restart. As handling deadlock is expensive to be called of a lot of codes need to be altered which will decrease the performance so for less critical jobs deadlock are ignored. Ostrich algorithm is used in deadlock Ignorance. Used in windows, Linux etc.
這是最流行的方法,它的作用就像沒有死鎖,用戶將重新啟動。 由于處理死鎖非常昂貴,因此需要更改許多代碼,這將降低性能,因此對于不太關(guān)鍵的作業(yè),死鎖將被忽略。 鴕鳥算法用于死鎖無知。 用于Windows,Linux等。
2.防止死鎖 (2. Deadlock prevention)
It means that we design such a system where there is no chance of having a deadlock.
這意味著我們設(shè)計的系統(tǒng)不會出現(xiàn)死鎖。
Mutual exclusion:
互斥:
It can’t be resolved as it is the hardware property. For example, the printer cannot be simultaneously shared by several processes. This is very difficult because some resources are not sharable.
由于它是硬件屬性,因此無法解決。 例如,打印機(jī)不能被多個進(jìn)程同時共享。 這是非常困難的,因為某些資源不可共享。
Hold and wait:
保持并等待:
Hold and wait can be resolved using the conservative approach where a process can start it and only if it has acquired all the resources.
保持等待可以使用保守的方法解決,在該方法中,只有當(dāng)進(jìn)程已獲取所有資源時,進(jìn)程才能啟動它。
Active approach:
主動方式:
Here the process acquires only requires resources but whenever a new resource requires it must first release all the resources.
在這里,過程僅獲取需要的資源,但是每當(dāng)需要新資源時,它都必須首先釋放所有資源。
Wait time out:
等待超時:
Here there is a maximum time bound until which a process can wait for other resources after which it must release the resources.
這里有一個最大的時間限制,在此之前,進(jìn)程必須等待其他資源,然后才能釋放資源。
Circular wait:
循環(huán)等待:
In order to remove circular wait, we assign a number to every resource and the process can request only in the increasing order otherwise the process must release all the high number acquires resources and then make a fresh request.
為了消除循環(huán)等待,我們?yōu)槊總€資源分配了一個編號,該流程只能按遞增順序請求,否則該流程必須釋放所有高編號獲取的資源,然后提出新的要求。
No pre-emption:
無搶占:
In no pre-emption, we allow forceful pre-emption where a resource can be forcefully pre-empted. The pre-empted resource is added to the list of resources where the process is waiting. The new process can be restarted only when it regains its old resources. Priority must be given to a process which is in waiting for state.
在沒有搶占的情況下,我們允許強(qiáng)制搶占,以便可以強(qiáng)制搶占資源。 被搶占的資源將添加到進(jìn)程正在等待的資源列表中。 僅當(dāng)重新獲得舊資源時,才能重新啟動新進(jìn)程。 必須優(yōu)先考慮處于等待狀態(tài)的進(jìn)程。
3.避免死鎖 (3. Deadlock avoidance)
Here whenever a process enters into the system it must declare maximum demand. To the deadlock problem before the deadlock occurs. This approach employs an algorithm to access the possibility that deadlock would occur and not act accordingly. If the necessary condition of deadlock is in place it is still possible to avoid feedback by allocating resources carefully.
在這里,只要有流程進(jìn)入系統(tǒng),它就必須聲明最大需求。 在出現(xiàn)死鎖之前先解決死鎖問題。 這種方法采用一種算法來訪問可能發(fā)生死鎖而不采取相應(yīng)措施的可能性。 如果死鎖的必要條件到位,則仍然可以通過仔細(xì)分配資源來避免反饋。
A deadlock avoidance algorithm dynamically examines the resources allocation state to ensure that a circular wait condition case never exists. Where the resources allocation state is defined by the of available and allocated resources and the maximum demand of the process. There are 3 states of the system:
避免死鎖算法動態(tài)檢查資源分配狀態(tài),以確保不存在循環(huán)等待條件的情況。 資源分配狀態(tài)由可用資源和已分配資源以及進(jìn)程的最大需求來定義。 系統(tǒng)有3種狀態(tài):
Safe state
安全狀態(tài)
When a system can allocate the resources to the process in such a way so that they still avoid deadlock then the state is called safe state. When there is a safe sequence exit then we can say that the system is in the safe state.
當(dāng)系統(tǒng)可以通過這種方式將資源分配給進(jìn)程,以便它們?nèi)匀槐苊馑梨i時,該狀態(tài)稱為安全狀態(tài)。 當(dāng)存在安全序列退出時,我們可以說系統(tǒng)處于安全狀態(tài)。
A sequence is in the safe state only if there exists a safe sequence. A sequence of process P1, P2, Pn is a safe sequence for the current allocation state if for each Pi the resources request that Pi can still make can be satisfied by currently available resources pulls the resources held by all Pj with j<i.
僅當(dāng)存在安全序列時,序列才處于安全狀態(tài)。 如果對于每個Pi來說 , Pi仍然可以提出的資源請求可以通過當(dāng)前可用資源得到滿足,則對于所有Pi而言 ,過程P1,P2,Pn的序列對于當(dāng)前分配狀態(tài)來說是一個安全序列,并且將所有Pj擁有的資源j <i 。
Methods for deadlock avoidance
避免死鎖的方法
1) Resource allocation graph
1)資源分配圖
This graph is also kind of graphical bankers' algorithm where a process is denoted by a circle Pi and resources is denoted by a rectangle RJ (.dots) inside the resources represents copies.
該圖也是圖形銀行家算法的一種,其中過程用圓圈Pi表示,資源用矩形RJ(。點)表示,資源內(nèi)部表示副本。
Presence of a cycle in the resources allocation graph is necessary but not sufficient condition for detection of deadlock. If the type of every resource has exactly one copy than the presence of cycle is necessary as well as sufficient condition for detection of deadlock.
資源分配圖中存在循環(huán)是必要的,但不足以檢測死鎖。 如果每個資源的類型都只有一個副本,則必須要有周期,并且要有足夠的條件來檢測死鎖。
This is in unsafe state (cycle exist) if P1 request P2 and P2 request R1 then deadlock will occur.
如果P1請求P2和P2請求R1處于不安全狀態(tài)(存在循環(huán)),則將發(fā)生死鎖。
2) Bankers’s algorithm
2)銀行家算法
The resource allocation graph algorithms not applicable to the system with multiple instances of the type of each resource. So for this system Banker’s algorithm is used.
資源分配圖算法不適用于具有每種資源類型的多個實例的系統(tǒng)。 因此,對于該系統(tǒng),使用Banker的算法。
Here whenever a process enters into the system it must declare maximum demand possible.
在這里,每當(dāng)流程進(jìn)入系統(tǒng)時,它都必須聲明可能的最大需求。
At runtime, we maintain some data structure like current allocation, current need, current available etc. Whenever a process requests some resources we first check whether the system is in a safe state or not meaning if every process requires maximum resources then is there ant sequence in which request can be entertaining if yes then request is allocated otherwise rejected.
在運行時,我們維護(hù)一些數(shù)據(jù)結(jié)構(gòu),例如當(dāng)前分配,當(dāng)前需求,當(dāng)前可用等。每當(dāng)一個進(jìn)程請求一些資源時,我們首先檢查系統(tǒng)是否處于安全狀態(tài),這意味著每個進(jìn)程是否需要最大的資源,然后有螞蟻序列如果是,則可以招待請求,然后分配請求,否則拒絕。
Safety algorithm
安全算法
This algorithm is used to find whether system is in safe state or not we can find
該算法用于發(fā)現(xiàn)系統(tǒng)是否處于安全狀態(tài)
Remaining Need = Max Need – Current allocation Current available = Total available – Current allocation .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}Let's understand it by an example:
讓我們通過一個例子來理解它:
Consider the following 3 process total resources are given for A= 6, B= 5, C= 7, D = 6
考慮以下三個過程總資源,其中A = 6,B = 5,C = 7,D = 6
First we find the need matrix by Need= maximum – allocation
首先,我們通過Need = maximum-分配找到需求矩陣
Then find available resources = total – allocated
然后找到可用資源=總計–已分配
A B C D( 6 5 7 6) - A B C D( 3 4 6 4Available resources A B C D( 3 1 1 2)Then we check whether the system is in deadlock or not and find the safe sequence of process.
然后,我們檢查系統(tǒng)是否處于死鎖狀態(tài),并找到安全的處理順序。
P1 can be satisfied
P1可以滿足
Available= P1 allocated + available( 1, 2, 2, 1) +( 3, 1, 1,2) = (4, 3, 3, 3)P2 can be satisfied
P2可以滿足
Available= P2 allocated + available(1, 0, 3, 3) + (4, 3, 3, 3) = (5, 3, 6, 6)P3 can be satisfied
P3可以滿足
Available= P3 allocated + available(1, 2, 1, 0) + (5, 3, 6, 6) = (6, 5, 7, 6)So the system is safe and the safe sequence is P1 → P2 → P3
因此系統(tǒng)是安全的,安全順序為P1→P2→P3
4.檢測與恢復(fù) (4. Detection and recovery)
When the system is in deadlock then one method is to inform the operates and then operator deal with deadlock manually and the second method is system will automatically recover from deadlock. There are two ways to recover from deadlock:
當(dāng)系統(tǒng)處于死鎖狀態(tài)時,一種方法是通知操作人員,然后由操作員手動處理死鎖,第二種方法是系統(tǒng)將自動從死鎖狀態(tài)中恢復(fù)。 有兩種方法可以從死鎖中恢復(fù):
Process termination:
流程終止:
Deadlock can be eliminated by aborting a process. Abort all deadlock process. Abort is processed at a time until the deadlock cycle is eliminated. This can help to recover the system from file deadlock.
可以通過中止進(jìn)程來消除死鎖。 中止所有死鎖過程。 一次處理中止,直到消除死鎖周期。 這可以幫助從文件死鎖中恢復(fù)系統(tǒng)。
Resources preemption:
資源搶占:
To eliminate deadlock using resources preemption, we prompt the same resources pas processes and give these resources to another process until the deadlock cycle is broken.
為了使用資源搶占消除死鎖,我們會提示使用相同的資源pas進(jìn)程,并將這些資源分配給另一個進(jìn)程,直到打破死鎖周期為止。
Here a process is partially rollback until the last checkpoint or and hen detection algorithm is executed.
在此,過程將部分回滾,直到執(zhí)行最后一個檢查點或母雞檢測算法為止。
翻譯自: https://www.includehelp.com/operating-systems/deadlock-and-method-for-handling-deadlock.aspx
死鎖 預(yù)防死鎖避免死鎖
總結(jié)
以上是生活随笔為你收集整理的死锁 预防死锁避免死锁_死锁和处理死锁的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c# uri.host_C#| Uri.
- 下一篇: 云计算应用迁移_应用迁移策略到云