0% found this document useful (0 votes)
12 views17 pages

Deadlock

The document discusses deadlock, which occurs when processes are waiting for resources held by other processes in a circular chain. It describes the four conditions for deadlock and methods for handling deadlocks, including prevention through allocating all resources in advance or ensuring a specific ordering, and avoidance using algorithms like Banker's algorithm to ensure the system remains in a safe state.

Uploaded by

imrank39199
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)
12 views17 pages

Deadlock

The document discusses deadlock, which occurs when processes are waiting for resources held by other processes in a circular chain. It describes the four conditions for deadlock and methods for handling deadlocks, including prevention through allocating all resources in advance or ensuring a specific ordering, and avoidance using algorithms like Banker's algorithm to ensure the system remains in a safe state.

Uploaded by

imrank39199
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/ 17

Deadlock

Dr. M. Bilal Qureshi

1
Deadlock
• Situation when resources are allocated to active processes, so
that no further processing is possible.
• Deadlock occurs when
resources ˂˂ processes
• A process in a multi-programming system is said to be in a state
of deadlock if it is waiting for a particular event that will not
occur.

2
Deadlock
• Consider two processes P1 and P2 and two resources R1 and
R2.
• Suppose P1 holds R1 and P2 holds R2. In the mean time P1
needs R2 and P2 needs R1 but no process can get extra
resources because they are already provided, so they are waiting
for other resources and the deadlock situation occurs.

P1 P2
Wait for

Wait for
holds holds

R1 R2

3
Conditions for deadlock occurrence
• Four conditions
 Mutually exclusive
 Wait for
 Non pre-emptive
 Circular chain

• Mutually exclusive
• Every process can be activated at any time and all active
processes have the same rights for sharing resources i.e.,
every process can request for every resources.

4
Conditions for deadlock occurrence
• Wait for
• When there is an infinite wait for a resource, it can lead to
deadlock situation.
P1 P2

holds holds
Request for

R1 R2

Drop this request


i.e., R2 cannot be
assigned to P1.

5
Conditions for deadlock occurrence
• Non pre-emptive
• If processes are pre-emptive deadlock can’t occurs.
• Circular chain
• If there exists a set of waiting processes {P0, P1, P2, …, Pn},
such that P0 is waiting for resource which is held by P1, and
P1 is waiting for resource which is held by P2, and Pn-1 is
waiting for resource which is held by Pn and Pn is waiting
for resources which is held by P0. This situation is called
circular chain (wait) which causes deadlock.
• When circular chain occurs, it means deadlock condition
occurs.

6
Conditions for deadlock occurrence
• Let there are three active processes P1, P2 and P3 and there are
three resources available R1, R2, and R3.

Drop request P1 Holds

R3 R1

Holds Drop request

P3 P2

Drop request R2 Holds

Circular chain condition example


7
Deadlock handling
• Two methods for dealing with deadlock problems
 Some protocol can be used to ensure that the system will
never enter into a deadlock state.
 Allow the system to enter a deadlock state and then recover
from deadlock.
 Ignore the problem and pretend that deadlocks never occur.

• Recovery from deadlock is quite difficult and expensive,


therefore methods of ensuring that deadlock will never occur are
considered. Following are such two methods.
 Deadlock prevention
 Deadlock avoidance

8
Deadlock prevention
• Most frequently used method.
• Various strategies for denying various conditions for deadlock
occurrence have been suggested.
1. All the resources must be allocated in advance and process
can’t be started if all resources are not available.
(denying the wait-for condition)

9
Deadlock prevention
P1 P2 P3

R1 R2 R3 R2 R4 R5 R3 R6 R7

The processes P1, P2, P3 cannot be executed in parallel because


some resources are common to all processes in this situation.

10
Deadlock prevention
2. If the request is denied, the process must release its original
resources and if necessary, request again with additional
resources.
(denying the wait-for and non pre-emption condition)

3. All the resources must be allocated in a certain order. i.e., all


processes should request resources by sequence number of
resources in a particular predetermined sequence.
(denying circular chain condition)

11
Deadlock avoidance
The side effects of deadlock prevention are
• Low device utilization and
• Reduced system efficiency

Therefore, deadlock avoidance is used.


• The resources are allocated and at the time of resources
allocation, the situation is checked whether deadlock will occur
or not.
• The most popular algorithm used for efficient resource
allocation was devised by Bukstra known as Banker’s
algorithm.

12
Banker’s algorithm
“All available resources should be allocated in a manner that it can
guarantee safe state.”

• Safe state means such a state where it is visible that all


processes could be completed successfully.

13
Banker’s algorithm
Example of safe state:
Available storage resources = 12
Active processes = 3
Active processes Max required Currently
resources allocated
resources
P1 10 4
P2 6 4
P3 4 2

Remaining available resources = 2

• According to Banker’s algorithm, each process cannot request


number of resources larger than the total number of available
resources. 14
Banker’s algorithm
• If 2 of the remaining resources are allocated to P3 then it can be
completed successfully by releasing 4 resources.
Now total available resources = 4

• Allocate 2 resources to P2, and it can be completed successfully


by releasing 6 resources.
Now total available resources = 8

• Allocate 6 of the 8 available resources to P1 and it can be


completed successfully.
• Hence resource allocation is made in such a way that all active
processes are completed successfully.

15
Banker’s algorithm
Example of un-safe state:

Active processes Max required Currently


resources allocated
resources
P1 10 8
P2 5 2
P3 3 1

Remaining available resources = 1

16
Starvation
• Situation in which resources will always be pre-empted from
same process. As a result this process is never completed its task
and starvation situation occurs.

17

You might also like