multithreading 死锁避免和死锁预防有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2485608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:07:15  来源:igfitidea点击:

What the difference between deadlock avoidance and deadlock prevention?

multithreading

提问by skydoor

I have heard both of these terms used, are they the same thing or different things?

我听说过这两个术语,它们是相同的东西还是不同的东西?

采纳答案by NG.

You could look at it as:

你可以把它看成:

Avoid: Don't share resources across processes / mulitple threads

避免:不要跨进程/多线程共享资源

Prevent: When accsessing shared resources, use a semaphore. If locking multiple semaphores, be sure to unlock in the reverse order of locking. Always be sure to handle errors within the critical sections so the semaphore is released under all conditions.

防止:访问共享资源时,使用信号量。如果锁定多个信号量,一定要按照锁定的相反顺序进行解锁。始终确保处理关键部分中的错误,以便在所有条件下都释放信号量。

回答by s_rock

Deadlock:

僵局:

A deadlock is a situation where two or more competing actions are each waiting for the other to finish, and thus neither ever does. It can also be defined as a set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.

死锁是一种情况,其中两个或多个相互竞争的动作都在等待对方完成,因此两者都没有完成。它也可以定义为一组阻塞的进程,每个进程持有一个资源并等待获取该集合中另一个进程持有的资源。

For example if there is a system with two disk drives. If there are two processes P1 and P2 each hold one disk drive and each needs the other one, then the situation of deadlock occurs. The following conditions will be held simultaneously in case of deadlock:

例如,如果系统有两个磁盘驱动器。如果有两个进程 P1 和 P2 各自持有一个磁盘驱动器,并且每个进程都需要另一个,那么就会出现死锁的情况。出现死锁时会同时保持以下条件:

? Mutual exclusion: only one process at a time can use a resource

? 互斥:一次只有一个进程可以使用一个资源

? Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes

? 持有并等待:持有至少一个资源的进程正在等待获取其他进程持有的额外资源

? No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task

? 无抢占:资源只能由持有它的进程在该进程完成其任务后自愿释放

? Circular wait: there exists a set {P0, P1, …, Pn} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0.

? 循环等待:存在等待进程的集合{P0, P1, ..., Pn},使得 P0 正在等待 P1 持有的资源,P1 正在等待 P2 持有的资源,..., Pn–1正在等待 Pn 持有的资源,而 Pn 正在等待 P0 持有的资源。

Differences between Deadlock prevention, avoidance and detection are as follows:

死锁预防、避免和检测的区别如下:

Prevention:

预防

? The goal is to ensure that at least one of the necessary conditions for deadlock can never hold.

? 目标是确保至少有一个死锁的必要条件永远不会成立。

? Deadlock prevention is often impossible to implement.

? 死锁预防通常是不可能实现的。

? The system does not require additional a priori information regarding the overall potential use of each resource for each process.

? 该系统不需要关于每个过程的每个资源的整体潜在使用的额外先验信息。

? In order for the system to prevent the deadlock condition it does not need to know all the details of all resources in existence, available and requested.

? 为了使系统防止死锁情况,它不需要知道所有存在的、可用的和请求的资源的所有细节。

? Deadlock prevention techniques include non-blocking synchronization algorithms, serializing tokens, Dijkstra's algorithm etc.

? 死锁预防技术包括非阻塞同步算法、序列化令牌、Dijkstra 算法等。

? Resource allocation strategy for deadlock prevention is conservative, it under commits the resources.

? 防止死锁的资源分配策略是保守的,它提交的资源不足。

? All resources are requested at once.

? 一次请求所有资源。

? In some cases preempts more than often necessary.

? 在某些情况下,抢占的次数多于必要。

Avoidance:

回避:

? The goal for deadlock avoidance is to the system must not enter an unsafe state.

? 避免死锁的目标是使系统不能进入不安全状态。

? Deadlock avoidance is often impossible to implement.

