All Exams Test series for 1 year @ ₹349 only
Question

Consider a system with five processes P 0, through P 4, and three resource types A, B and C. Resource type A has seven instances, resource type B has two instances and resource type C has six instances suppose at time T 0we Have the following allocation.

Process

Allocation

Request

Available

A

B

C

A

B

C

A

B

C

P 0

0

1

0

0

0

0

0

0

0

P 1

2

0

0

2

0

2

P 2

3

0

3

0

0

0

P 3

2

1

1

1

0

0

P 4

0

2

2

0

0

2

If we implement Deadlock detection algorithm we claim that system is ______.

The correct answer is

Not in deadlock state

Analyzing System State for Deadlock Detection

The question asks us to determine if a system with five processes (P0 through P4) and three resource types (A, B, C) is in a deadlock state at a given time T0. We are provided with the current allocation of resources to each process, the resources requested by each process, and the currently available resources. To answer this, we will apply a deadlock detection algorithm.

Initial System State

Let's first look at the given state of the system at time T0.

Total instances of resources:

  • Resource A: 7 instances
  • Resource B: 2 instances
  • Resource C: 6 instances

Initial available resources:

\(\text{Available} = \begin{bmatrix} 0 & 0 & 0 \end{bmatrix}\)

Allocation of resources to each process:

Process Allocation A Allocation B Allocation C
P0 0 1 0
P1 2 0 0
P2 3 0 3
P3 2 1 1
P4 0 2 2

Resources requested by each process:

Process Request A Request B Request C
P0 0 0 0
P1 2 0 2
P2 0 0 0
P3 1 0 0
P4 0 0 2

Applying the Deadlock Detection Algorithm

The deadlock detection algorithm works by checking if there is a sequence of processes that can complete execution given the current available resources. If such a sequence (often called a 'finish' sequence or 'safe' sequence in related algorithms) exists, the system is not in a deadlock state. Otherwise, if no such sequence can be found for all processes, the system is in a deadlock state, and the processes that cannot be finished are part of the deadlock.

The algorithm uses a `Work` vector, initially equal to `Available`, and a `Finish` vector, initially all false, indicating no process has finished.

Initial state:

  • \(\text{Work} = \text{Available} = \begin{bmatrix} 0 & 0 & 0 \end{bmatrix}\)
  • \(\text{Finish} = [\text{false}, \text{false}, \text{false}, \text{false}, \text{false}]\) for P0 to P4

We search for a process Pi such that `Finish[i]` is false and `Request`i \(\le\) `Work`. If we find such a process, we assume it finishes, update `Work` by adding its `Allocation` to it, and set `Finish[i]` to true. We repeat this until no such process can be found.

