0

Amazon Interview Question:

The problem statement is very simple.
Suppose we have N number of resources (all are independent from each other), then what is the maximum number of process that can run simultaneously, using atleast one of the resources, such that there will be no deadlock ?
A sample example is provided below, showing that two process using two resources can be in deadlock.
Two process in deadlock
(source: ucla.edu)

Someone please provide me the answer with details.

3

2 Answers 2

0

The question is incomplete. A deadlock can only occur if all of the following conditions hold simultaneously in a system (check https://en.wikipedia.org/wiki/Deadlock):

  1. Mutual exclusion: at least one resource must be held in a non-shareable mode. Only one process can use the resource at any given instant of time.
  2. Hold and wait or resource holding: a process is currently holding at least one resource and requesting additional resources which are being held by other processes.
  3. No preemption: a resource can be released only voluntarily by the process holding it.
  4. Circular wait: a process must be waiting for a resource which is being held by another process, which in turn is waiting for the first process to release the resource. In general, there is a set of waiting processes, P = {P1, P2, …, PN}, such that P1 is waiting for a resource held by P2, P2 is waiting for a resource held by P3 and so on until PN is waiting for a resource held by P1.

So, here are different assumptions and the corresponding answers:

Case 1: Resources are non exclusive or can be preempted or have any other mechanism to avoid deadlocks (obvious answer)

Then, any number of processors can be running. As a side note: of course, depending on the case, you may want to limit the number of processes to avoid/reduce contention.

Case 2: None of the previous conditions

Then, without any logic to avoid deadlocks, only one process can be run at a time if you want to avoid deadlocks. As soon as you add another process to the system, you can have a situation as the one described in your example (P1 has Ra and wants Rb, P2 has Rb and wants Ra).


PS: I believe this is more the kind of question asked to see your understanding of the concepts more than to receive an exact answer.

0

The Question is incomplete. The question doesn't specify the maximum number resources a process needs to finish its execution.

Let me rephrase the question:

Suppose we have N number of resources (all are independent of each other), then what is the maximum number of process that can run simultaneously, using at least one of the resources and maximum M resources by each process, such that there will be no deadlock?


Answer:

Let, the number of available resources = N, the maximum number of resources a process can hold simultaneously = M and number of process = P.

The system is in safe mode if following condition is satisfied.

N >= P ( M - 1 ) + 1

Maximum number of processes,

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

For example, if the number of available resources is 6 (N=6) and each process uses at most 2 (M=2) resources to finish execution, the maximum number of processes is 5. And the system is still safe.


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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.