OS Chapter -4
OS Chapter -4
Deadlock
Senait G. (MSc)
The Deadlock Problem
✓ A deadlock is a situation where a process or a set of processes is blocked,
waiting for some resource that is held by some other waiting process.
✓ If each process now requests another drive, the two processes will be in a
deadlocked state.
Bridge Crossing Example
2. Use: The process can operate on the resource (example, print on printer).
No preemption
Circular wait
Cont…
Mutual exclusion- only one process at a time can use a resource. If another
process requests that resource, the requesting process must be delayed until the
resource has been released.
Hold and wait- A process must be simultaneously holding at least one resource
and waiting for at least one resource that is currently being held by some other
process.
Circular Wait- There must be a circular chain of two or more processes, each of
which is waiting for a resource held by the next member of the chain.
Resource-Allocation Graph
✓ A deadlock is described in terms of a direct graph called a
system RAG
✓ A set of vertices V and a set of edges E.
✓ V is partitioned into two types:
◦ P = {P1, P2, …, Pn}, the set consisting of all the processes in
the system.
◦ R = {R1, R2, …, Rm}, the set consisting of all resource types
in the system.
✓ Request edge – directed edge Pi → Rj
✓ Assignment edge – directed edge Rj → Pi
Con’t…
Process
Pi requests instance of Rj Pi
Pi is holding an instance of Rj Pi
Example of a Resource Allocation Graph
Process State:
✓ P1 is holding an instance of R2 and waiting for an
instance of R1.
✓ P2 is holding an instance of R1 and R2, and is
✓ P2→R3→P3→R2→P2
✓ We have a cycle:
P1→R1→P3→R2→P1
✓P4 may release its instance of R2 and
◦ Detection and recovery. Let deadlocks occur, detect them, and take action.
◦ Just ignore the problem. Maybe if you ignore it, it will ignore you.
Deadlock Prevention
associated memory,
For Example: Tape drive, as only one process can access data from a Tape drive at a time.
For other resources like printers, we can use a technique called Spooling.
A Printer has associated memory which can be used as a spooler directory (memory that is
In spooling, when multiple processes request the printer, their jobs ( instructions of the
processes that require printer access) are added to the queue in the spooler directory.
The printer is allocated to jobs on a first come first serve (FCFS) basis. In this way, the
process does not have to wait for the printer and it continues its work after adding its job to
the queue.
Cont…
Cont…
Spooling suffers from two kinds of problems.
2. After some point of time, there may arise a race condition between
the processes to get space in that spool.
Protocol 1: request all tape, disk, printer and hold for entire
execution; note printer will be idle for a long time.
Cont…
The problem with the approach is:
1.Practically not possible. (As a process executes
instructions one by one, it cannot know about all
required resources before execution.)
2.Low resource utilization
3.Possibility of getting starved will be increases due to the
fact that some process may hold a resource for a very
long time.
3. No preemption
To eliminate circular wait, we assign a priority to each resource. A process can only
request resources in increasing order of priority.
Con’t…
Example: Let R={R1, R2, …, Rm} be the set of resource types.
✓ Assign to each resource type a unique integer number to compare two
resources and to determine whether one proceeds another in
ordering.
✓ For example: If the set of resource types R includes tape drives, disk
drives, and printers, then a priority might be defined as follows:
Among all the methods, violating Circular wait is the only approach that can
be implemented practically.
Deadlock Avoidance
✓ Deadlock Avoidance is a process used by the Operating System to avoid
Deadlock.
✓ Requires that the system has some additional a priori information about the use
of resources by processes
how can Operating System avoid Deadlock?
Operating System avoids Deadlock by knowing the maximum resources
requirements of the processes initially, and also Operating System knows the
free resources available at that time.
Operating System tries to allocate the resources according to the process
requirements and checks if the allocation can lead to a safe state or an unsafe
state.
If the resource allocation leads to an unsafe state, then Operating System does not
proceed further with the allocation sequence.
✓ Simplest and most useful model requires that each process declare
the maximum number of resources of each type that it may need.
✓ The deadlock-avoidance algorithm dynamically examines the
resource-allocation state to ensure that there can never be a circular-
wait condition.
✓ Resource-allocation state is defined by the number of available and
allocated resources, and the maximum demands of the processes.
Cont…
The main difference between deadlock prevention and deadlock
avoidance is that the deadlock prevention ensures that at least
one of the necessary conditions to cause a deadlock will never
occur, while deadlock avoidance ensures that the system will not
enter an unsafe state.
Safe State
✓ A state is safe if the system can allocate all resources requested by all
of deadlock.
the process
edge.
Resource-Allocation Graph For Deadlock
Avoidance
✓ From it’s name, it could be used in a banking system to ensure that the bank never
allocates its available cash such that it can no longer satisfy the needs of all
customers.(banks never run out of the resources or money and always been in a safe
state).
Data Structures for the Banker’s Algorithm
Let “n” be the number of processes and “m” be the number of resources
types. So we needs the following DS to implement Bankers algorithm.
Available: Vector of length “m” indicates the number of available resources
of each type.
Note: “n” is number of processes in the system and “m” is the number of
resource types.
✓ Resources allocated to deadlocked processes will be idle until the deadlock can
be broken.
✓ In addition, the number of processes involved in the deadlock cycle may grow.
✓ Deadlocks occur only when some process makes a request that cannot be
granted immediately.
Recovery from Deadlock
✓ When a detection algorithm determines that a deadlock exists, several
✓ One possibility is to inform the operator that a deadlock has occurred and to
let the operator deal with the deadlock manually.
✓ Another possibility is to let the system recover from the deadlock automatically.
✓ The deadlocked processes may have computed for a long time, and
the results of these partial computations must be discarded and
probably will have to be recomputed later.
B. Abort one process at a time until the deadlock cycle is
eliminated:
✓ This method incurs considerable overhead, since after each
process is aborted, a deadlock-detection algorithm must be invoked
to determine whether any processes are still deadlocked.
Aborting a process may not be easy. If the process was in the midst
of updating a file, terminating it will leave that file in an incorrect
state.
Recovery from Deadlock: Resource Preemption
✓ If preemption is required to deal with deadlocks, then three issues need to be
addressed:
Rollback: If we preempt a resource from a process, what should be done with that
process? We must roll back the process to some safe state, and restart it from
that state.