OS——Deadlocks

1 System Model

Each process utilizes a resource as follows:
request use Release

2 Deadlock Characterization

  • 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:
  • Resource Allocation Graph
    A deadlock situation can arise if the above four conditions hold simultaneously
    V is partitioned into two different types:
    – P = {P1, P2, …, Pn}, the set consisting of all the processes in the system.
    – R = {R1, R2, …, Rm}, the set consisting of all resource types in the system.
    E is classed into two different types:
    – request edge
    – assignment edge
    If graph contains no cycles => no deadlock.
    If graph contains a cycle =>
    – if only one instance per resource type, then deadlock.
    – if several instances per resource type, possibility of deadlock( Requirement)

3 Methods for Handling Deadlocks

  • Ensure that the system will never enter a deadlock state.
    – Deadlock-prevention
    – Deadlock-avoidance
  • Allow the system to enter a deadlock state, detect it, and then recover.
  • Ignore the problem and pretend (认为) that deadlocks never occur in the system; used by most operating systems, including UNIX

.

4 Deadlock Prevention

Restrain the ways request can be made.

Mutual Exclusion — must hold for nonsharable resources.
Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources.
Two alternations:
1. Require process to request and be allocated all its resources before it begins execution
2. allow process to request resources only when the process has none (release resources first).
No Preemption

  • To ensure No Preemption does not hold, the protocol as follows is used if P1 is now holding some resources R1 and requests another resource R2, which is now occupied by P2 and cannot be immediately allocated to P1.
  • for example, P2 is in the running state and using R2, then P1 turns into the waiting state and R1 held by P1 is released
  • preempted resources R1 is added to the list of resources for which the process P3 is waiting, so the waiting process P3 may be enabled to run the resources-preempted process
  • P1 will be restarted only when it can regain its old resources, as well as the new ones that it is requesting
    - if P1 requests R1 that is not available, and if R1 is allocated to a waiting process P2 that is waiting for another resource R2, thenP1 preempts R1 from the waiting process P2

Circular Wait each process requests resources in an increasing order of enumeration.
Two alternations:(协议)
– The process can request instances of resource type Rj if and only if F(Rj)>F(Ri). If several instances of the same resource type are needed, a single request for all of them must be issued.
– Whenever a process requests an instance of resource type Rj, it has released any resources Ri such that F(Ri)>=F(Rj)

5 Deadlock Avoidance

  • maximum number of resources

  • deadlock-avoidance algorithm

  • Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes.

  • Approaches to Deadlock Avoidance
    – Do not start a process if its demands might lead to deadlock.
    – Do not grant an incremental resource request to a process if this allocation might lead to deadlock

  • Safe State
    System is in safe state if there exists a safe sequence of all processes. Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j< i.
    – If the resources that Pi needs are not immediately available, then Pi can wait until all Pj have finished.
    – When Pj is finished, Pi can obtain needed resources, execute, return its allocated resources, and terminate.
    – When Pi terminates, Pi+1 can obtain its needed resources, and so on.

Resource-Allocation Graph Algorithm

In a resource-allocation system with only one instance of each resource type.
– Pi requests Rj.
The request can be granted only if converting Pi → Rj to Rj → Pi does not result in the formation of a cycle in the resource-allocation graph.
– check for safety by using a cycle-detection algorithm.

Banker’s Algorithm

Applicable to a resource-allocation system with multiple instances of each resource type.
Each process must a priori claim maximum use of instances of each resource type. When a process requests a set of resources, the system must determine whether the allocation of these resources will leave the system in a safe state.
Need[i][j] = Max[i][j] – Allocation[i][j]

  • Safety Algorithm
    needi (<=)Work
  • Resource-Request Algorithm for Process Pi

6 Deadlock Detection

– systems with only a single instance of each resource type

a deadlock exists in the system <==>the wait-for graph contains a cycle.

-systems with several instances of each resource type

Detection Algorithm
Requesti <=Work

7 Recovery from Deadlock

To break a deadlock
– Abort one or more processes.
– Preempt some resources from one or more of the deadlocked processes

Process Termination

To eliminate deadlock by aborting a process
– Abort all deadlocked processes.
– Abort one process at a time until the deadlock cycle is eliminated.
System reclaims all resources allocated to the terminated processes.
In which order should we choose a process to abort?
– Priority of the process.
– How long the process has computed, and how much longer to completion.
– How much and what type of resources the process has used.
– How much more resources the process needs to complete.
– How many processes will need to be terminated.
– Is the process interactive or batch?

Resource Preemption

successively preempt some resources from processes and give these resources to other processes until the deadlock cycle is broken.
Selecting a victim – which resources and which processes are to be preempted? Minimize cost.
Rollback (回滚)– return the process to some safe state, and restart it from that state. Abort the process and then restart it.
Starvation – the same process may always be picked as victim. Include the number of rollbacks in cost factor.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值