使用N个资源可以运行的最大处理器数是多少,这样就不会发生死锁?

时间:2013-07-12 04:58:56

标签: process operating-system deadlock

亚马逊访谈问题:

问题陈述非常简单 假设我们有N个资源(都是彼此独立的),那么使用至少一个资源可以同时运行的最大进程数是多少,这样就不会出现死锁?
下面提供了一个示例,显示使用两个资源的两个进程可能处于死锁状态 Two process in deadlock http://www.cs.ucla.edu/classes/spring09/cs111/scribe/10/images/figure1.jpg

有人请详细说明答案。

2 个答案:

答案 0 :(得分:0)

问题不完整。只有在系统中同时存在以下所有条件时,才会发生死锁 (查看https://en.wikipedia.org/wiki/Deadlock):

  1. 相互排斥:必须至少保留一个资源 不可共享模式。只有一个进程可以在任何进程中使用该资源 给定的时间。
  2. 等待或持有资源:一个过程 目前至少持有一个资源并请求额外资源 其他过程持有的资源。
  3. 没有先发制人:资源只能由持有它的进程自愿释放。
  4. 循环等待:进程必须等待资源 由另一个进程持有,而另一个进程正在等待 第一个发布资源的过程。一般来说,有一套 等待进程,P = {P1,P2,...,PN},这样P1正在等待 由P2持有的资源,P2正在等待P3和持有的资源 等等,直到PN等待P1持有的资源。
  5. 所以,这里有不同的假设和相应的答案:

    案例1:资源是非排他性的,或者可以被抢占或者有任何其他机制来避免死锁(明显的答案)

    然后,任何数量的处理器都可以运行。 作为旁注:当然,根据具体情况,您可能希望限制要避免/减少contention的进程数。

    案例2:以前没有条件

    然后,如果你想避免死锁,没有任何逻辑可以避免死锁,一次只能运行一个进程。只要您向系统添加另一个进程,就会出现示例中描述的情况(P1具有Ra并且需要Rb,P2具有Rb并且需要Ra)。

    PS:我认为这更像是一种问题,要求您了解这些概念,而不是获得确切的答案。

答案 1 :(得分:0)

问题不完整。该问题没有指定进程完成执行所需的最大资源数。

<块引用>

让我重新表述这个问题:

假设我们有N个资源(都是相互独立的),那么可以同时运行的最大进程数是多少,至少使用其中一个资源,最大M个资源由每个进程,这样就不会出现死锁?


答案:

设,可用资源数=N,一个进程可以同时持有的最大资源数=M,进程数=P。

如果满足以下条件,则系统处于安全模式。

N >= P ( M - 1 ) + 1

最大进程数,

P <= ( N - 1) / ( M - 1)

<块引用>

例如,如果可用资源数为6(N=6),每个进程最多使用2(M=2)个资源完成执行,则最大进程数为5,系统仍然安全.


Let, illustrate another scenario. the number of available resources is 6 (N=6) and each process uses at most 3 (M=3) resources to finish execution
Now, we have 6 processes in the system. All possible resources allocations are:

    P1  |   P2  |   P3  |   P4  |   P5  |   P6  |   Available   |   Safe    |   Comment 
        |       |       |       |       |       |   resources   |           |           
--------|-------|-------|-------|-------|-------|---------------|-----------|-----------------------------------------------------------
    1   |   1   |   1   |   1   |   1   |   1   |       0       |   No      |each process requires at least 2 resources to finish
        |       |       |       |       |       |               |           |But no resource is available
    
Now, we have 5 processes in the system. All possible resources allocations are:

    P1  |   P2  |   P3  |   P4  |   P5  |   Available   |   Safe    |   Comment 
        |       |       |       |       |   resources   |           |
--------|-------|-------|-------|-------|---------------|-----------|-----------------------------------------------------------
    1   |   1   |   1   |   1   |   1   |       1       |   No      |each process requires at least 2 resources to finish
        |       |       |       |       |       |       |           |But only one resource is available
--------|-------|-------|-------|-------|---------------|-----------|-----------------------------------------------------------
    2   |   1   |   1   |   1   |   1   |       0       |   No      |P1 requires at least 1 resource to finish
        |       |       |       |       |       |       |           |But no resource is available

Now, we have 4 processes in the system. All possible resources allocations are:

    P1  |   P2  |   P3  |   P4  |   Available   |   Safe    |   Comment 
        |       |       |       |   resources   |           |
--------|-------|-------|-------|---------------|-----------|-----------------------------------------------------------
    1   |   1   |   1   |   1   |       2       |   Yes     |
--------|-------|-------|-------|---------------|-----------|-----------------------------------------------------------
    2   |   1   |   1   |   1   |       1       |   Yes     |P1 requires at least 1 resources to finish
        |       |       |       |               |           |and 1 resource is available. So the system is safe for this allocation.
--------|-------|-------|-------|---------------|-----------|-----------------------------------------------------------
    2   |   2   |   1   |   1   |       0       |   No      |

Now, we have 3 processes in the system. All possible resources allocations are:

    P1  |   P2  |   P3  |   Available   |   Safe    |   Comment 
        |       |       |   resources   |           |
--------|-------|-------|---------------|-----------|-----------------------------------------------------------
    1   |   1   |   1   |       3       |   Yes     |
    2   |   1   |   1   |       2       |   Yes     |
    2   |   2   |   1   |       1       |   Yes     |
    2   |   2   |   2   |       0       |   No      |

Now, we have 2 processes in the system. All possible resources allocations are:

    P1  |   P2  |   Available   |   Safe    |   Comment 
        |       |   resources   |           |
--------|-------|---------------|-----------|-----------------------------------------------------------
    1   |   1   |       4       |   Yes     |
    2   |   1   |       3       |   Yes     |
    2   |   2   |       2       |   Yes     |
If the maximum no of processes is 2, the system will be safe whatever the resources allocations are.

So using the given formula above, 
For, the number of available resources, N = 6 
And the maximum number of resources required by each process, M = 3
The maximum number processes, P <= (6-1) / (3-1) = 2.5 = 2 (apx.)