0% found this document useful (0 votes)
3 views

CH_6_OS

Chapter 6 discusses deadlock handling techniques, including prevention, avoidance, detection, and recovery. It outlines necessary conditions for deadlock and strategies to prevent it, such as mutual exclusion, hold and wait, no preemption, and circular wait. The chapter also introduces the Banker's Algorithm for resource allocation and deadlock avoidance, ensuring systems remain in a safe state by managing resource requests effectively.

Uploaded by

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

CH_6_OS

Chapter 6 discusses deadlock handling techniques, including prevention, avoidance, detection, and recovery. It outlines necessary conditions for deadlock and strategies to prevent it, such as mutual exclusion, hold and wait, no preemption, and circular wait. The chapter also introduces the Banker's Algorithm for resource allocation and deadlock avoidance, ensuring systems remain in a safe state by managing resource requests effectively.

Uploaded by

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

Chapter 6

Deadlock
Deadlock Handling Techniques
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• Recovery from Deadlock
Deadlock Prevention
• Deadlock prevention and avoidance are strategies used in
computer systems to ensure that different processes can run
smoothly without getting stuck waiting for each other forever.
Think of it like a traffic system where cars (processes) must move
through intersections (resources) without getting into a gridlock.
• Necessary Conditions for Deadlock
• Mutual Exclusion

• Hold and Wait

• No Preemption

• Circular Wait
1. Mutual Exclusion
• Mutual section from the resource point of view is the
fact that a resource can never be used by more than
one process simultaneously which is fair enough but
that is the main reason behind the deadlock. If a
resource could have been used by more than one
process at the same time then the process would have
never been waiting for any resource.
• However, if we can be able to violate resources
behaving in the mutually exclusive manner then the
deadlock can be prevented.
2. Hold and Wait

• Hold and wait condition lies when a process holds a


resource and waiting for some other resource to
complete its task. Deadlock occurs because there can
be more than one process which are holding one
resource and waiting for other in the cyclic order.
• However, we have to find out some mechanism by
which a process either doesn't hold any resource or
doesn't wait. That means, a process must be assigned
all the necessary resources before the execution starts.
A process must not wait for any resource once the
execution has been started.
3. No Preemption

• Deadlock arises due to the fact that a process can't be


stopped once it starts. However, if we take the resource
away from the process which is causing deadlock then
we can prevent deadlock.
• This is not a good approach at all since if we take a
resource away which is being used by the process then
all the work which it has done till now can become
inconsistent.
4. Circular Wait

• To violate circular wait, we can assign a priority number


to each of the resource. A process can't request for a
lesser priority resource. This ensures that not a single
process can request a resource which is being utilized
by some other process and no cycle will be formed.
Deadlock Avoidance
• In deadlock avoidance, the request for any resource will be
granted if the resulting state of the system doesn't cause
deadlock in the system. The state of the system will
continuously be checked for safe and unsafe states.
• In order to avoid deadlocks, the process must tell OS, the
maximum number of resources a process can request to
complete its execution.
• The simplest and most useful approach states that the process
should declare the maximum number of resources of each
type it may ever need. The Deadlock avoidance algorithm
examines the resource allocations so that there can never be
a circular wait condition.
Resource Allocation Graph
• The resource allocation graph is the pictorial
representation of the state of a system. As its name
suggests, the resource allocation graph is the complete
information about all the processes which are holding
some resources or waiting for some resources.
• It also contains information about all the instances of all
the resources, whether they are available or being used by
the processes.
• In Resource allocation graph, the process is represented by
a Circle while the Resource is represented by a rectangle.
Let's see the types of vertices and edges in detail.
Example
• Let'sconsider 3 processes P1, P2 and P3, and two types
of resources R1 and R2. The resources are having 1
instance each.
• According to the graph, R1 is being used by P1, P2 is
holding R2 and waiting for R1, P3 is waiting for R1 as
well as R2.
• The graph is deadlock-free since no cycle is being
formed in the graph.
Construct RAG
• P = { P1,P2,P3}
• R= {R1,R2,R3,R4}
• E= {P1->R1,P2->R3,R1->P2,R2->P2,R2->P1,R3->P3}
Question to solve
Process Allocation Request
Resource Resource

R1 R2 R3 R1 R2 R3
P1 0 1 0 1 0 0
P2 1 0 0 0 0 1
P3 0 0 1 0 1 0
Question for RAG
Process Allocation Request
Resource Resource

R1 R2 R3 R1 R2 R3
P1 0 1 0 1 0 0
P2 1 0 0 0 0 1
P3 0 0 1 0 1 0
P4 0 0 1 0 0 0
Bankers Algorithm
• Banker’s Algorithm is a resource allocation and deadlock avoidance
algorithm used in operating systems. It ensures that a system
remains in a safe state by carefully allocating resources to
processes while avoiding unsafe states that could lead to
deadlocks.
• The Banker’s Algorithm is a smart way for computer systems to
manage how programs use resources, like memory or CPU time.

• It helps prevent situations where programs get stuck and can not
finish their tasks. This condition is known as deadlock.

• By keeping track of what resources each program needs and


what’s available, the banker algorithm makes sure that programs
only get what they need in a safe order.
Data Structures in the Banker's Algorithm
1.Available: It is an array of length 'm' that defines each type of
resource available in the system.
2.Max: It is a 2D matrix that indicates each process P[i] can store
the maximum number of resources R[j] (each type) in a system.
3.Allocation: It is a matrix of m x n orders that indicates the type
of resources currently allocated to each process in the system.
4.Need: It is an M x N matrix sequence representing the number
of remaining resources for each process.
5.Finish: It is the vector of the order m. It includes a Boolean
value (true/false) indicating whether the process has been
allocated to the requested resources, and all resources have
been released after finishing its task.
Example to understand Bankers
algo
• https://www.youtube.com/watch?v=7gMLNiEz3nw
Find its safe or unsafe if safe give the safe
sequence.
Total Resource A =10 , B =5 and C = 7
Step 1- Find Remaining Need
Need = Max – Allocation
Check need and Available
Need < Available

You might also like