Let's trace the execution:

  1. Iteration 1: Current \(\text{Work} = \begin{bmatrix} 0 & 0 & 0 \end{bmatrix}\)

    • P0: Request \(\begin{bmatrix} 0 & 0 & 0 \end{bmatrix}\). Is \(\begin{bmatrix} 0 & 0 & 0 \end{bmatrix} \le \begin{bmatrix} 0 & 0 & 0 \end{bmatrix}\)? Yes. P0 can run.
    • P1: Request \(\begin{bmatrix} 2 & 0 & 2 \end{bmatrix}\). Is \(\begin{bmatrix} 2 & 0 & 2 \end{bmatrix} \le \begin{bmatrix} 0 & 0 & 0 \end{bmatrix}\)? No (2 > 0, 2 > 0).
    • P2: Request \(\begin{bmatrix} 0 & 0 & 0 \end{bmatrix}\). Is \(\begin{bmatrix} 0 & 0 & 0 \end{bmatrix} \le \begin{bmatrix} 0 & 0 & 0 \end{bmatrix}\)? Yes. P2 can run.
    • P3: Request \(\begin{bmatrix} 1 & 0 & 0 \end{bmatrix}\). Is \(\begin{bmatrix} 1 & 0 & 0 \end{bmatrix} \le \begin{bmatrix} 0 & 0 & 0 \end{bmatrix}\)? No (1 > 0).
    • P4: Request \(\begin{bmatrix} 0 & 0 & 2 \end{bmatrix}\). Is \(\begin{bmatrix} 0 & 0 & 2 \end{bmatrix} \le \begin{bmatrix} 0 & 0 & 0 \end{bmatrix}\)? No (2 > 0).

    Processes P0 and P2 can potentially run. Let's choose P0.

    • P0 finishes.
    • \(\text{Work} = \text{Work} + \text{Allocation}_{P_0} = \begin{bmatrix} 0 & 0 & 0 \end{bmatrix} + \begin{bmatrix} 0 & 1 & 0 \end{bmatrix} = \begin{bmatrix} 0 & 1 & 0 \end{bmatrix}\)
    • \(\text{Finish}[0] = \text{true}\). \(\text{Finish} = [\text{true}, \text{false}, \text{false}, \text{false}, \text{false}]\).
  2. Iteration 2: Current \(\text{Work} = \begin{bmatrix} 0 & 1 & 0 \end{bmatrix}\). Search among P1, P2, P3, P4.

    • P1: Request \(\begin{bmatrix} 2 & 0 & 2 \end{bmatrix}\). Is \(\begin{bmatrix} 2 & 0 & 2 \end{bmatrix} \le \begin{bmatrix} 0 & 1 & 0 \end{bmatrix}\)? No (2 > 0, 2 > 0).
    • P2: Request \(\begin{bmatrix} 0 & 0 & 0 \end{bmatrix}\). Is \(\begin{bmatrix} 0 & 0 & 0 \end{bmatrix} \le \begin{bmatrix} 0 & 1 & 0 \end{bmatrix}\)? Yes. P2 can run.
    • P3: Request \(\begin{bmatrix} 1 & 0 & 0 \end{bmatrix}\). Is \(\begin{bmatrix} 1 & 0 & 0 \end{bmatrix} \le \begin{bmatrix} 0 & 1 & 0 \end{bmatrix}\)? No (1 > 0).
    • P4: Request \(\begin{bmatrix} 0 & 0 & 2 \end{bmatrix}\). Is \(\begin{bmatrix} 0 & 0 & 2 \end{bmatrix} \le \begin{bmatrix} 0 & 1 & 0 \end{bmatrix}\)? No (2 > 0).

    Only P2 can run among the remaining processes. Let's choose P2.

    • P2 finishes.
    • \(\text{Work} = \text{Work} + \text{Allocation}_{P_2} = \begin{bmatrix} 0 & 1 & 0 \end{bmatrix} + \begin{bmatrix} 3 & 0 & 3 \end{bmatrix} = \begin{bmatrix} 3 & 1 & 3 \end{bmatrix}\)
    • \(\text{Finish}[2] = \text{true}\). \(\text{Finish} = [\text{true}, \text{false}, \text{true}, \text{false}, \text{false}]\).
  3. Iteration 3: Current \(\text{Work} = \begin{bmatrix} 3 & 1 & 3 \end{bmatrix}\). Search among P1, P3, P4.

    • P1: Request \(\begin{bmatrix} 2 & 0 & 2 \end{bmatrix}\). Is \(\begin{bmatrix} 2 & 0 & 2 \end{bmatrix} \le \begin{bmatrix} 3 & 1 & 3 \end{bmatrix}\)? Yes. P1 can run.
    • P3: Request \(\begin{bmatrix} 1 & 0 & 0 \end{bmatrix}\). Is \(\begin{bmatrix} 1 & 0 & 0 \end{bmatrix} \le \begin{bmatrix} 3 & 1 & 3 \end{bmatrix}\)? Yes. P3 can run.
    • P4: Request \(\begin{bmatrix} 0 & 0 & 2 \end{bmatrix}\). Is \(\begin{bmatrix} 0 & 0 & 2 \end{bmatrix} \le \begin{bmatrix} 3 & 1 & 3 \end{bmatrix}\)? Yes. P4 can run.

    P1, P3, and P4 can potentially run. Let's choose P1.

    • P1 finishes.
    • \(\text{Work} = \text{Work} + \text{Allocation}_{P_1} = \begin{bmatrix} 3 & 1 & 3 \end{bmatrix} + \begin{bmatrix} 2 & 0 & 0 \end{bmatrix} = \begin{bmatrix} 5 & 1 & 3 \end{bmatrix}\)
    • \(\text{Finish}[1] = \text{true}\). \(\text{Finish} = [\text{true}, \text{true}, \text{true}, \text{false}, \text{false}]\).
  4. Iteration 4: Current \(\text{Work} = \begin{bmatrix} 5 & 1 & 3 \end{bmatrix}\). Search among P3, P4.

    • P3: Request \(\begin{bmatrix} 1 & 0 & 0 \end{bmatrix}\). Is \(\begin{bmatrix} 1 & 0 & 0 \end{bmatrix} \le \begin{bmatrix} 5 & 1 & 3 \end{bmatrix}\)? Yes. P3 can run.
    • P4: Request \(\begin{bmatrix} 0 & 0 & 2 \end{bmatrix}\). Is \(\begin{bmatrix} 0 & 0 & 2 \end{bmatrix} \le \begin{bmatrix} 5 & 1 & 3 \end{bmatrix}\)? Yes. P4 can run.

    P3 and P4 can potentially run. Let's choose P3.

    • P3 finishes.
    • \(\text{Work} = \text{Work} + \text{Allocation}_{P_3} = \begin{bmatrix} 5 & 1 & 3 \end{bmatrix} + \begin{bmatrix} 2 & 1 & 1 \end{bmatrix} = \begin{bmatrix} 7 & 2 & 4 \end{bmatrix}\)
    • \(\text{Finish}[3] = \text{true}\). \(\text{Finish} = [\text{true}, \text{true}, \text{true}, \text{true}, \text{false}]\).
  5. Iteration 5: Current \(\text{Work} = \begin{bmatrix} 7 & 2 & 4 \end{bmatrix}\). Search among P4.

    • P4: Request \(\begin{bmatrix} 0 & 0 & 2 \end{bmatrix}\). Is \(\begin{bmatrix} 0 & 0 & 2 \end{bmatrix} \le \begin{bmatrix} 7 & 2 & 4 \end{bmatrix}\)? Yes. P4 can run.

    P4 can run. Let's choose P4.

    • P4 finishes.
    • \(\text{Work} = \text{Work} + \text{Allocation}_{P_4} = \begin{bmatrix} 7 & 2 & 4 \end{bmatrix} + \begin{bmatrix} 0 & 2 & 2 \end{bmatrix} = \begin{bmatrix} 7 & 4 & 6 \end{bmatrix}\)
    • \(\text{Finish}[4] = \text{true}\). \(\text{Finish} = [\text{true}, \text{true}, \text{true}, \text{true}, \text{true}]\).