? 死锁避免通常是不可能实现的。

? The system requires additional a priori information regarding the overall potential use of each resource for each process.

? 系统需要关于每个过程的每个资源的总体潜在使用的附加先验信息。

? In order for the system to be able to figure out whether the next state will be safe or unsafe, it must know in advance at any time the number and type of all resources in existence, available, and requested.

? 为了让系统能够确定下一个状态是安全还是不安全,它必须随时提前知道所有存在、可用和请求的资源的数量和类型。

? Deadlock avoidance techniques include Banker's algorithm, Wait/Die, Wound/Wait etc.

? 死锁避免技术包括银行家算法、Wait/Die、Wound/Wait 等。

? Resource allocation strategy for deadlock avoidance selects midway between that of detection and prevention.

? 避免死锁的资源分配策略在检测和预防之间选择中间位置。

? Needs to be manipulated until at least one safe path is found.

? 需要进行操作,直到找到至少一条安全路径。

? There is no preemption.

? 没有抢占。

Detection:

检测:

? The goal is to detect the deadlock after it occurs or before it occurs.

? 目标是在死锁发生之后或发生之前检测死锁。

? Detecting the possibility of a deadlock before it occurs is much more difficult and is, in fact, generally undecidable. However, in specific environments, using specific means of locking resources, deadlock detection may be decidable.

? 在死锁发生之前检测它的可能性要困难得多,而且实际上通常是不可判定的。但是,在特定环境中,使用特定的锁定资源手段,死锁检测可能是可判定的。

? The system does not requires additional a priori information regarding the overall potential use of each resource for each process in all cases.

? 在所有情况下,系统不需要关于每个进程的每个资源的整体潜在使用的额外先验信息。

? In order for the system to detect the deadlock condition it does not need to know all the details of all resources in existence, available and requested.

? 为了让系统检测死锁条件,它不需要知道所有存在的、可用的和请求的资源的所有细节。

? A deadlock detection technique includes, but is not limited to, Model checking. This approach constructs a Finite State-model on which it performs a progress analysis and finds all possible terminal sets in the model.

? 死锁检测技术包括但不限于模型检查。这种方法构建了一个有限状态模型,在该模型上执行进度分析并找到模型中所有可能的终端集。

? Resource allocation strategy for deadlock detection is very liberal. Resources are granted as requested.

? 死锁检测的资源分配策略非常自由。根据请求授予资源。

? Needs to be invoked periodically to test for deadlock.

? 需要定期调用以测试死锁。

? Preemption is seen.

? 抢占可见。

回答by Pounds

Deadlock avoidance: means, whenever a request is made for a particular resource by a particular process, you look at the available resources, if the future resource needs for process's already in use resources, determine the possibility of a deadlock in case the resource is granted. If possible, don't grant the resource, if not possible then grant the resource.

死锁避免:意味着,每当特定进程对特定资源发出请求时,您会查看可用资源,如果未来的资源需要进程已在使用的资源,则确定在授予资源的情况下发生死锁的可能性. 如果可能,不要授予资源,如果不可能,则授予资源。

Deadlock prevention: make sure that at least one of the condition for deadlock to occur is not fulfilled at anytime. This can be achieved in the way resources are requested and granted in the system.

死锁预防:确保在任何时候都至少不满足发生死锁的条件之一。这可以通过在系统中请求和授予资源的方式来实现。

回答by Sercan Ayd?n

1) Prevention: structure the system in such a way that one of the deadlock conditions is negated

1)预防:以一种死锁条件被否定的方式构建系统

2) Detection and recovery: let deadlocks occur, detect them and take action

2)检测和恢复:让死锁发生,检测它们并采取行动

3) Avoidance: -don't start processes whose requests may cause a deadlock -don't grant requests which may cause a deadlock

3) 避免: - 不要启动可能导致死锁的请求的进程 - 不要授予可能导致死锁的请求