死锁 预防死锁避免死锁_死锁和处理死锁的方法

死锁 预防死锁避免死锁

僵局 (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.

在多程序操作系统中,存在许多争夺有限数量资源的处理,并且有时等待进程永远没有机会更改其状态,因为它等待的资源由另一个等待进程持有。 当一组进程等待同一组中另一个事件调用的事件发生时,它们称为死锁。

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.

在这里,每个进程都将遵循系统模型,这意味着该进程将请求资源(如果未分配),然后等待,否则分配的资源将使用该资源,并在使用后释放资源。

处理死锁的方法 (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.

这是最流行的方法,它的作用就像没有死锁,用户将重新启动。 由于处理死锁非常昂贵,因此需要更改许多代码,这将降低性能,因此对于不太关键的作业,死锁将被忽略。 鸵鸟算法用于死锁无知。 用于Windows,Linux等。

2.防止死锁 (2. Deadlock prevention)

It means that we design such a system where there is no chance of having a deadlock.

这意味着我们设计的系统不会出现死锁。

  • 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.

    由于它是硬件属性,因此无法解决。 例如,打印机不能被多个进程同时共享。 这是非常困难的,因为某些资源不可共享。

  • 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.

    保持等待可以使用保守的方法解决,在该方法中,只有当进程已获取所有资源时,进程才能启动它。

  • Active approach:

    主动方式:

    Here the process acquires only requires resources but whenever a new resource requires it must first release all the resources.

    在这里,过程仅获取需要的资源,但是每当需要新资源时,它都必须首先释放所有资源。

  • 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.

    这里有一个最大的时间限制,在此之前,进程必须等待其他资源,然后才能释放资源。

  • Circular wait:

    循环等待:

    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.

    为了消除循环等待,我们为每个资源分配了一个编号,该流程只能按递增顺序请求,否则该流程必须释放所有高编号获取的资源,然后提出新的要求。

  • 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.

    在没有抢占的情况下,我们允许强制抢占,以便可以强制抢占资源。 被抢占的资源将添加到进程正在等待的资源列表中。 仅当重新获得旧资源时,才能重新启动新进程。 必须优先考虑处于等待状态的进程。

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.

在这里,只要有流程进入系统,它就必须声明最大需求。 在出现死锁之前先解决死锁问题。 这种方法采用一种算法来访问可能发生死锁而不采取相应措施的可能性。 如果死锁的必要条件到位,则仍然可以通过仔细分配资源来避免反馈。

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:

避免死锁算法动态检查资源分配状态,以确保不存在循环等待条件的情况。 资源分配状态由可用资源和已分配资源以及进程的最大需求来定义。 系统有3种状态:

Safe state

安全状态

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.

当系统可以通过这种方式将资源分配给进程,以便它们仍然避免死锁时,该状态称为安全状态。 当存在安全序列退出时,我们可以说系统处于安全状态。

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.

仅当存在安全序列时,序列才处于安全状态。 如果对于每个Pi来说Pi仍然可以提出的资源请求可以通过当前可用资源得到满足,则对于所有Pi而言 ,过程P1,P2,Pn的序列对于当前分配状态来说是一个安全序列,并且将所有Pj拥有的资源j

死锁 预防死锁避免死锁_死锁和处理死锁的方法_第1张图片

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(。点)表示,资源内部表示副本。

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.

资源分配图中存在循环是必要的,但不足以检测死锁。 如果每个资源的类型都只有一个副本,则必须要有周期,并且要有足够的条件来检测死锁。

死锁 预防死锁避免死锁_死锁和处理死锁的方法_第2张图片

This is in unsafe state (cycle exist) if P1 request P2 and P2 request R1 then deadlock will occur.

如果P1请求P2和P2请求R1处于不安全状态(存在循环),则将发生死锁。

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.

资源分配图算法不适用于具有每种资源类型的多个实例的系统。 因此,对于该系统,使用Banker的算法。

Here whenever a process enters into the system it must declare maximum demand possible.

在这里,每当流程进入系统时,它都必须声明可能的最大需求。

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.

在运行时,我们维护一些数据结构,例如当前分配,当前需求,当前可用等。每当一个进程请求一些资源时,我们首先检查系统是否处于安全状态,这意味着每个进程是否需要最大的资源,然后有蚂蚁序列如果是,则可以招待请求,然后分配请求,否则拒绝。

Safety algorithm

安全算法

This algorithm is used to find whether system is in safe state or not we can find

该算法用于发现系统是否处于安全状态

    Remaining Need = Max Need – Current allocation

    Current available = Total available – Current allocation


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  4
    Available 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.

然后,我们检查系统是否处于死锁状态,并找到安全的处理顺序。

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

因此系统是安全的,安全顺序为P1→P2→P3

4.检测与恢复 (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:

当系统处于死锁状态时,一种方法是通知操作人员,然后由操作员手动处理死锁,第二种方法是系统将自动从死锁状态中恢复。 有两种方法可以从死锁中恢复:

  • 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.

    可以通过中止进程来消除死锁。 中止所有死锁过程。 一次处理中止,直到消除死锁周期。 这可以帮助从文件死锁中恢复系统。

  • 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进程,并将这些资源分配给另一个进程,直到打破死锁周期为止。

    Here a process is partially rollback until the last checkpoint or and hen detection algorithm is executed.

    在此,过程将部分回滚,直到执行最后一个检查点或母鸡检测算法为止。

翻译自: https://www.includehelp.com/operating-systems/deadlock-and-method-for-handling-deadlock.aspx

死锁 预防死锁避免死锁

你可能感兴趣的:(算法,java,python,操作系统,linux)