Deadlock

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 44

UNIT 4

DEADLOCKS

1
Deadlock
• Deadlocks are a state in a computer system where two or more processes are
unable to proceed because each is waiting for the other to release a resource,
resulting in a standstill.
• Deadlocks typically occur in systems with shared resources where processes
compete for access to these resources.
• There are several conditions necessary for a deadlock to occur, including mutual
exclusion (resources cannot be shared), hold and wait (processes hold resources
while waiting for others), no preemption (resources cannot be forcibly taken from
processes), and circular wait (a cycle of processes waiting for resources).
• Deadlocks can significantly impact system performance and must be managed
effectively to ensure system stability and reliability.
• Various techniques such as deadlock detection, prevention, avoidance, and
recovery are used to handle deadlocks in operating systems and other computer
systems. 2
• Deadlock is a situation where a set of processes are blocked because each process is
holding a resource and waiting for another resource acquired by some other process.

• Deadlock is a common problem in multi-processing where several processes share a


specific type of mutually exclusive resource known as software or hardware

• Consider an Example, when two trains are coming toward each other on the same track
and there is only one track, none of the trains can move once they are in front of each
other.

• Single Road
3
• A similar situation occurs in operating systems when there are two or more processes that hold
some resources and wait for resources held by other(s).

• For example, in the below diagram, Process 1 is holding Resource 1 and waiting for resource 2

which is acquired by process 2, and process 2 is waiting for resource 1.

4
5
SIMPLE DEMONSTRATION
(DEADLOCKS)

6
DEADLOCK IN P-R

7
Deadlock Characterization:
• Deadlock characterization refers to the identification and understanding of the
conditions necessary for a deadlock to occur in a computer system. These
conditions, also known as the deadlock conditions, typically include:
• Mutual Exclusion: Resources cannot be shared; only one process can use a
resource at a time.
• Hold and Wait: Processes can hold resources while waiting for additional
resources to be allocated.
• No Preemption: Resources cannot be forcibly taken from a process; a process
holding a resource must release it voluntarily.
• Circular Wait: There exists a circular chain of processes, each holding at least one
resource and waiting for another resource held by another process in the chain.

8
• These conditions create a situation where processes become
deadlocked, meaning they are unable to proceed because each is
waiting for a resource held by another process.
• Deadlock characterization helps system designers and developers
understand the circumstances under which deadlocks can arise,
enabling them to implement appropriate deadlock prevention,
detection, avoidance, and recovery mechanisms in operating systems
and other computer systems to mitigate the risk of deadlock
occurrence and ensure system reliability and stability.

9
Circular wait?
• One process is waiting for the resource, which is held by the second
process, which is also waiting for the resource held by the third
process etc. This will continue until the last process is waiting for a
resource held by the first process. This creates a circular chain.

• For example, Process A is allocated Resource B as it is requesting


Resource A. In the same way, Process B is allocated Resource A, and
it is requesting Resource B. This creates a circular wait loop.

10
CIRCULAR WAIT REPRESENTATION

11
Example of Circular wait
• For example, a computer has three USB drives and three
processes. Each of the three processes able to holds one of
the USB drives.

• So, when each process requests another drive, the three


processes will have the deadlock situation as each process
will be waiting for the USB drive to release, which is
currently in use. This will result in a circular chain

12
Methods for Handling Deadlocks:

• Handling deadlocks involves implementing strategies to detect,


prevent, avoid, or recover from deadlock situations in a computer
system. Here are some common methods for handling deadlocks:

13
1) DEADLOCK IGNORACNCE

2) DEADLOCK PREVENTION

A) MUTUAL EXC

B) NO PREEMPTION- PREEMPTION (TIME QUANTUM)

C) HOLD AND WAIT (GIVE ALL RESOURCES TO ONE PROCESS)

D) CIRCULAR WAIT- NO CIRCULAR WAIT (ORDERING OF RESOURCES)

14
• 3) DEADLOCK AVOIDANCE (SAFE SITUATION OR NOT)
USING BANKERS ALGORITHM- SUFFICIENT
RESOURCES/FUNDS TO DISTRIBUTE

• 4) DEADLOCK RECOVERY (ADDITIONAL MECHANISM TO


COPEUP WITH RECOVERY)

