操作系统中的死锁
1.1究竟什么是僵局? (1.1 What exactly is a deadlock?)
In a multiprogramming environment, there may be several processes with a finite number of resources. A process may request another resource while still holding some of the other resources with itself. If these requested resources are not available at the time, the process may enter into a waiting state. This waiting state may never end if the resources requested by this process are held by other waiting processes. This situation is called "Deadlock" where none of the processes are ready to release their resources and are also waiting indefinitely for other waiting processes to release their resources.
在多程序环境中,可能会有几个进程使用有限数量的资源。 一个进程可能会请求另一个资源,同时仍然保留其他一些资源。 如果这些请求的资源当时不可用,则过程可能会进入等待状态。 如果此进程请求的资源由其他等待进程持有,则此等待状态可能永远不会结束。 这种情况称为“死锁” ,其中没有一个进程准备释放它们的资源,并且也无限期地等待其他等待的进程释放它们的资源。
1.2系统模型 (1.2 System Model)
A system consists of a finite number of resources and these resources are partitioned into several types, each consisting of some number of identical instances.
一个系统由有限数量的资源组成,并且这些资源被分为几种类型,每种类型都由一定数量的相同实例组成。
Resource types include the printer, DVD drives, CPU cycles, files, etc. For example, if a system has two printers then the resource type Printer has two instances.
资源类型包括打印机,DVD驱动器,CPU周期,文件等。例如,如果系统有两台打印机,则资源类型“打印机”有两个实例。
These instances may be identical or different. If the instances are similar, any instance can be assigned to the process, and if not, then the two printers need to be defined as separate resource classes.
这些实例可以相同或不同。 如果实例相似,则可以将任何实例分配给该流程,否则,则需要将两个打印机定义为单独的资源类。
A process must request the resource before using it and must release it after use.
进程必须在使用资源之前请求资源,并且在使用之后必须释放资源。
A process may request as many resources as it requires for the completion of its task but it should not exceed the total number of resources present in the system.
进程可以请求完成任务所需的资源,但是它不应超过系统中存在的资源总数。
To summarize, a process must utilize a resource in the following manner,
总而言之,流程必须以以下方式利用资源:
- Request: The process must request for the resource. If the request can't be granted immediately, it must wait to acquire the resource.请求 :进程必须请求资源。 如果无法立即批准该请求,则它必须等待获取资源。
- Use: The process must use the resource after acquiring it.使用 :流程必须在获取资源后使用它。
- Release: The process must release the resource after using it.释放 :进程必须在使用后释放资源。
1.3死锁特征 (1.3 Deadlock Characterization)
There are four necessary conditions for a deadlock to occur. If any of the four conditions fail, then the condition is not called a deadlock and maybe recovered easily. The following are the necessary conditions,
发生死锁有四个必要条件。 如果这四个条件中的任何一个失败,则该条件不称为死锁,并且可能容易恢复。 以下是必要条件,
1.3.1 Mutual Exclusion
1.3.1互斥
At least one resource must be held in a non-sharable mode. This means that only one process can use the resource at a particular time. If any other process requests this non-sharable resource, then, it must wait for the process which is holding the resource to release it.
至少一种资源必须处于不可共享的模式。 这意味着在特定时间只有一个进程可以使用资源。 如果任何其他进程请求此不可共享资源,则它必须等待持有该资源的进程释放它。
If we think clearly, this is a necessary condition for deadlock because if more than one process is allowed to use a resource at the same time, the deadlock will never occur and all the resources will carry out their tasks easily.
如果我们想清楚,这是死锁的必要条件,因为如果允许多个进程同时使用一个资源,则死锁将永远不会发生,所有资源将轻松执行其任务。
1.3.2 Hold and Wait
1.3.2保持并等待
A process must be holding at least one resource and waiting to acquire other resources that are currently being held by other processes.
一个进程必须至少持有一种资源,并等待获取其他进程当前正在持有的其他资源。
1.3.3 No preemption
1.3.3无抢占
The resources cannot be preempted. What this means is that one process cannot snatch a resource from another process that currently holds it. It can only be released voluntarily by the process holding it after its execution.
资源不能被抢占。 这意味着一个进程无法从当前拥有该进程的另一进程中抢夺资源。 它只能在执行后由持有它的进程自动释放。
1.3.4 Circular Wait
1.3.4循环等待
A set {P0, P1, ..., Pn} of waiting process must exist such that P0 is waiting for a resource held by P1, P1 is waiting for a resource held by P2, …, Pn-1 is waiting for a resource held by Pn, and Pn is waiting for a resource held by P0.
必须存在一组{P0,P1,...,Pn}等待过程,这样P0正在等待P1拥有的资源,P1正在等待P2拥有的资源,...,Pn-1正在等待资源Pn持有,而Pn正在等待P0持有的资源。
The circular wait condition implies the hold and waits for condition, so these four conditions are not independent as we shall see in the next article.
循环等待条件暗含保持和等待条件,因此这四个条件不是独立的,我们将在下一篇文章中看到。
1.4资源分配图 (1.4 Resource Allocation Graph)
Resource allocation graphs are mainly used as methods for the detection of a deadlock. We will discuss this feature when we read about Deadlock Handling mechanisms.
资源分配图主要用作检测死锁的方法。 当我们阅读有关死锁处理机制时,我们将讨论此功能。
Deadlock can be best represented in the terms of a directed graph called a system resource-allocation graph. The graph consists of a set of vertices V and a set of edges E.
死锁最好用称为系统资源分配图的有向图来表示 。 该图由一组顶点V和一组边E组成 。
The edges can be of two types, represented as Pi for a process i and Rj for a resource type j, where i, j are the integer values representing the number of processes and resource types respectively.
边缘可以是两种类型,分别表示为进程i的 Pi和资源类型j的 Rj ,其中i,j是分别表示进程数和资源类型的整数值。
The vertices V are partitioned into two different types of nodes: P = {P1, P2, ..., Pn}, the set consisting of all the active processes in the system, and R = {R1, R2, ..., Rm}, the set consisting of all the resource types in the system.
顶点V分为两个不同类型的节点: P = {P1,P2,...,Pn} ,该集合由系统中所有活动进程组成, R = {R1,R2,..., Rm} ,由系统中所有资源类型组成的集合。
A directed edge from process Pi to resource type Rj is denoted by Pi → Rj. It shows that process Pi is requesting an instance of resource type Rj and is called a request edge.
从Pi到资源类型Rj的有向边用Pi → Rj表示。 它显示进程Pi正在请求资源类型Rj的实例,并且被称为请求边缘。
A directed edge from resource type Rj to process Pi is denoted by Rj → Pi. It shows that an instance of resource type Rj is allocated to process Pi and is called an assignment edge.
从资源类型Rj到进程Pi的有向边用Rj → Pi表示。 它显示资源类型Rj的实例已分配给进程Pi ,称为分配边缘。
Pictorially, each process Pi is represented as a circle and each resource type Rj as a rectangle. Since there may be more than one instance of a resource, the instances are denoted by a dot within the rectangle.
如图所示,每个进程Pi表示为一个圆形,每个资源类型Rj表示为一个矩形。 由于资源的实例可能不止一个,因此实例在矩形内用一个点表示。
When process Pi requests and instance of a resource type Rj, a request edge is inserted in the graph. When the request is fulfilled, the request edge is instantaneously transformed into an assignment edge. When the process completes its work, it releases the resource; as a result, the assignment edge is deleted.
当进程Pi请求资源类型为Rj的实例时,请求边将插入图中。 当请求被满足时,请求边被立即转换为分配边。 流程完成工作后,将释放资源。 结果,分配边被删除。
Consider a few examples,
考虑几个例子,
The sets P, R, and E:
集合P,R和E:
- P = {P1, P2, P3}
- R = {R1, R2, R3, R4}
- E = {P1 → R1, P2 → R3, R1 → P2, R2 → P2, R2 → P1, R3 → P3}
Resource instances:
资源实例:
- One instance of resource type R1
- Two instances of resource type R2
- One instance of resource type R3
- Three instance of resource type R4
Fig 1.1 Resource Allocation Graph图1.1资源分配图
Process states:
流程状态:
- Process P1 is holding an instance of resource type R2 and is waiting for an instance of resource type R1.
- Process P2 is holding an instance of R1 and an instance of R2 and is waiting for an instance of R3.
- Process P3 is holding an instance of R3.
This resource allocation graph shows that there is no cycle in the graph. Therefore, no process in the system is deadlocked. If the graph does contain a cycle, then a deadlock may exist. This depends on two situations as follows.
此资源分配图显示图中没有循环。 因此,系统中的任何进程都不会陷入僵局。 如果图形确实包含一个循环,则可能存在死锁。 这取决于以下两种情况。
IMPORTANT POINTS TO NOTE:
注意要点:
If each resource type has exactly one instance, then a cycle implies that a deadlock has occurred. Each process in the cycle is deadlocked. In this case, a cycle in the graph is both a necessary and sufficient condition for the existence of deadlock.
如果每种资源类型仅具有一个实例,则循环表示发生了死锁。 循环中的每个进程都处于死锁状态。 在这种情况下,图中的循环既是存在死锁的必要条件,也是充分条件。
If each resource has several instances, then a cycle is not the necessary condition to determine that deadlock has occurred.
如果每个资源都有多个实例,则循环不是确定发生死锁的必要条件。
To illustrate this concept, we include a request edge from P3 → R2.
为了说明这个概念,我们包括一个从P3→R2的请求边。
Fig 1.2 Resource Allocation Graph (Cycle with deadlock)
图1.2资源分配图(带有死锁的周期)
Here, there are two cycles present in the system:
在这里,系统中存在两个周期:
P1 → R1 → P2 → R3 → P3 → R2 → P1
P2 → R3 → P3 → R2 → P2
CONCLUSION: Processes P1, P2, and P3 are deadlocked.
结论:进程P1,P2和P3处于死锁状态。
Now, consider another graph where the following cycle exists:
现在,考虑存在以下循环的另一个图:
P1 → R1 → P3 → R2 → P1
CONCLUSION: There is no deadlock. The process P4 may release its instance of resource type R2. That resource can then be allocated to P3, breaking the cycle.
结论:没有死锁。 过程P4可以释放其资源类型R2的实例。 然后可以将该资源分配给P3,从而中断周期。
Fig 1.3 Cycle but no deadlock
图1.3循环但无死锁
To summarize: If a resource allocation graph does not have a cycle, then the system is not in a deadlocked state. If there is a cycle, then the system may or may not be in a deadlocked state. When there are multiple instances of a resource type, the presence of a deadlock is determined using Banker's Algorithm. We will study that in further articles.
总结一下:如果资源分配图没有周期,则系统不会处于死锁状态。 如果存在周期,则系统可能处于死锁状态,也可能未处于死锁状态。 当资源类型有多个实例时,使用Banker算法确定死锁的存在。 我们将在后续文章中对此进行研究。
References:
参考文献:
Operating System Concepts, 8th edition, by author: Silberschatz Galvin Gagne.
操作系统概念,第8版,作者:Silberschatz Galvin Gagne。
Operating System Design/Concurrency/Deadlock
操作系统设计/并发/死锁
Deadlock
僵局
Hold and wait a process must be holding at least one
保持并等待一个进程必须至少持有一个
Chapter 6 Deadlocks
第六章僵局
翻译自: https://www.includehelp.com/operating-systems/introduction-of-deadlock.aspx
操作系统中的死锁