!!!Chapter 7 Deadlock

本文深入探讨了死锁的概念,包括必要条件、资源分配图和解决方法。通过阐述预防、避免死锁的策略,如互斥、持有等待、无预抢占和循环等待条件的管理,以及资源分配图算法的应用,旨在为系统设计者提供有效防止和检测死锁的指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A process requests resources; if the resources are not available at that time, the process enters a waiting state. Sometimes, a waiting process is never again able to change state, because the resources it has requested are held by other waiting processes. This situation is called a deadlock.

7.1 System Model

A process may utilize a resource in following sequence:

1. Request. The process requests the resource. If the request cannot be granted immediately, then the requesting process must wait until it can acquire the resource.

2. Use. The process can operate on the resource.

3. Release. The process releases the resource.

7.2 Deadlock Characterization

7.2.1 Necessary Conditions

1. Mutual exclusion. At least one resource must be held in a nonsharable mode; that is, only one process at a time can use the resource.

2. Hold and wait. A process must be holding at least one resource and waiting to acquire additional resources that are currently being held by other processes.

3. No preemption. Resources cannot be preempted; that is, a resource can be released only voluntarily by the process holding it, after that process has completed task.

4. Circular wait. A set{P0, P1...Pn} of waiting processes must exist such that P0 is waiting for resource hold by P1, P1 is waiting for...

7.2.2 Resource-Allocation Graph

System resource-allocation graph contains a set of vertices V and a set of edges E. Vertices V has two different types of nodes P = {P1, P2...} means active processes; R = {R1, R2...} means all resource types.

A directed edge Pi -> Rj is a request edge; a direct edge Rj -> Pi is called aassignment edge.

If the graph contains no cycles, then no process in the system is deadlocked. If the graph does contain cycle, then a deadlock may exist.

7.3 Methods for Handling Deadlocks

We can deal with deadlock in three ways:

  • User protocol to prevent or avoid deadlocks.
  • We can allow system enter deadlock, detect it, and recover
  • We can ignore the problem, pretend deadlock never occur.  (This is used by most OS)

7.4 Deadlock Prevention

Deadlock prevention provides a set of methods for ensuring that at least one of the necessary conditions cannot hold.

7.4.1 Mutual Exclusion

The mutual-exclusion condition must hold for nonsharable resources.

In general, however, we cannot prevent deadlocks by denying the mutual-exclusion condition, because some resources are intrinsically nonsharable.

7.4.2 Hold and Wait          Method 1

To ensure hold-and-wait never occur, we must guarantee that whenever a process requests a resource, it does not hold any other resources.

One protocol can require each process to request and be allocated all its resources before it begins execution.

Another protocol allows a process to request resources only when it has none.

- resource utilization may be low

- starvation is possible

7.4.3 No Preemption        Method 2

Protocol one: if a process is holding some resources and requests another resource that cannot be immediately allocated to it, then all resources the process is currently holding are preempted.

Protocol two: if a process request some resources, we first check whether they are available. If they are, we allocate them. If they are not, we check whether they are allocated to some other process that is waiting for additional resources. If so, we preempt the desired resources from the waiting process and allocate them to the requesting process.

This protocol is often applied to resources whose state can be easily saved and restored later.

7.4.4 Circular Wait           Method 3

One way to ensure this condition never hold is to impose a total ordering of all resource types and to require that each process requests resources in an increasing order of enumeration.

7.5 Deadlock Avoidance

An alternative method for avoiding deadlocks is to require additional information about how resources are to be requested.

The simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need.

A deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that a circular-wait condition can never exist.

7.5.1 Safe State

A system is in a safe state only if there exists a safe sequence.

A sequence of processes <P1, P2, ... Pn> is a safe sequence for the current allocation state if, for each Pi, the resource requests that Pi can still make can be satisfied by the currently available resources plus the resources held by all Pj, with j < i.


We can define avoidance algorithms that ensure the system will always remain in a safe state.

7.5.2 Resource-Allocation-Graph Algorithm

If the resource-allocation system has only one instance of each resource type, we can use resource-allocation-graph algorithm.

In addition to the request and assignment edges, we add a new type of edge calledclaim edge. A claim edge Pi ---> Rj indicates that process Pi may request resource Rj at some time in the future.
Suppose process Pi requests resource Rj. The request can be granted only if converting the request edge Pi ---> Rj to an assignment edge Rj ---> Pi does not result in the information of a cycle in the resource-allocation-graph. The cycle-detection algorithm takes n^2, n is the number of processes. 








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值