Operating Systems - Deadlock

Concurrency: Deadlock and starvation

Deadlock

basic concept

Deadlock: a set of processes between competing system resources or communicate "permanent" congestion situation. When each process a group process are waiting for an event, and the only other blocked processes in this group process can trigger the event, this group of processes to deadlock. Because there is no event can be triggered, so deadlock is permanent.

 

Livelock: refers to the task or performer is not blocked, because some conditions are not met, resulting in repeated attempts - failed - attempt - failure of the process. Entity is active lock is in constant state of change, it is possible to live on their own unlock the lock. (Similar to the process of constant humility)

 

Reusable Resources: You can only use for a process safely and without the use of depleted resources. The processor / io channels, memory external storage device and a file, database, and semaphores.

 

Consumable resources: can be created (production) and destruction (consumption) of resources. The number of such resources does not appear to produce a non-blocking process can create any number of such resources. When a consumer process to get a resource when the resource ceases to exist. Such interrupt information signals, messages, and I / O buffer.

 

Resource allocation map: characterization of process resource allocation directed graph. reference

image.pngimage.pngimage.png

 

Deadlock conditions

The main reason Deadlock including competition system resources, and promote the process of the order illegal.

Deadlock has three necessary conditions:

  1. mutually exclusive: Only one process may use a resource. Other processes can not access resources have been allocated to other processes 

  1. Possession and wait: When a process waits for another process, continue to occupy the resources allocated
  2. Not seize: resources are not forced to seize possession of the process has

The first three conditions are met, a series of events that can occur can cause circulation unsolvable wait. The unsolvable deadlock cycle is actually waiting for the definition, and therefore there is a sufficient condition:

  1. Loop wait: there is a closed process chain, so that each process occupies at least one resource to the next process in the chain needs.

Deadlock processing strategy

 

in principle

Resource allocation policy

Program

advantage

Shortcoming

prevention

Conservative, would rather idle resources

A one-time request all resources

Very effective process to perform a series of activities; do not need to seize

Initialization inefficient, delayed the process

You must know the future resource request

seize

Resources for the state saved and restored easily

Too often not necessary to seize

Sort resources

When the detection may be implemented by compiling

Need not be computed at run time

Inconvenience flexible application of new resources

avoid

Run-time judge

Operation to find at least a safe path

不需要抢占

必须知道将来的资源请求

进程不能被长时间阻塞

检测

非常自由,只要允许就分配

周期性地调用以测试死锁

不会延迟进程的初始化

易于在线处理

通过抢占解除死锁,造成损失

死锁的预防

防止死锁的发生只需要破坏死锁产生的四个条件之一即可。

1.破坏互斥条件

    允许系统的资源都能共享使用,但是有些资源根本不能同时访问,如打印机等。因此,破坏互斥不太可行

2.破坏占有且等待

    要求进程一次性地请求所有需要的资源,并且阻塞这个进程直到所有的请求都满足。但是这个方法是低效率的,一方面进程可能被长时间阻塞。另一方面,分配给一个进程的资源可能长时间不会使用到,但是在此期间,也不能被其他进程使用。

3.破坏不可抢占

   如果占有某些资源的进程进一步申请资源被拒绝的时候,该进程必须释放它最初占有的资源。

   一个进程请求当前被另一个进程占有的一个资源,操作系统可以抢占另一个进程,要求它释放资源,要求优先级不同。

4.破坏循环等待

   定义资源类型的线性顺序,如果一个进程分配到了R型,则接下来的资源只能是排在R型之后的资源类型。

死锁的避免

死锁的避免同样也是属于事先预防的策略,但并不是事先采取某种限制措施破坏死锁的必要条件,而是在资源动态分配过程中,防止系统进入不安全状态,以避免发生死锁。死锁避免允许三个必要条件,限制条件更弱,能够获得更好的系统性能和允许更多的并发。

 

死锁避免不需要死锁预防中的抢占和回滚进程,限制也少。但是,死锁避免的限制在于:必须事先声明每个进程请求的最大资源,所讨论的进程是无关的,没有同步要求的限制,分配的资源数目必须是固定的,在占有资源的时候,进程不能退出。

1.进程启动拒绝

 如果一个进程的请求会导致死锁,则不启动此进程。

 在分配资源给进程之前需要考虑是否会导致系统进入不安全状态。

 安全状态指的是系统能按某种进程推进顺序,为每个进程分配所需资源,直到满足每个进程对资源的最大需求,使得每个进程都可顺序地完成。如果不存在这样的序列,则系统就是不安全的。系统进入不安全状态,就有可能进入死锁状态,反之,只要系统处于安全状态,系统便可避免进入死锁状态。

2.资源分配拒绝

 资源分配拒绝策略又称之银行家算法。进程向操作系统请求分配资源,操作系统按照规则为进程分配资源。当进程首次申请资源的时候,要测试该进程对资源的最大需求量,如果系统现存的资源可以满足它的最大需求则进行分配,否则就推迟分配。当进程在执行中继续申请资源的时候,先测试该进程已占用的资源与本次申请的资源数之和是否超过了它自身的最大需求量,如果超过则拒绝,如果没有超过再测试系统现存的资源能否满足进程的需要。若能则分配,否则延迟分配。

 

数据结构描述:

struct state{
    int resource[m];    //表示资源数目 int available[m]; //未分配给进程的每种资源的总量 int claim[n][m]; //进程对资源的最大需求 有时也用max表示 int allocation[n][m] //n是进程数,m是资源种类数目 }

银行家算法描述:

image.png

安全性算法

image.png

举例说明

image.png

image.png

 

image.png

 

image.png

 

image.png

 

image.png

死锁的检测

系统在分配资源的时候不采取任何措施,而是周期性地执行一个算法(前面的安全性算法)检测死锁条件4-循环等待条件。

死锁定理-资源分配图

1. In the resource allocation way to find out is neither obstruction are not solitary point of the process Pi, eliminate all its requests and distribution side edges, making it an isolated node.

2. The process of Pi released resources can wake up some blocking process. The original blocking processes may become non-blocking process. Repeat 1)

If all erased edges, the graph can be simplified and deadlock does not exist, otherwise exist.

Deadlock lift

  •  Cancel all the process deadlock
  •  Each deadlock the process to roll back to the previous checkpoint, and restart all processes.
  •  Continuous cancel the process deadlock until there are no deadlocks.
  •  Continuous seize resources until deadlock no longer exists. Preempted process needs to be rolled back.

Guess you like

Origin www.cnblogs.com/suntorlearning/p/11031247.html