2. What is dead lock
In multiprogramming environment , several processes
compete for resources. A situation may arise where a
process is waiting for resources that is held by other
waiting processes. This situation is called a dead lock.
2
3. Deadlock is defined as a situation where set of
processes are blocked because
each process holding a resource and waiting to
acquire a resource held by another process,
A deadlock happens in operating system when two or
more processes need some resource to complete
their execution that is held by the other process.
3
4. Deadlock…
Consider an example when two trains/ vehicle are coming toward
each other on same track and there is only one track, none of the
trains can move once they are in front of each other.
Similar situation occurs in operating systems.
4
5. Note:
A process in operating systems uses different resources , the resources are
used in the following way.
1) Requests a resource
2) Use the resource
2) Releases the resource
5
7. Dead lock….
In the above diagram, the process 1 has resource 1 and needs to
acquire resource 2.
Similarly process 2 has resource 2 and needs to acquire resource 1.
Process 1 and process 2 are in deadlock as each of them needs the
other’s resource to complete their execution but neither of them is
willing to release their resources.
7
8. Deadlock Example
One process has access to printer but is waiting for tap drive ,
while another process has access to tape drive but is waiting for
printer.
8
9. Resources
A resources is an object that is used by a process. It can be a piece of hardware
such as
Tape drive
Printer
Scanner
Buffers
Memory etc.
9
10. Types of Resources
1) Pre-emptible Resources :
A Pre-emptible Resource is one that can be allocated to a given process for a
period of time. Then it can be allocated to another process. Then its can reallocated
to the first process without any negative effects.
Example: memory, buffers etc.
2) Non Pre-emptible Resources :
A Non Pre-emptible Resource cannot be taken from one process and given to
another without side effects.
One example is printer. A printer cannot be taken away from one process and given
to another process in the middle of a print job.
Note: Deadlocks usually involve Non Pre-emptible Resources.
10
11. 6.3 Deadlock Characterization
Deadlock can arise if following four conditions take place
simultaneously in a system. (Necessary Conditions)
1) Mutual Exclusion
2) Hold and Wait
3) No Preemption
4) Circular Wait
11
12. Deadlock Characterization
1) Mutual Exclusion: Only one process at a time can use a
resource. If another process request that resources the
requesting process will delayed until the resource has
been released .
In the diagram below, there is a single instance of Resource
1 and it is held by Process 1 only.
12
13. 2) Hold and Wait: A process must be holding at least one
resource and waiting to acquire additional resources that are
concurrently being held by other processes.
In the diagram given above, Process 2 holds Resource 2 and
Resource 3 and is requesting the Resource 1 which is held by
Process 1.
13
14. 3) No preemption: A resource can be released only voluntarily
by the process holding it, after that process has completed its
task. OR
A resource cannot be preempted from a process by force. A
process can only release a resource voluntarily.
In the diagram, Process 2 cannot preempt Resource 1 from Process
1. It will only be released when Process 1 release it voluntarily
after its execution is complete.
14
15. 4) Circular Wait:
There exists a set {P0, P1, …, Pn} of waiting processes
such that P0 is waiting for a resource that is held by P1, P1
is waiting for a resource that is held by P2, …, Pn–1 is
waiting for a resource that is held by
Pn, and Pn is waiting for a resource that is held by P0.
15
16. OR
A process is waiting for the resource held by the
second process, which is waiting for the resource
held by the third process and so on, till the last
process is waiting for a resource held by the first
process. This forms a circular chain..
16
17. For example: Process 1 is allocated Resource2 and it is
requesting Resource 1. Similarly, Process 2 is allocated
Resource 1 and it is requesting Resource 2. This forms a
circular wait loop
17
18. Deadlock Detection
A deadlock can be detected by a resource scheduler as it
keeps track of all the resources that are allocated to
different processes. After a deadlock is detected, it can be
resolved using the following methods
All the processes that are involved in the deadlock are
terminated. This is not a good approach as all the progress
made by the processes is destroyed.
Resources can be preempted from some processes and given
to others till the deadlock is resolved.
18
19. Method for Handling Deadlocks
1. Deadlock Prevention
2. Deadlock Avoidance
3. Deadlock Detection and Recovery
19
20. Deadlock Prevention
We can prevent deadlocks by ensuring that at least one of the four
necessary conditions for deadlock cannot occur. If at least one condition is
not satisfied, a deadlock will not occur.
20
21. Deadlock Prevention…..
1) Mutual Exclusion
Mutual exclusion condition must hold for non-sharable resources.
For example, only one process can have access to a printer at a time, otherwise
the output is disturbed.
Some resources can be made sharable like a read-only file.
Several processes can be granted read- only access to a file without interfering
with each other.
However, deadlock cannot be prevented by only denying the mutual exclusion
condition because some resources are intrinsically (by birth) non-sharable.
21
22. Deadlock Prevention…..
Elimination of Mutual Exclusion
If it is shareable resource, then we can break the mutual exclusion (such as:
Read-only file)
If it is not a shareable resource, then mutual exclusion must hold (such as:
Printer) because non shareable means that their states cannot be saved and
restored without ill effects.
22
23. Deadlock Prevention…..
2) Elimination of Hold and wait: two methods
A) Require process to request and be allocated all its resources before it begins
execution.
B) Allow process to request resources only when the process has none.
Example:
We consider a process that copies data from DVD drive to a picture file on disk
and then prints the picture to a printer
Method 1: Request DVD drive, Disk, and Printer before it execute. It will hold
the Printer for entire execution, even though it needs the printer only at the end.
Method2: Request only for DVD drive and Disk initially. It finishes the copy step
and releases both resources. The process must then again ask for disk and printer
To finish the job.
23
24. Deadlock Prevention…..
3) Elimination of No preemption:
a) Release any resource already being held if the process can't get an
additional resource.
b) Allow preemption - if a needed resource is held by another process,
which is also waiting on some resource, steal it. Otherwise wait.
c) Process will be restarted only when it can regain its old Resources, as
well as the new ones that it is requesting.
24
25. Deadlock Prevention…..
4) Elimination of Circular wait
Each resource will be assigned with a numerical number. A process
can request the resources only in increasing order of numbering.
For Example,
if P1 process is allocated R5 resources, now next time if P1 ask for
R4, R3 lesser than R5 such request will not be granted, only request
for resources more than R5 will be granted.
25
26. Deadlock Avoidance
Deadlock avoidance can be done with help of Banker’s Algorithm.
Banker’s Algorithm:
Bankers' Algorithm is resource allocation and deadlock avoidance
algorithm which test all the request made by processes for resources,
it checks for the safe state, if after granting request system remains
in the safe state it allows the request and if there is no safe state it
doesn’t allow the request made by the process.
26
27. Safe State and Unsafe State
A state is said to be a safe state if the system may allocate
the required resources to each process up to the maximum
required in a particular sequence without facing deadlock.
In safe state , deadlock cannot occur.
If a deadlock is possible the system is said to be in an
unsafe state.
The idea of avoiding a deadlock is to simply not allow the
system to enter an unsafe state that may cause a deadlock
27
28. Inputs to Banker’s Algorithm:
1) Max need of resources by each process.
2) Currently allocated resources by each
process.
3) Max free available resources in the
system.
28