Skip to content
Official PYQ · 2021

GATE CS 2021 Set 2 Q43 — Deadlock

Explain with AI

Consider a computer system with multiple shared resource types, with one instance per resource type. Each instance can be owned by only one process at a time. Owning and freeing of resources are done by holding a global lock (L)(L). The following scheme is used to own a resource instance:

function OWNRESOURCE(Resource R)
    Acquire lock L // a global lock
    if R is available then
        Acquire R    
        Release lock L
    else
        if R is owned by another process P then     
        Terminate P, after releasing all resources owned by P    
        Acquire R    
        Restart P   
        Release lock L   
        end if   
    end if    
end function

Which of the following choice(s) about the above scheme is/are correct?

Select all correct options
View worked solution

Answer: A;B;C

To determine which choices are correct, let us analyze the behavior of the OWNRESOURCE function and how it manages resources.

1. Deadlock Prevention (Option A)

A deadlock requires four necessary conditions: Mutual Exclusion, Hold and Wait, No Preemption, and Circular Wait.

  • In this scheme, when a process requests a resource RR that is currently held by another process PP, the requesting process does not wait.
  • Instead, it immediately terminates PP, forces the release of PP's resources, acquires RR, and restarts PP.
  • Since no process ever enters a waiting state for a resource, the Hold and Wait condition is eliminated, and a Circular Wait cannot form.

Therefore, deadlocks cannot occur. Option A is correct.

2. Livelock (Option B)

A livelock occurs when two or more processes continuously change their state in response to each other without making any forward progress.

  • Consider two processes, P1P_1 and P2P_2, and two resources, R1R_1 and R2R_2.
  • P1P_1 acquires R1R_1 and P2P_2 acquires R2R_2.
  • Now, P1P_1 requests R2R_2. Since R2R_2 is held by P2P_2, P1P_1 terminates P2P_2, releases R2R_2, acquires R2R_2, and restarts P2P_2.
  • Before P1P_1 can complete its execution, the restarted P2P_2 requests R1R_1. Since R1R_1 is held by P1P_1, P2P_2 terminates P1P_1, releases all of P1P_1's resources (including R2R_2), acquires R1R_1, and restarts P1P_1.
  • This cycle of mutual termination and restart can continue indefinitely, preventing both processes from ever completing.

Therefore, the scheme may lead to livelock. Option B is correct.

3. Starvation (Option C)

Starvation occurs when a process is perpetually denied the resources or CPU time it needs to complete its execution.

  • As shown in the livelock scenario, a process can be repeatedly terminated and restarted by other active processes before it can finish its task.
  • Consequently, such a process will suffer from starvation.

Therefore, the scheme may lead to starvation. Option C is correct.

4. Mutual Exclusion (Option D)

Mutual exclusion means that a resource instance can be held by at most one process at any given time.

  • When a process requests an already-owned resource RR, the owner PP is terminated, and all of PP's resources are released first before the requesting process acquires RR.
  • Thus, at any instant, no resource is owned by more than one process.

Therefore, mutual exclusion is not violated. Option D is incorrect.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Deadlock questions