Process 为了避免死锁发生,可以使用N个资源运行的处理器的最大数量是多少?
亚马逊采访问题: 问题陈述非常简单。Process 为了避免死锁发生,可以使用N个资源运行的处理器的最大数量是多少?,process,operating-system,deadlock,Process,Operating System,Deadlock,亚马逊采访问题: 问题陈述非常简单。 假设我们有N个资源(所有资源彼此独立),那么可以同时运行的最大进程数是多少,至少使用其中一个资源,这样就不会出现死锁? 下面提供了一个示例,说明使用两个资源的两个进程可能处于死锁状态。 (来源:) 有人请详细回答我。这个问题不完整。只有在系统中同时满足以下所有条件时,才会发生死锁 (核对): 互斥:在一个数据库中必须至少保存一个资源 不可共享模式。任何时候只有一个进程可以使用该资源 给定时间的瞬间 保持并等待或资源保持:一个过程 当前至少拥有一个资源并请求
假设我们有N个资源(所有资源彼此独立),那么可以同时运行的最大进程数是多少,至少使用其中一个资源,这样就不会出现死锁?
下面提供了一个示例,说明使用两个资源的两个进程可能处于死锁状态。
(来源:)
有人请详细回答我。这个问题不完整。只有在系统中同时满足以下所有条件时,才会发生死锁 (核对):
PS:我认为这类问题更多的是为了了解你对概念的理解,而不是为了得到准确的答案。问题不完整。这个问题没有指定进程完成其执行所需的最大资源数 让我重新表述一下问题: 假设我们有N个资源(所有资源都相互独立),那么可以同时运行的最大进程数是多少,每个进程至少使用一个资源和最大M个资源,这样就不会出现死锁
回答: 让可用资源的数量=N,一个进程可以同时容纳的最大资源数量=M,进程数量=p 如果满足以下条件,则系统处于安全模式 N>=p(M-1)+1 最大进程数
p有人能解释一下吗?答案是:N*(N-1)
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.)