操作系统中的死锁_操作系统中的死锁介绍

操作系统中的死锁

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


    • Introduction to Deadloack | Image 1


      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的请求边。

Introduction to Deadloack | Image 2


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,从而中断周期。

Introduction to Deadloack | Image 3


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

操作系统中的死锁

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

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

相关文章

《云数据管理:挑战与机遇》2.3 数据库系统

本节书摘来自华章出版社《云数据管理》一书中的第2章,第3节,作者迪卫艾肯特阿格拉沃尔,更多章节内容可以访问云栖社区“华章计算机”公众号查看本节中,我们将为数据库系统中的一些主要概念提供一个相当抽象、简洁和高层次的描述。…

sql server与oracle的分页,详解SQLServer和Oracle的分页查询

不管是DRP中的分页查询代码的实现还是面试题中看到的关于分页查询的考察,都给我一个提示:分页查询是重要的。当数据量大的时候是必须考虑的。之前一直没有花时间停下来好好总结这里。现在又将Oracle视频中关于分页查询的内容看了一遍,发现很容…

java treemap_Java TreeMap lastEntry()方法与示例

java treemapTreeMap类的lastEntry()方法 (TreeMap Class lastEntry() method) lastEntry() method is available in java.util package. lastEntry()方法在java.util包中可用。 lastEntry() method is used to return the entry (key-value pairs) that exists with the large…

LeetCode OJ 之 Valid Anagram

题目: Given two strings s and t, write a function to determine if t is an anagram of s. For example,s "anagram", t "nagaram", return true.s "rat", t "car", return false. Note: You may assume the string…

oracle光标位置无效,解决在Form表单中光标移动不了问题

apply p8727236_10123 for Developer Suite 10.1.2.3 in Linux首先到oracle的技术支持下载所需补丁,然后1先打补丁7121788,把p7121788_10123_LINUX.zip解压到/home/oracledev目录下(ORACLE_HOME为/u01/app/oracledev/OraHome_dev)$cd /home/oracledev/7121788$expo…

java treemap_Java TreeMap HigherKey()方法与示例

java treemapTreeMap类HigherKey()方法 (TreeMap Class higherKey() method) higherKey() method is available in java.util package. HigherKey()方法在java.util包中可用。 higherKey() method is used to return the lowest key value element higher than the given key e…

centos配置ipv6地址

首先打开网站注册一个账号:http://www.tunnelbroker.net创建一个ipv6的地址:把下面的命令在linux上执行一遍,这个方式是临时生效,重启网卡和重启系统自动失效。把上面的命令保存到一个配置文件中:vi /etc/sysconfig/ne…

php oracle 需要libmysql.dll么_,Windows7环境下Apache+PHP+MySQL完美配置

写作此篇文章的目的在于记录Windows 7环境下成功配置WAMP环境, 初学者在不使用整合好的WAMPServer和XAMPP的情况下徒手配置整合环境貌似有很多意想不到的问题. 这将是我们需要讨论的.我将重现几个经典的问题, 并一一排除. 希望对各位看官有点借鉴作用.一. Apache在整合PHP后无法…

stringreader_Java StringReader skip()方法与示例

stringreaderStringReader类skip()方法 (StringReader Class skip() method) skip() method is available in java.io package. skip()方法在java.io包中可用。 skip() method is used to skip the given number of characters in the stream. skip()方法用于跳过流中给定数量的…

NFS部署及优化(一)

NFS部署及优化(一)一、NFS的基本概念NFS network file system 网络文件系统必然通过网络通信来实现文件的访问和写入,所以做这个实验的话最好有两台虚拟机配置:A:一个192.169.50.201为server端B:一个192.169.50.200为…

oracle 11g跳过坏块,oracle 使用Dbms_Repair跳过坏块

原博文:http://blog.chinaunix.net/uid-77311-id-3051382.html使用Dbms_Repair跳过坏块步骤1:表tb_test中有坏块(模拟坏块同方法1)SQL> select count(1) from hxl.tb_test;select count(1) from hxl.tb_test*ERROR at line 1:ORA-01578: ORACLE data block corru…

strictmath_Java StrictMath nextUp()方法与示例

strictmathStrictMath类nextUp()方法 (StrictMath Class nextUp() method) Syntax: 句法: public static float nextUp(float fl);public static double nextUp(double do);nextUp() method is available in java.lang package. nextUp()方法在java.lang包中可用。…

并发数据结构-1.1 并发的数据结构的设计

原文链接,译文链接,译者:董明鑫,校对:周可人 随着多个处理器共享同一内存的机器在商业上的广泛使用,并发编程的艺术也产生了巨大的变化。当前的趋势向着低功耗芯片级多线程(CMT)发展…

printstream_Java PrintStream close()方法与示例

printstreamPrintStream类close()方法 (PrintStream Class close() method) close() method is available in java.io package. close()方法在java.io包中可用。 close() method is used to close the underlying output stream. close()方法用于关闭基础输出流。 close() meth…

oracle底层执行顺序,select语句结构与执行顺序-Oracle

select语句结构与执行顺序select语句的结构与执行顺序,下面的序号代表执行顺序8 SELECT (9)DISTINCT11 1 ROM 3   JOIN 2   ON 4 WHERE 5 GROUP BY 6 WITH {CUBE | ROLLUP}7 HAVING 10 ORDER BY 补…

HDU 4923 Room and Moor(瞎搞题)

瞎搞题啊。找出1 1 0 0这样的序列,然后存起来,这样的情况下最好的选择是1的个数除以这段的总和。然后从前向后扫一遍。变扫边进行合并。每次合并。合并的是他的前驱。这样到最后从t-1找出的那条链就是最后满足条件的数的大小。Room and Moor Time Limit:…

java define_Java Long类的define()方法与示例

java define长类解码()方法 (Long class decode() method) decode() method is available in java.lang package. 在java.lang包中提供了define ()方法 。 decode() method is used to decode the given String value into a Long value. encode()方法用于将给定的String值解码…

linux修改文件用户组,linux命令 修改文件、文件夹所属用户、用户组

最近学习hadoop,在替换配置文件的时候,发现老是报错,没有权限替换。我们知道如何改变文件的用户组与拥有者了,那么,什么时候要使用chown或chgrp呢?或许你会觉得奇怪吧?是的,确实有时…

Kotlin 开篇

Kotlin 是一个基于 JVM 的新的编程语言,由 JetBrains 开发官网地址:http://kotlinlang.org。JetBrains,作为目前广受欢迎的 Java IDE IntelliJ 的提供商,在 Apache 许可下已经开源其Kotlin 编程语言。开源地址:https:/…

inputstream示例_Java InputStream close()方法与示例

inputstream示例InputStream类close()方法 (InputStream Class close() method) close() method is available in java.io package. close()方法在java.io包中可用。 close() method is used to close this InputStream and free all system resources linked with this stream…