Conclusion on System State

After applying the deadlock detection algorithm, we found a sequence of processes (e.g., P0, P2, P1, P3, P4) in which all processes can complete their execution. This is because, at each step, we were able to find at least one process whose resource request could be satisfied by the currently available resources (represented by the `Work` vector). As processes finished, they released their allocated resources, increasing the `Work` vector, which in turn allowed subsequent processes to run.

Since all processes were able to finish, the system is in a state where deadlock is not currently occurring. This state is considered safe, although the detection algorithm itself only determines if the current state is deadlocked, not necessarily if it's 'safe' in the sense of the Banker's algorithm for *avoidance*. However, the key finding here is the absence of deadlock.

Therefore, the system is not in a deadlock state.

Revision Table: Deadlock Detection Analysis

Step Current Work Runnable Processes (Request <= Work) Process Chosen to Finish New Work (Work + Allocation) Finish Array
Initial \(\begin{bmatrix} 0 & 0 & 0 \end{bmatrix}\) P0, P2 P0 \(\begin{bmatrix} 0 & 1 & 0 \end{bmatrix}\) [T, F, F, F, F]
After P0 \(\begin{bmatrix} 0 & 1 & 0 \end{bmatrix}\) P2 P2 \(\begin{bmatrix} 3 & 1 & 3 \end{bmatrix}\) [T, F, T, F, F]
After P2 \(\begin{bmatrix} 3 & 1 & 3 \end{bmatrix}\) P1, P3, P4 P1 \(\begin{bmatrix} 5 & 1 & 3 \end{bmatrix}\) [T, T, T, F, F]
After P1 \(\begin{bmatrix} 5 & 1 & 3 \end{bmatrix}\) P3, P4 P3 \(\begin{bmatrix} 7 & 2 & 4 \end{bmatrix}\) [T, T, T, T, F]
After P3 \(\begin{bmatrix} 7 & 2 & 4 \end{bmatrix}\) P4 P4 \(\begin{bmatrix} 7 & 4 & 6 \end{bmatrix}\) [T, T, T, T, T]

Since all entries in the Finish array are true, all processes can complete.

Additional Information: Deadlock Concepts

Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource held by some other process in the same set. The four necessary conditions for a deadlock to occur are:

  • Mutual Exclusion: At least one resource must be held in a non-sharable mode.
  • Hold and Wait: A process must be holding at least one resource and waiting to acquire additional resources held by other processes.
  • No Preemption: Resources cannot be preempted; they can only be released voluntarily by the process holding them.
  • Circular Wait: A set of processes {P0, P1, ..., Pn} must exist such that P0 is waiting for a resource held by P1, P1 is waiting for a resource held by P2, ..., Pn-1 is waiting for a resource held by Pn, and Pn is waiting for a resource held by P0.

Deadlock detection algorithms are used to determine if a system is currently in a deadlocked state. If detected, a recovery mechanism (like process termination or resource preemption) might be employed. The algorithm used here is similar to the Banker's algorithm's safety check but is applied to detect an existing deadlock rather than avoid one.

In contrast to detection, deadlock prevention ensures that at least one of the four necessary conditions cannot hold. Deadlock avoidance dynamically examines the resource-allocation state to ensure that a circular wait condition can never exist. The Banker's algorithm is a well-known deadlock avoidance algorithm.

Was this answer helpful?

Important Questions from Deadlock Handling - Teaching

  1. Suppose a system has 12 magnetic tape drives and at time t 0, three processes are allotted tape drives out of their as given below:

    Maximum Needs

    Current Needs

    p 0

    10

    5

    p 1

    4

    2

    p 2

    9

    2

    At time t 0, the system is in safe state. Which of the following is safe sequence so that deadlock is avoided?

    NOTE:

    Current Need is currently allocated resource to a process

  2. A computer has six tapes drives with n processes competing for them. Each process may need two drives. What is the maximum value of n for the system to be deadlock free?

  3. Suppose a system has 12 instances of some resources with n processes competing for that resource. Each process may require 4 instances of the resource. The maximum value of n for which the system never enters into deadlock is

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App