操作系统中的死锁

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)

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

    一个系统由有限数量的资源组成,并且这些资源被分为几种类型,每种类型都由一定数量的相同实例组成。

  2. 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周期,文件等。例如,如果系统有两台打印机,则资源类型“打印机”有两个实例。

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

    这些实例可以相同或不同。 如果实例相似,则可以将任何实例分配给该流程,否则,则需要将两个打印机定义为单独的资源类。

  4. A process must request the resource before using it and must release it after use.

    进程必须在使用资源之前请求资源,并且在使用之后必须释放资源。

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

    进程可以请求完成任务所需的资源,但是它不应超过系统中存在的资源总数。

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

资源分配图主要用作检测死锁的方法。 当我们阅读有关死锁处理机制时,我们将讨论此功能。

  1. 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组成

  2. 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是分别表示进程数和资源类型的整数值。

  3. 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} ,由系统中所有资源类型组成的集合。

  4. A directed edge from process Pi to resource type Rj is denoted by PiRj. It shows that process Pi is requesting an instance of resource type Rj and is called a request edge.

    Pi到资源类型Rj的有向边用PiRj表示。 它显示进程Pi正在请求资源类型Rj的实例,并且被称为请求边缘。

  5. A directed edge from resource type Rj to process Pi is denoted by RjPi. It shows that an instance of resource type Rj is allocated to process Pi and is called an assignment edge.

    从资源类型Rj到进程Pi的有向边用RjPi表示。 它显示资源类型Rj的实例已分配给进程Pi ,称为分配边缘。

  6. 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表示为一个矩形。 由于资源的实例可能不止一个,因此实例在矩形内用一个点表示。

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

注意要点:

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

    如果每种资源类型仅具有一个实例,则循环表示发生了死锁。 循环中的每个进程都处于死锁状态。 在这种情况下,图中的循环既是存在死锁的必要条件,也是充分条件。

  2. If each resource has several instances, then a cycle is not the necessary condition to determine that deadlock has occurred.

    如果每个资源都有多个实例,则循环不是确定发生死锁的必要条件。

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

操作系统中的死锁

操作系统中的死锁_操作系统中的死锁介绍相关推荐

  1. 线程中如何使用对象_多线程中如何使用gdb精确定位死锁问题

    在多线程开发过程中很多人应该都会遇到死锁问题,死锁问题也是面试过程中经常被问到的问题,这里介绍在c++中如何使用gdb+python脚本调试死锁问题,以及如何在程序运行过程中检测死锁. 首先介绍什么是 ...

  2. 操作系统hpf算法事例_操作系统调度算法是什么

    操作系统调度算法是什么 导读:小编根据大家的需要整理了一份关于<操作系统调度算法是什么>的内容,具体内容:学习操作系统的朋友们肯定遇到过调度算法,目的是控制资源使用者的数量,选取资源使用者 ...

  3. 中累计直方图_试验研究中的利器强大的直方图和箱线图

    上次小编给大家介绍了跟误差线有关的几个概念以及相关的柱状图,散点图,和小提琴图(试验数据统计中常用的 量,图,和线--再也不担心文章的统计用图了!).这些图和线都属于"比较统计学" ...

  4. c# mysql代码中写事务_代码中添加事务控制 VS(数据库存储过程+事务) 保证数据的完整性与一致性...

    [c#]代码库代码中使用事务前提:务必保证一个功能(或用例)在同一个打开的数据连接上,放到同一个事务里面操作. 首先是在D层添加一个类为了保存当前操作的这一个连接放到一个事务中执行,并事务执行打开同一 ...

  5. eof在c语言中表示什么_日语中的鍵为什么既能表示“钥匙”也能表示“锁”?...

    我们知道,日语中的「鍵(かぎ)」表示"钥匙"的意思,例如:(1)玄関(げんかん)の鍵をなくした.房门钥匙弄丢了.但同时还能表示"锁"的意思.例如:(2)納戸(な ...

  6. sql活动监视器 死锁_监视SQL Server死锁–简单方法

    sql活动监视器 死锁 SQL Server is a very powerful tool and wherever I go, I see the tool being way much unde ...

  7. sql server死锁_了解SQL Server死锁图的图形表示

    sql server死锁 #contentor img { float: left; } #contentor img { float: left; } 介绍 (Introduction) If yo ...

  8. 操作系统 非连续分配_操作系统中的连续和非连续内存分配

    操作系统 非连续分配 In this article, we will learn about the different types of memory management techniques ...

  9. 排序算法中平均时间复杂度_操作系统中的作业排序(算法,时间复杂度和示例)...

    排序算法中平均时间复杂度 作业排序 (Job sequencing) Job sequencing is the set of jobs, associated with the job i wher ...

最新文章

  1. mybatisplus 结果_Java之MyBatis Plus介绍
  2. TensorFlow入门(二)简单前馈网络实现 mnist 分类
  3. Python开发工程师必知十大机器学习库
  4. 我的Java教程,不断整理,反复学习,记录着那些年大学奋斗的青春
  5. vue 入口main.js 调用app.vue 入口页面以及触发第三方登陆校验
  6. 请领导批阅文件怎么说_刚到公司,应该怎么喊领导,别直接说名字,高情商这样称呼...
  7. Android programming on Mac 之安装Eclipse
  8. 阶乘数码(洛谷P1591题题解,Java语言描述)
  9. Git常用的步骤和命令
  10. 最好的PDF阅读器,Foxit Reader绿色V8.0
  11. CTF-Crypto-RSA整理
  12. Xshell 5下载详细教程
  13. Qt网络编程01-QTcpSocket和QTcpServer的基本使用
  14. 个人如何申请注册公司邮箱?企业邮箱注册申请流程详解
  15. 利用js脚本自动删除微博
  16. 如何将EXCEL数据表里面的数据逆时针旋转90度
  17. 最新可用的电脑开机密码查看工具
  18. 例25:求1+2!+3!+...+20!的和。
  19. 什么是节点流和处理流?
  20. Home Assistant 接入 Lifesmart(云起智能)家居的途径。

热门文章

  1. python连接redis哨兵_Python redis.sentinel方法代码示例
  2. java矩形翻转_如何判断一个点在旋转后的矩形中
  3. excel单元格斜线_怎么在excel中画斜线?怎么在excel表格中画斜线?
  4. Qt图形界面编程入门(基本窗口及控件)
  5. 如果删除网上服务器登陆账号密码,怎么清除SVN的用户名和密码
  6. python几何拼贴画_图形几何图形拼贴画
  7. android app应用后台休眠,安卓手机锁屏后程序自动关闭,怎么设置手机app允许锁屏后台运行...
  8. 计算图片相似度的方法
  9. MFC鼠标OnMouseHover使用
  10. POJ 3422 费用流