0% found this document useful (0 votes)
35 views45 pages

7-Deadlocks, Resource-Allocation Graph, Methods For Handling Deadlocks-13!08!2024

bjkm nj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views45 pages

7-Deadlocks, Resource-Allocation Graph, Methods For Handling Deadlocks-13!08!2024

bjkm nj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Scheduling

Module 3
Module 3

Deadlock

§ It refers to a situation where two or more processes are


unable to proceed because each is waiting for a resource that
is held by another process in the same set of processes.
Module 3

Deadlock – System Model

§ It is a formal representation of the resources and processes


involved in a deadlock situation.

§ It helps in understanding the conditions and interactions


that can lead to a deadlock.
Module 3

Deadlock – System Model - Elements


Resources
§ Entities that processes require to perform their tasks.
Resources can be classified into two types:
§ Preemptable Resources: These resources can be taken away from a
process without causing any adverse effects. Examples include memory,
CPU cycles, and I/O devices.
§ Non-preemptable Resources: These resources cannot be taken away
from a process once they have been allocated. Examples include
printers, CD drives, and specialized hardware.
Module 3

Deadlock – System Model - Elements


Processes

§ These are the executing units in an operating system.

§ Processes can request and release resources during their execution.

§ Each process has a set of activities or tasks that need to be completed.

§ Process or Thread may utilize a resource in only the following sequence:


§ Request. The thread requests the resource. If the request cannot be granted immediately (for
example, if a mutex lock is currently held by another thread), then the requesting thread must
wait until it can acquire the resource.
§ Use. The thread can operate on the resource (for example, if the resource is a mutex lock, the
thread can access its critical section).
§ Release. The thread releases the resource.
Module 3

Deadlock – System Model - Elements


Resource Allocation Graph:

§ It is a graphical representation of the resource allocation and request


relationships among processes.

§ The graph consists of nodes representing processes and resources and


directed edges representing the allocation and request relationships.
§ Process Nodes: Each process is represented by a circle or node in the graph.
§ Resource Nodes: Each resource is represented by a rectangle or node in the graph.
§ Directed Edges: An edge from a resource node to a process node represents the
allocation relationship, indicating that the process currently holds the resource. An
edge from a process node to a resource node represents the request relationship,
indicating that the process is waiting to acquire the resource.
Module 3

Deadlock – Conditions
The deadlock system model includes the four necessary conditions for deadlock
to occur:
§ Mutual Exclusion: Resources can only be used by one process at a time.

§ Hold and Wait: Processes hold allocated resources while waiting for additional
resources.
§ No Preemption: Resources cannot be forcibly taken away from a process.

§ Circular Wait: There exists a circular chain of processes, with each process
holding a resource that is requested by the next process in the chain.
Module 3

Deadlock – Conditions
§ By analyzing the resource allocation graph and checking for
the presence of a circular chain of processes, it is possible to
identify the occurrence of a deadlock in the system.

§ The deadlock system model provides a basis for designing


deadlock prevention, avoidance, and detection algorithms to
manage and resolve deadlock situations effectively.
Module 3

Deadlock – Resource Allocation Graph


Module 3

Deadlock – Resource Allocation Graph


Module 3

Deadlock – Resource Allocation Graph

§ Mutual Exclusion

At least one resource can only be used by a single process at


any given time. This means that once a process acquires a
resource, other processes are denied access to it until the
resource is released.
Module 3

Deadlock – Resource Allocation Graph

Hold and Wait:

Processes hold resources while waiting for additional


resources. This means that a process may be holding one or
more resources and is also waiting to acquire additional
resources that are currently being held by other processes.
Module 3

Deadlock – Resource Allocation Graph

No Preemption:

Resources cannot be forcibly taken away from a process. They


can only be released voluntarily by the process holding them.
Module 3

Deadlock – Resource Allocation Graph


Circular Wait:

There exists a circular chain of two or more processes, where


each process is waiting for a resource that is held by another
process in the chain. In other words, the last process in the
chain is waiting for a resource held by the first process,
creating a circular dependency.
Module 3

Deadlock – Management
§ When the conditions (discussed in last 4 slides) are satisfied, a deadlock can occur.
As a result, none of the processes involved can make progress, leading to a system
deadlock.

§ Deadlocks can be prevented, avoided, or resolved using various techniques, such as


§ Resource allocation strategies,
§ Deadlock detection algorithms, and
§ Deadlock recovery mechanisms.

§ The goal is to ensure that the system remains in a safe state where deadlocks cannot
occur, or if they do occur, they can be resolved efficiently to restore system
functionality.
Module 3

Deadlock – Management - Prevention


§ Ensure that any one condition - Mutual Exclusion, No
preemption, Hold and Wait, circular wait does not hold. Then
occurrences of the Deadlock can be prevented.

§ It is a proactive approach.

§ It aims to identify and eliminate the conditions that lead to


deadlocks by enforcing restrictions on resource allocation
and process execution.
Module 3

Deadlock – Management - Prevention


§ Mutual Exclusion Avoidance:

§ Ensure that resources are shareable whenever possible.