15
Deadlock Detection and Recovery:
• Deadlock Detection: Periodically check the system's resource
allocation state to identify whether a deadlock has occurred. This can
be done using algorithms such as the Resource Allocation Graph
(RAG) algorithm, which examines resource allocation and request
patterns to detect deadlocks.
• Recovery Methods: Once a deadlock is detected, various recovery
methods can be employed to resolve it. These may include process
termination, where one or more processes involved in the deadlock
are terminated to free up resources, or resource preemption, where
resources are forcibly taken from processes to break the deadlock.

16
Deadlock Prevention:
• Resource Allocation Policies: Implement policies and protocols to
prevent the conditions necessary for deadlocks from occurring. For
example, ensuring that processes request and acquire resources in a
predefined order can prevent circular wait situations.
• Elimination of Hold and Wait: Require processes to request all the
resources they need before starting execution to prevent processes
from holding resources while waiting for others.

17
Deadlock Avoidance

• In deadlock avoidance, the operating system checks whether the system is in safe
state(stable)(allocates the resources to each process sequentially) or in unsafe
state(unstable) at every step which the operating system performs.

• The process continues until the system is in safe state. Once the system moves to
unsafe state, the OS has to backtrack one step to look for disturbing situation.

• In simple words, The OS reviews each allocation so that the allocation doesn't
cause the deadlock in the system.
18
Deadlock Avoidance:

• Banker's Algorithm: Dynamically assess resource requests to ensure


that granting them will not lead to a deadlock. The Banker's algorithm
predicts whether allocating resources to a process will result in a safe
state, where deadlock is avoided.
• Request-Deny Approach: Resource requests are granted only if the
system can guarantee that deadlock will not occur as a result. If
granting a request would potentially lead to deadlock, it is denied.

19
Recovery from Deadlock:

• Process Termination: Identify and terminate one or more processes


involved in the deadlock to free up resources and allow the remaining
processes to continue execution.
• Resource Preemption: Forcefully take resources from processes
involved in the deadlock to break the circular wait and resolve the
deadlock.
• Killing Processes: In some cases, it may be necessary to restart the
system or kill all processes involved in the deadlock to recover from
the deadlock state.

20
Deadlock Ignorance

• Deadlock Ignorance is the most widely used approach among all the
mechanism. This is being used by many operating systems mainly for end
user uses. In this approach, the Operating system assumes that deadlock
never occurs.

• It simply ignores deadlock.

• This approach is best suitable for a single end user system where User uses
the system only for browsing and all other normal stuff
21
• In these types of systems, the user has to simply restart
the computer in the case of deadlock. Windows and
Linux are mainly using this approach.

22
Banker’s Algorithm
• The Banker's Algorithm is a resource allocation and deadlock avoidance algorithm used in
operating systems, particularly in multi-process and multi-threaded environments. It is designed
to ensure that the allocation of resources to processes occurs in a safe and deadlock-free manner.
The algorithm was named after the banking system, where banks only lend money if they have
enough reserves to cover all outstanding loans.

23
Key Components:
• Available Resources (Vector): Represents the currently available
instances of each resource type. It indicates how many instances of each
resource are available for allocation at any given time.
• Maximum Demand Matrix: Specifies the maximum number of resources
of each type that a process may request during its execution. It indicates
the maximum resource needs of each process.
• Allocation Matrix: Indicates the number of resources of each type
currently allocated to each process.
• Need Matrix: Represents the remaining resource needs of each process.
It is calculated as the difference between the maximum demand and the
currently allocated resources for each process.
24
Safety Algorithm:
• The Banker's Algorithm checks whether allocating resources to a process could potentially lead to a
deadlock. It does this by simulating the allocation of resources and verifying if a safe execution
sequence exists.
• Initialization: The algorithm starts with the available resources vector and the initial allocation and
maximum demand matrices.
• Resource Allocation: It then iterates through each process, trying to allocate resources to it.
• Safety Check: After allocating resources to a process, the algorithm checks if the system remains in a
safe state. A safe state is one where there exists a sequence of processes such that each process can
complete its execution and release its allocated resources, allowing the subsequent processes to
acquire the necessary resources without leading to a deadlock.
• Deadlock Avoidance: If allocating resources to a process could potentially lead to an unsafe state (i.e.,
a deadlock), the allocation is denied, and the process is blocked until sufficient resources become
available.
• Release Resources: Once a process completes its execution, it releases all allocated resources, making
them available for allocation to other processes.
25
• Example:
• Consider a system with three types of resources (A, B, and C) and
three processes (P1, P2, and P3). Here are the available resources,
maximum demand, allocation, and need matrices:
• Available Resources: [10,5,7]
• Maximum Demand Matrix:

