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

死锁 预防死锁避免死锁

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

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

DeadLock

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

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

DeadLock

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.

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

DeadLock

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

DeadLock

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

死锁 预防死锁避免死锁

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545444.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

消息队列——发布订阅模式

在 Redis 中提供了专门的类型:Publisher(发布者)和 Subscriber(订阅者)来实现消息队列。 在文章开始之前,先来介绍消息队列中有几个基础概念,以便大家更好的理解本文的内容。 首先,发布消息的叫做发布方或发布者,也就是消息的生产者,而接收消息的叫做消息的订阅方或…

eclipse安装jetty插件

2019独角兽企业重金招聘Python工程师标准>>> 1 eclipse安装jetty插件 由于项目需要&#xff0c;eclipse需要安装jetty插件&#xff0c;从百度搜索的结果基本都是从http://run-jetty-run.googlecode.com/svn/trunk/updatesite获取jetty&#xff0c;国内的网络根本无法…

实战:分布式锁详解与代码

什么是锁? 锁是一种常用的并发控制机制,用于保证一项资源在任何时候只能被一个线程使用,如果其他线程也要使用同样的资源,必须排队等待上一个线程使用完。 锁的示意图,如下所示: 什么是分布式锁? 上面说的锁指的是程序级别的锁,例如 Java 语言中的 synchronized 和 …

编译原理第二章

编译原理第二章2.3_句型的分析&#xff08;1&#xff09;语法树和二义性&#xff08;2&#xff09;短语和句柄&#xff08;规约问题&#xff09;2.5_文法和语言的Chomsky分类&#xff08;1&#xff09;0型文法&#xff08;2&#xff09;1型文法&#xff08;3&#xff09;2型文法…

微软职位内部推荐-Sr. SW Engineer for Azure Networking

微软近期Open的职位:Senior SW EngineerThe world is moving to cloud computing. Microsoft is betting Windows Azure as our cloud computing platform. Important steps have already been taken to virtualize storage and computing through software, increasing agilit…

对称密码和非对称密码体系_密码学类型:对称和不对称

对称密码和非对称密码体系Cryptography is a study of different techniques used for encryption and decryption of the text to convert the plain text into ciphertext and vice-versa. There are many different cryptographic techniques and algorithm which have been…

servlet 和 struts2 同时使用 以及 使用struts2标签库时报错

做网页的时候 想让 servlet 和 struts 都有效。但是在过滤的时候出了点问题&#xff1a;就是 过滤器的*.action 的时候 struts 标签库失效的问题我觉得以下是个不错的 解决方案&#xff1a;转载&#xff1a;http://blog.sina.cn/dpool/blog/s/blog_7d681d490100zbwf.htmlThe St…

编译原理第三章

编译原理第三章3.1_正规文法和状态转换图&#xff08;1&#xff09;构造状态转换图&#xff08;2&#xff09;状态矩阵3.2_有限自动机3.2.1_确定的有限自动机DFA3.2.2_非确定的有限自动机NFA3.3_NFA转换为DFA&#xff08;NFA确定化&#xff09;3.3.1_无ε动作的NFA确定化3.3.2_…

消息队列终极解决方案——Stream(下)

在开始使用消息分组之前,我们必须手动创建分组才行,以下是几个和 Stream 分组有关的命令,我们先来学习一下它的使用。 消息分组命令 创建消费者群组 127.0.0.1:6379> xgroup create mq group1 0-0 OK相关语法: xgroup create stream-key group-key ID其中: mq 为…

ftp上传和下载命令

2019独角兽企业重金招聘Python工程师标准>>> 假设有一目标FTP服务器&#xff0c;IP&#xff1a;123.123.123.123&#xff0c;用户名&#xff1a;ftpname 密码&#xff1a;ftppwd。当前要通过命令行将D:\ftpin目录下的file.doc上传到目标服务器&#xff0c;从服务器下…

String.IsNullOrEmpty()方法以及C#中的示例

C&#xff03;String.IsNullOrEmpty()方法 (C# String.IsNullOrEmpty() Method) String.IsNullOrEmpty() method is a built-in method of String class and it is used to check whether a string is Null or Empty? If string object is not initialized with a correct val…

消息队列终极解决方案——Stream(上)

在 Redis 5.0 Stream 没出来之前,消息队列的实现方式都有着各自的缺陷,例如: 发布订阅模式 PubSub,不能持久化也就无法可靠的保存消息,并且对于离线重连的客户端不能读取历史消息的缺陷;列表实现消息队列的方式不能重复消费,一个消息消费完就会被删除;有序集合消息队列…

《算法导论》学习笔记——快速排序

快速排序 1.快速排序原理 快速排序是一种应用很广泛的排序算法&#xff0c;与归并排序类似&#xff0c;快速排序也采用了分治策略。对于一个待排序的数组A[p...r]进行快速排序&#xff0c;根据分治思想&#xff0c;可以分为如下三个步骤&#xff1a;   - 分解&#xff1a;数组…

JavaTCP连接

输入输出操作 可以这样理解&#xff1a; BufferedReader/BufferedWriter使用三部曲&#xff1a; 服务器操作 import java.io.*; import java.net.ServerSocket; import java.net.Socket;public class Server {private static int port 8002;//设置端口号public static v…

JavaScript中的If和Else语句(香草)

Generally if statements have the following syntax: 通常&#xff0c; if语句具有以下语法&#xff1a; if(condition){Our code;}The condition, here can be anything from the mathematical expression, Boolean, relation operations etc. 条件&#xff0c;这里可以是数…

不创建 sequence 自增字段

drop table t_log;create table t_log (log_id number primary key,user_name varchar2(100),log_date date);--新建一个表&#xff0c;有id 用户名 和 日期字段create or replace trigger tri_logonafter logon on databasebegininsert into t_log values(nvl((select max(…

完整案例:实现延迟队列的两种方法

延迟队列是指把当前要做的事情,往后推迟一段时间再做。 延迟队列在实际工作中和面试中都比较常见,它的实现方式有很多种,然而每种实现方式也都有它的优缺点,接下来我们来看。 延迟队列的使用场景 延迟队列的常见使用场景有以下几种: 超过 30 分钟未支付的订单,将会被取…

Java API概述及应用

Java API概述及应用5.1_Scanner和Random的使用&#xff08;1&#xff09;Scanner&#xff08;2&#xff09;Random生成随机数5.2_ArrayList集合的使用&#xff08;1&#xff09;ArrayList的定义及限制&#xff08;2&#xff09;函数调用&#xff08;3&#xff09;字符串字符串加…

定位position详解:relative与absolute

定位标签&#xff1a;position 包含属性&#xff1a;relative&#xff08;相对&#xff09; absolute&#xff08;绝对&#xff09; 1.position:relative; 如果对一个元素进行相对定位&#xff0c;首先它将出现在它所在的位置上。然后通过设置垂直或水平位置&#xff0c;让这个…

实战:布隆过滤器安装与使用及原理分析

我们前面有讲到过 HyperLogLog 可以用来做基数统计,但它没提供判断一个值是否存在的查询方法,那我们如何才能查询一个值是否存在于海量数据之中呢? 如果使用传统的方式,例如 SQL 中的传统查询,因为数据量太多,查询效率又低有占用系统的资源,因此我们需要一个优秀的算法…