§ If multiple processes can access a resource simultaneously


without interfering with each other's operations, the issue of
mutual exclusion is eliminated.

§ For resources that cannot be shared, consider allowing multiple


processes to have read-only access while enforcing exclusive
access for write operations.
Module 3

Deadlock – Management - Prevention


§ Hold and Wait Avoidance:

§ Implement a strategy where:


§ Processes must request and acquire all the necessary resources they need
before starting execution. This can be achieved by using a resource
allocation protocol known as "all or nothing" or "claim all" strategy.
§ Alternatively, require processes to release all currently held resources
before requesting new ones. This is known as the "no preemption"
approach.
Module 3

Deadlock – Management - Prevention


§ No Preemption Avoidance:

§ In some cases, it may be possible to preempt resources from


processes to break potential deadlocks. it is often preferable
to design systems where preemption is not required.

§ Avoid allocating non-preemptable resources if there is a


possibility of deadlock. Allocate such resources only when
they are not requested by other processes.
Module 3

Deadlock – Management - Prevention


§ Circular Wait Avoidance:
§ Introduce a total ordering of resources and impose a rule that
processes can only request resources in an increasing order. This
eliminates the possibility of circular wait by ensuring that a process
always requests resources in a consistent order.
§ Implement a resource hierarchy, where each process is assigned a
unique identifier, and processes can only request resources with a
lower identifier. This ensures a partial ordering of resources and
prevents circular wait.
Module 3

Deadlock – Management - Detection


§ Maintain wait-for graph
§ Nodes are threads
§ Ti -> Tj if Ti is waiting for Tj
§ Periodically invoke an algorithm that searches for a cycle in the
graph. If there is a cycle, there exists a deadlock

§ An algorithm to detect a cycle in a graph requires an order of n2


operations, where n is the number of vertices in the graph
Module 3

Deadlock – Management - Detection


§ Maintain wait-for graph

§ For the given resource allocation

Graph create wait for graph


Module 3

Deadlock – Management - Detection


§ Maintain wait-for graph
Module 3

Deadlock – Management – Avoidance


§ Only Single Instance of Resources exist in system
§ Method Applied is Resource Allocation Graph.
§ Example:

Claim Edge
Allocation R1
Edge

P1 P2

Reqquest
Edge R2
Module 3

Deadlock – Management – Avoidance


§ Converting claim edge to Request Edge and then to Allocation Edge.

§ Example: Cycle forms and then Deadlock is identified.

R1 R1

P1 P2 P1 P2

R2 R2
Module 3

Deadlock – RAG – Eg. 1


§ Consider the given scenario where,
§ 1 instance of R1 is held by P1
§ P1 is requesting for R2
§ 1 instance of R2 is held by P2
§ P2 is requesting for 1 Instance R1
§ 1 Instance of R2 is held by P3.

§ Draw the resource allocation graph for the scenario. Identify


whether the scenario leads to deadlock otherwise find a safe
sequence.
Module 3

Deadlock – RAG – Eg. 1


The given resource allocation graph is
multi-instance with a cycle contained in
it.

The system may or may not be in a


deadlock state.
Module 3

Deadlock – RAG – Eg. 1


§ Construct a table
Process Resource Allocation Resource Need
R1 R1 R1 R2
P1 1 0 0 1
P2 0 1 1 0
P3 0 1 0 0

§ Available Resource – R1=0, R2=0


Module 3

Deadlock – RAG – Eg. 1


§ P3 executes and releases it resource then the table changes
Process Resource Allocation Resource Need
R1 R1 R1 R2
P1 1 0 0 1
P2 0 1 1 0
P3 0 0 0 0

§ Available Resource – R1=1, R2=0


Module 3

Deadlock – RAG – Eg. 1


§ With available resources only P1 can be satisfied.

§ P1 is allocated with R1 resource, it completes execution and releases its resources.

Process Resource Allocation Resource Need


R1 R2 R1 R2
P1 0 0 0 0
P2 0 1 1 0
P3 0 0 0 0

§ Available Resource – R1=1, R2=1


Module 3

Deadlock – RAG – Eg. 1


§ Now with available P2 can be satisfied.

§ P2 is allocated with R1 resource, it completes execution and releases its resources.

Process Resource Allocation Resource Need


R1 R2 R1 R2
P1 0 0 0 0
P2 0 0 0 0
P3 0 0 0 0
§ Available Resource – R1=1, R2=2, There exists a safe sequence P3, P1, P2 in which all the
processes can be executed.
Module 3

Deadlock – RAG – Eg. 2


Consider the following scenario and identify whether the system in deadlock, otherwise find a safe sequence.
§ There are R1- 2 instances, R2 – 3 instances, R3 – 2 Instances
§ P1 requests R1
§ 1 instance of R1 is allocated to P1
§ 1 instance of R1 is allocated to P0
§ P0 requests R2
§ 1 instance of R2 is allocated to P1
§ 1 instance of R2 is allocated to P2
§ 1 instance of R2 is allocated to P3
§ P3 requests 2 instances of R2
§ 1 instance of R3 is allocated to P0
§ P0 requests 1 instance of R3
§ P2 Requests 1 instance of R3
Module 3