A B C
P1 7 5 3
P2 3 2 2
P3 9 0 2
26
Allocation Matrix:

A B C

P1 0 1 0

P2 2 0 0

P3 3 0 2

27
Need Matrix (calculated as Maximum
Demand - Allocation):

A B C

P1 7 4 3

P2 1 2 2

P3 6 0 0

28
Current
Available Remaining
Process Allocation Max Need Resources Need Safe State

P1 [0, 1, 0] [7, 5, 3] [7, 4, 3] No

P2 [2, 0, 0] [3, 2, 2] [1, 2, 2] Yes

P3 [3, 0, 2] [9, 0, 2] [6, 0, 0] No

29
• Using the Banker's Algorithm, the system will allocate resources to
processes in such a way that it avoids deadlocks while ensuring
efficient resource utilization. The algorithm will also handle resource
requests from processes dynamically, ensuring that the system
remains in a safe state throughout its execution.

30
Example of Banker’s algorithm-
• Assume that we have the following resources:
• 5 Pen drives
• 2 Printers
• 4 Scanners
• 3 Hard disks
• Here, we have created a vector representing
total resources: Available = (5, 2, 4, 3).

31
• Assume there are four processes. The available resources are already
allocated as per the matrix table below-

32
33
• Here, the allocated resources is the total of these
columns:

• Allocated = (4, 2, 2, 3).

• We also create a Matrix to display the number of


each resource required for all the processes. This
matrix is called Need=(3,0,2,2)

34
35
• The available vector will be :
• Available=Available- Allocated
= (5, 2, 4, 3) -(4, 2, 2, 3)
=(1, 0, 2, 0)

36
Resource Request Algorithm

• Resource request algorithm enables you to represent the system


behavior when a specific process makes a resource request.
• Let understand this by the following steps:
• Step 1) When a total requested instance of all resources is lesser than
the process, move to step 2.
• Step 2) When a requested instance of each and every resource type is
lesser compared to the available resources of each type, it will be
processed to the next step. Otherwise, the process requires to wait
because of the unavailability of sufficient resources.
• Step 3) Resource is allocated as shown in the below given Pseudocode.
37
38
Characteristics of Banker’s Algorithm

• Keep many resources that satisfy the


requirement of at least one client

• Whenever a process gets all its resources, it


needs to return them in a restricted period.

39
Disadvantage of Banker’s algorithm

• Does not allow the process to change its Maximum need


while processing

• It allows all requests to be granted in restricted time

• All processes must know and state their maximum


resource needs in advance.

40
Summary:

• Banker’s algorithm is used majorly in the banking system to avoid deadlock. It helps you to
identify whether a loan will be given or not.

• Notations used in banker’s algorithms are 1) Available 2) Max 3) Allocation 4) Need

• Resource request algorithm enables you to represent the system behavior when a specific
process makes a resource request.

• Banker’s algorithm keeps many resources that satisfy the requirement of at least one client

• The biggest drawback of banker’s algorithm this that it does not allow the process to change its
Maximum need while processing.

41
Examples

42
• For a given system with three resource types (A, B, C) and three processes (P0, P1, P2), where the total available
resources are [10, 5, 7], and the maximum demand of each process is given by the max matrix below, determine if
the system is in a safe state or not:

• Max Matrix:
• | P0 | P1 | P2 |
• |----|----|----|
• |7 |5 |3 |
• |3 |2 |2 |
• |9 |0 |2 |
• Given the following system state represented by the allocation matrix, calculate the total number of instances of
each resource type in the system:
• | P0 | P1 | P2 |
• |----|----| ----|
• |2 |1 |3 |
• |0 |3 |0 |
• |3 |0 |2 |

43
• Given the following system state represented by the allocation matrix, max matrix, available
resources, and request matrix, determine if granting the request will lead to a safe state using the
Banker's algorithm:
• Allocation Matrix:
• | P0 | P1 | P2 |
• |----|----|----|
• |0 |1 |0 |
• |2 |0 |0 |
• |3 |0 |3 |
• Max Matrix:

• | P0 | P1 | P2 |
• |----|----|----|
• |7 |5 |3 |
• |3 |2 |2 |
• |9 |0 |2 |
• Available Resources: [3, 3, 2]
• Request Matrix for P1: [0, 0, 1]

44

You might also like