【Deadlock】

This article focuses on the following four parts:

1. Concept
of deadlock 2. Reasons for
deadlock 3. Necessary conditions
for deadlock 4. Basic methods for dealing with deadlock






1. The concept of deadlock

        refers to: multiple processes are running In the process, a deadlock (DeadlyEmbrace) caused by competing for resources, when the process is in this deadlock state, without external force, they will not be able to move forward.


Second, the causes of deadlock

        1. Competition for resources.

        When the number of resources shared by multiple processes in the system, such as printers, public queues, etc., is not enough to meet the needs of the processes, it will cause the processes to compete for resources and cause deadlocks.

        2. The advance order between processes is illegal.

        During the running process of the process, the order of requesting and releasing resources is not correct, which will also lead to process deadlock.


Third, the necessary conditions for deadlock

        1. Mutual exclusion conditions.

        Refers to: the process makes exclusive use of the allocated resources, that is, a certain resource is only occupied by one process within a period of time. If there are other processes requesting the resource at this time, the requester can only wait until the process occupying the resource is used up and released.

        2. Request and Hold Conditions.

        Refers to: the process has maintained at least one resource, but a new resource request has been made, and the resource has been occupied by other processes. At this time, the requesting process is blocked, but it keeps other resources that it has obtained.

        3. No deprivation of conditions.

        Refers to: the resources that the process has obtained cannot be deprived before they are used up, and can only be released by themselves when they are used up.

        4. Loop wait condition.

        Refers to: when a deadlock occurs, there must be a process-resource ring chain, that is, P0 in the process set {P0, P1, ..., Pn} is waiting for a resource occupied by P1; P1 is waiting for a resource occupied by P2; ...; Pn is waiting for a resource already occupied by P0.


Fourth, the basic method of dealing with deadlock

        1. Prevent deadlock

        by setting certain restrictions to destroy one or more of the four necessary conditions for deadlock to prevent deadlock from occurring.

        Deadlock prevention is an easier method to implement and has been widely used. However, the constraints imposed are often too strict, which may result in lower system resource utilization and system throughput.

        The way to prevent deadlock is to make one of the 2nd, 3rd, and 4th conditions in the four necessary conditions fail to hold to avoid deadlock. As for the necessary condition 1, because it is determined by the inherent characteristics of the equipment, not only can it not be changed, but it should also be guaranteed.

        (1) Abandon the "request and hold" condition

        In this way, the system stipulates that all processes must apply for all the resources they need in the entire running process at one time before starting to run. At this point, if the system has enough resources allocated to a process, it can allocate all the resources it needs to the process, so that the process will no longer ask for resources during the entire running period, thus abandoning the request condition.

        However, when allocating resources, as long as there is one resource that cannot meet the requirements of a process, even if all other required resources are idle, it will not be allocated to the process, and the process will wait. Since the process does not hold any resources while it is waiting, the hold condition is also discarded, thus avoiding deadlocks.

            1) Advantages: simple, easy to implement, and very safe.

            2) Disadvantages:

                A. Resources are seriously wasted.

                Because a process obtains all the resources required for its entire running process at one time and monopolizes the resources, some of which may be rarely used, or even unused during the entire running period, which seriously deteriorates the utilization of system resources Rate.

                B. Make the process run with a delay.

                Only when a process has obtained all the resources it needs, it can start running, but some resources may have been occupied by other processes for a long time, and the process waiting for the resource may be delayed to run.

        (2)摒弃“不剥夺”条件

        采用这种方式时,进程是逐个地提出对资源的要求的。当一个已经保持了某些资源的进程,再提出新的资源请求而不能立即得到满足时,必须释放它已经保持了的所有资源,待以后需要时再重新申请。这意味着,某一进程已经占有的资源,在运行过程中会被暂时地释放掉,也可认为是被剥夺了,从而摒弃了“不剥夺”条件。

        1)缺点:

        实现比较复杂,且要付出很大的代价。因为一个资源在使用一段时间后,它的被迫释放可能会造成前段工作的失效,即使是采取了某些防范措施,也还会使进程前后两次运行的信息不连续;此外,还可能因为反复地申请和释放资源,致使进程的执行被无限地推迟,这不仅延长了进程的周转时间,而且也增加了系统开销,降低了系统吞吐量。

        (3)摒弃“环路等待”条件。

        采用这种方法时,系统将所有资源按类型进行线性排队,并赋予不同的序号。例如:输入机序号为1,打印机序号为2,磁带机为3,磁盘为4。所有进程堆资源的请求必须严格按照资源序号递增的次序提出,这样,在所形成的资源分配图中,不可能再出现环路,因而摒弃了“环路等待”条件。

        1)优点:与前两种方法相比,这种方法的资源利用率和系统吞吐量都有较明显的改善。

        2)缺点:

            A. 为系统中各类资源所分配(确定)的序号必须相对稳定,这就限制了新类型设备的增加。

            B. 尽管在为资源的类型分配序号时,已经考虑到大多数作业在实际使用这些资源时的顺序,但也经常发生这种情况:作业(进程)使用各类资源的顺序与系统规定的顺序不同,这就会造成对资源的浪费。

            例如:某进程先用磁带机,后用打印机,但按系统规定,该进程应先申请打印机后申请磁带机,致使先获得的打印机被长时间闲置。

            C. 为方便用户,系统对用户在编程时所施加的限制条件应尽量少。然而这种按规定次序申请的方法,必然会限制用户简单、自主地编程。


        2. 避免死锁。

        该方法同样属于事先预防的策略,但它并不需要事先采取各种限制措施去破坏产生死锁的四个必要条件。而是在资源的动态分配过程中,用某种方法去防止系统进入不安全状态,从而避免发生死锁。

        这种方法只需要事先施加较弱的限制条件,便可获得较高的资源利用率及系统吞吐量,但在实现上有一定的难度。

        最著名的避免死锁的算法:Dijkstra的银行家算法。


        3. 检测死锁。

        这种方法不需要事先采取任何限制性措施,也不必检查系统是否已经进入不安全区,而是允许系统在运行过程中发送死锁。

        但可通过系统所设置的检测机构,及时地检测出死锁的发生,并精确地确定与死锁有关的进程和资源;然后,采取适当措施,从系统中将已发生的死锁清除掉。


        4. 解除死锁。

        它是与检测死锁相配套的一种措施。当检测到系统中已发生死锁时,需将进程从死锁状态中解脱出来。

            方法(1):剥夺资源。从其它进程剥夺足够数量的资源给死锁进程,以解除死锁状态。

            方法(2):撤销进程。使全部死锁进程都夭折掉;或者,按照某种顺序逐个地撤销进程,直至有足够的资源可用,使死锁状态消除为止。

        死锁的检测和解除措施有可能使系统获得较好的资源利用率和吞吐量,但在实现上难度也最大。




整理时重点参考:《计算机操作系统》(第三版) -- 汤小丹、梁红兵、哲凤屏、汤子瀛 编著

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327057454&siteId=291194637