Deadlock – RAG – Eg. 1


Module 3

Deadlock – RAG – Eg. 1


§ The given resource allocation graph is multi-instance with a
cycle contained in it. So, the system may or may not be in a
deadlock state
Module 3

Deadlock – RAG – Eg. 1


Constructing table - Available = 0 0 1

Process Resource Allocation Resource Need


R1 R2 R3 R1 R2 R3
P0 1 0 1 0 1 1
P1 1 1 0 1 0 0
P2 0 1 0 0 0 1
P3 0 1 0 0 2 0
Module 3

Deadlock – RAG – Eg. 1


Available = 0 0 1 => 0 1 1

only the requirement of the process P2 can be satisfied. So, process P2


is allocated the requested resources. It completes its execution and then
free up the instances of resources held by it.
Process Resource Allocation Resource Need
R1 R2 R3 R1 R2 R3
P0 1 0 1 0 1 1
P1 1 1 0 1 0 0
P2 0 1 0 0 0 1
P3 0 1 0 0 2 0
Module 3

Deadlock – RAG – Eg. 1


Available = 0 1 1 => 1 1 2

only the requirement of the process P0 can be satisfied. So, process P0


is allocated the requested resources. It completes its execution and then
free up the instances of resources held by it.
Process Resource Allocation Resource Need
R1 R2 R3 R1 R2 R3
P0 0 0 0 0 0 0
P1 1 1 0 1 0 0
P2 0 0 0 0 0 0
P3 0 1 0 0 2 0
Module 3

Deadlock – RAG – Eg. 1


Available = 1 1 2 => 2 2 2

only the requirement of the process P1 can be satisfied. So, process P1


is allocated the requested resources. It completes its execution and then
free up the instances of resources held by it.
Process Resource Allocation Resource Need
R1 R2 R3 R1 R2 R3
P0 0 0 0 0 0 0
P1 0 0 0 0 0 0
P2 0 0 0 0 0 0
P3 0 1 0 0 2 0
Module 3

Deadlock – RAG – Eg. 1


Available = 2 2 2 => 2 3 2

only the requirement of the process P3 can be satisfied. So, process P3


is allocated the requested resources. It completes its execution and then
free up the instances of resources held by it.
Process Resource Allocation Resource Need
R1 R2 R3 R1 R2 R3
P0 0 0 0 0 0 0
P1 0 0 0 0 0 0
P2 0 0 0 0 0 0
P3 0 0 0 0 0 0
Module 3

Deadlock – RAG – Eg. 1


Available = 2 2 2 => 2 3 2

There exists a safe sequence P2, P0, P1, P3 in which all the processes
can be executed. So, the system is in a safe state.

Process Resource Allocation Resource Need


R1 R2 R3 R1 R2 R3
P0 0 0 0 0 0 0
P1 0 0 0 0 0 0
P2 0 0 0 0 0 0
P3 0 0 0 0 0 0
Module 3

Deadlock – Management - Avoidance


§ A strategy employed in operating systems to prevent the
occurrence of deadlocks.

§ It involves dynamically allocating resources to processes in a


way that guarantees the system will always be in a safe state,
where deadlock cannot occur.

§ The primary objective of deadlock avoidance is to ensure that all


processes can progress towards completion without getting stuck
in a deadlock situation.
Module 3

Deadlock – Management - Avoidance


§ Safe State:
§ a "safe state" refers to a system state in which all processes can complete
their execution successfully, without entering a deadlock.

§ In a safe state, it is guaranteed that there exists at least one execution


sequence or order in which all processes can acquire the resources they need
and release them in a way that avoids deadlock.

§ A safe state is characterized by the absence of a circular wait condition.


§ This means that there is no circular chain of processes where each process
is waiting for a resource held by another process in the chain.
Module 3

Deadlock – Management - Avoidance


§ Banker’s Algorithm/Avoidance Algorithm:
§ It is a resource allocation and deadlock avoidance algorithm.
§ First simulating the allocation of the maximum possible
amounts of all resources.
§ It performs an “safe-state” check to test for potential deadlock
conditions among all other pending activities.
§ Only after ensuring that there are no potential deadlocks does it
allow the allocation to proceed.”
Module 3

Deadlock – Management - Avoidance


§ Three things are very important in Banker algorithm

§ How much of each resource each process could possibly


request (“MAX”).

§ How much of each resource each process is currently


holding (ALLOCATED).

§ How much of each resource the system currently has


available (“AVAILABLE”).
Module 3

Deadlock – Management - Summary


Deadlock Prevention
§ Invalidate any one condition.

Deadlock Avoidance
§ RAG Scheme – when single Instance of Resource exists.
§ Bankers Algorithm - when multiple Instance of Resource exists.

Deadlock Detection
§ Wait for graph. - single Instance of Resource exists
§ Detection algorithm using Available, Allocated, Requested, Need - multiple Instance of Resource exists

Deadlock Recovery
§ Process Termination.
§ Resource Preemption.

You might also like