死锁(Deadlock)、活锁(LiveLock)、饥饿(Starvation)、挂起(Suspension)

最近在做并发缺陷的分类,发现不同学者对缺陷分类粒度不同,现对其中几个概念重述,欢迎交流!
本文以书面文字为准,附上说明来源。
若希望简单易懂的举例,请移步死锁,活锁和饥饿

死锁

1971年,Coffman总结系统产生死锁必定同时保持如下4个必要条件。

  1. 互斥条件(mutual exclusion):临界资源是独占资源,进程应互斥且排他地使用这些资源。
  2. 占有和等待条件(hold and wait):进程在请求资源得不到满足而等待时,不释放已占有资源。
  3. 不剥夺条件(no preemption):又称不可抢占,已获资源只能由进程自愿释放,不允许被其他进程剥夺。
  4. 循环等待(circular wait):又称环路条件,存在循环等待链,其中,每个进程都在等待链中等待下一个进程所持有的资源,造成这组进程处于永远等待状态。

前三个条件是死锁存在的必要条件,但不是充分条件。第四个条件是前三个条件同时存在时所产生的结果,故条件并不完全独立。但是单独考虑每个条件是有用的,只要破坏四个之一,就可以防止死锁

费翔林 骆斌《操作系统教程》(第五版)第三章 同步、通信与死锁

Deadlock happens when a thread(process) cannot proceed because it needs to obtain a resource which is held by another thread, while it holds a resource that the other thread needs. During deadlock , all involved threads are in a waiting state.
谷歌翻译:当一个线程(进程)由于需要获取另一个线程持有的资源,当前线程持有另一个线程所需要的资源,就会发生死锁。 在死锁期间,所有涉及的线程都处于等待状态。

Sara Abbaspour Asadollah. etc . 10 years of research on debugging concurrent and multicore software:a systematic mapping study .Software Qual J 2016

活锁

Livelock happens when a thread is waiting for a resource that will never become available, while the CPU is busy releasing and acquiring the share resource. It is similar to deadlock except that the state of the process involved in the livelock constantly changes and is frequently executing without making progress.
谷歌翻译:当线程正在等待永远不会变得可用的资源,而CPU忙于释放和获取共享资源时,就会发生Livelock。 它与死锁类似,不同之处在于,活锁中涉及的进程状态不断变化,并且频繁执行而没有取得进展。

Sara Abbaspour Asadollah. etc . 10 years of research on debugging concurrent and multicore software:a systematic mapping study .Software Qual J 2016

A thread often acts in response to the action of another thread. If the other thread’s action is also a response to the action of another thread, then livelock may result.

Livelocked threads are:

  • unable to make further progress (As with deadlock)
  • are not blocked — they are simply too busy responding to each other to resume work.

https://gerardnico.com/data/concurrency/livelock

饥饿

Starvation happens when a process is indefinitely delayed because other processes are always given preference. During starvation, at least one of the involved threads remains in the ready queue.
谷歌翻译:当某个进程无限期地延迟时会发生饥饿,因为总是优先考虑其他进程。 在饥饿期间,至少有一个涉及的线程保留在就绪队列中。

Sara Abbaspour Asadollah. etc . 10 years of research on debugging concurrent and multicore software:a systematic mapping study .Software Qual J 2016

挂起Suspension

Suspension occurs when a thread(that is not deadlocked)waits for an unacceptably long time in a queue to acquire a lock for accessing a shared resource.
谷歌翻译:当线程(未死锁)在队列中等待不可接受的长时间以获取用于访问共享资源的锁时,就会发生挂起。

Sara Abbaspour Asadollah. etc . 10 years of research on debugging concurrent and multicore software:a systematic mapping study .Software Qual J 2016

A (Thread) Execution Suspension can be caused:

  • by a execution suspension method
  • by a thread block (when it attempts to get a lock).
  • by a thread contention

https://gerardnico.com/data/concurrency/execution_suspension

四类缺陷对比

Deadlock Livelock Starvation Suspension
至少一个线程处于等待态
至少一个线程处于执行态
至少一个线程处于就绪态
所有线程持锁
至少一个线程持锁
所有线程都有对spinlock读和写的权限
所有线程都在等待其他线程持有的锁
至少一个线程在就绪队列等待了不可接受的长时间 in the ready queue for an unacceptably long time
至少一个线程在等待态等待了不可接受的长时间 waiting state
没有线程在处理和有进度proceed and progress
线程数比空闲处理器核数多

Sara Abbaspour Asadollah. etc . 10 years of research on debugging concurrent and multicore software:a systematic mapping study .Software Qual J 2016

livelock 获取不到资源时不会释放CPU,starvation会让出CPU

四种缺陷的进程状态对比

Linux操作系统进程5态模型
在这里插入图片描述

发布了27 篇原创文章 · 获赞 1 · 访问量 686

猜你喜欢

转载自blog.csdn.net/SUKI547/article/details/102678560