View on GitHub

Notes

reference notes

DEADLOCK BACKGROUND

Deadlock

Definition: The permanent blocking of a set of processes that either compete for system resources or communicate with each other

A set of processes is deadlocked when each process in the set is blocked: awaiting an event that can only be triggered by another blocked process in the set

Since none of the events ever triggered, deadlock is:-

2 Resource Categories

Reusable Consumable
Can be safely used by only one process at a time Can be created(produced) and destroyed(consumed)
like: processors, I/O channels, main and secondary memory, devices, and data structures such as files, databases, and semaphores like: interrupts, signals, messages, and information. information inside I/O buffers

Reusable Resources Deadlock

Reusable Resources Deadlock

consider two processes that compete for exclusive access to a disk file D and a drive T. The programs engage in the operations depicted above

Deadlock occurs if each process holds one resource and requests the other. For example:

Deadlock occurs if both processes progress to their second request

Consumable Resources Deadlock

Consider a pair of processes, in which each process attempts to receive a message from the other process and then send a message to the other process:

Resource Allocation Graphs

Resource Allocation Graphs

a directed graph that depicts a state of :

A graph edge directed from a process to a resource indicates a resource that has been requested by the process but not yet granted ( Figure a ).

A graph edge directed from a reusable resource node dot to a process indicates a request that has been granted ( Figure b ); that is, the process has been assigned one unit of that resource

Resource Allocation Graphs

Figure c shows an example deadlock. There is only one unit each of resources Ra and Rb. Process P1 holds Rb and requests Ra, while P2 holds Ra but requests Rb

Figure d has the same topology as Figure c , but there is no deadlock because multiple units of each resource are available.

DEADLOCK CONDITION

4 Conditions for Deadlock

Mutual Exclusion Hold and Wait No Preemption Circular Wait
only one process may use a resource at a time a process may hold allocated resources while awaiting assignment of others no resource can be forcibly removed from a process holding it a closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain

Three conditions of policy must be present for a deadlock to be possible: Mutual Exclusion, Hold and Wait, and No Preemption.

The first three conditions are necessary, but not sufficient, for a deadlock to exist. For deadlock to actually take place, a fourth condition is required: Circular wait.The fourth condition is, actually, a potential consequence of the first three. That is, given that the first three conditions exist, a sequence of events may occur that lead to an unresolvable circular wait. The unresolvable circular wait is in fact the definition of deadlock. The circular wait listed as condition 4 is unresolvable because the first three conditions hold. Thus, the four conditions, taken together, constitute necessary and sufficient conditions for deadlock.

DEALING WITH DEADLOCKS

Three general approaches exist for dealing with deadlock:

Deadlock Prevention

Design a system in such a way that the possibility of deadlock is excluded

Two main methods:

  1. Indirect: prevent the occurrence of one of the three necessary conditions
  2. Direct: prevent the occurrence of a circular wait (the fourth condition)

Indirect Prevention

Direct Prevention

Havender’s Linear Ordering

Havender's Linear Ordering

Deadlock Avoidance

Sometimes it is not feasible to prevent deadlock. - This can occur when we need the most effective use of all our resources.

Allows the three necessary conditions but makes judicious choices to assure that the deadlock point is never reached.

Avoidance allows more concurrency than prevention

A decision is made dynamically whether the current resource allocation request will, if granted, potentially lead to a deadlock

Requires knowledge of future process requests

Two approaches to deadlock avoidance:

  1. Resource Allocation Denial
  2. Process initiation Denial

Resource Allocation Denial

Referred to as the banker’s algorithm

Bankers algorithm:

Determination of a Safe State

Determining a Safe State

Can any of the four processes be run to completion with the resources available?

Steps needed to check either it is a safe state or not:

  1. Construct the Need Matrix
    • Need = Claim Matrix – Allocation Matrix Need Matrix
    • it shows how many resources needed by each program in order for the process to complete execution.
  2. To check either the current state is safe or not safe,
    • Compare the content of available vector with the need matrix.
      • Is there any process which can be allocated with the available resources and the process can run to completion? Safe State
    • For this example the system has 1 unit of R2 and 1 unit of R3. Based from the claim matrix P2 can runs to completion.
  3. After a process runs to completion, it will release all the resources to system. Construct new available vector.
    • New AV = Current Av + Allocation Matrix of the chosen process

    Safe State

Repeat step 2 and 3 until all processes complete execution.

When all processes run to completion, the value for available vector is equal to resource vector Determining a Safe State

Thus, the state defined originally is a safe state

Determination of a Unsafe State

Determining a Unsafe State

P1 request for additional of 1 unit of R1 and 1 unit of R3. Is it safe to grant what P1 want?

Is this a safe state?

Deadlock Avoidance Advantages

It is not necessary to pre-empt and rollback processes, as in deadlock detection

4 Deadlock Avoidance Restrictions

  1. Maximum resource requirement for each process must be stated in advance
  2. Processes under consideration must be independent and with no synchronization requirements
  3. There must be a fixed number of resources to allocate
  4. No process may exit while holding resources

Deadlock Detection

Deadlock prevention strategies are very conservative

Deadlock Detection Algorithms

Can be made as frequently as each resource request or, less frequently, depending on how likely it is for a deadlock to occur

Steps:

  1. Mark each process that has a row in the Allocation Matrix of all zeros.
    • This process does not hold any resources
  2. Initialize a temporary vector W to equal to Available Vector.
  3. Find an index i such that process i is currently unmarked and ith row of Q is less then or equal to W (Q <= W). If no such row found terminate the algorithm.
  4. If such a row is found ( ith row exist), mark process i and add the corresponding row of the Allocation Matrix to W. Then return to step 3.

Example:

Deadlock Detection

Deadlock exists if and only if there are unmarked processes at the end of the algorithm (exist a process request that can’t be fulfilled)

Strategy in this algorithm is to find:

Then the algorithm will look for another process.

This algorithm will not guarantee to prevent deadlock, it will depend on the order in which requested are granted.

It is only to determine either deadlock currently exist or not

RECOVERY

Recovery Strategies

  1. Abort all deadlocked processes (most common strategy)
  2. Back up each deadlocked process to some previously defined checkpoint and restart all processes (rollback/restart)
  3. Successively abort deadlocked processes until deadlock no longer exists
  4. Successively preempt resources/processes until deadlock no longer exists.

The process in 3 and 4 will be selected according to certain criteria, e.g.

Integrated Deadlock Strategy

Combine the previous approaches into the following way:

Example of resource classes:

Summary

Deadlock:

Dealing with deadlock: