OS Unit 4 Deadlock
OS Unit 4 Deadlock
• Deadlock
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• Dining philosopher's problem
Deadlock
• Permanent blocking of a set of processes
that compete for system resources
• A set of processes is deadlocked when
each of the process in the set is blocked
awaiting an event that can only be
triggered by another blocked process in
the set.
• Involve conflicting needs for resources by
two or more processes
Deadlock
• Under normal mode of operation, a
process may utilize a resource in following
sequence:
1. Request
2. Use
3. Release
System Table
→ Records whether each resource is free or
allocated
→If allocated then to which process
→If a process requests a resource currently
allocated to another process then it is
added to a queue of processes waiting for
that resource.
Consumable Resources
• Created (produced) and destroyed
(consumed)
• There is no limit on no of consumable
resources of a particular type.
• Information in I/O buffers, Interrupts,
signals, messages
Reusable Resources
• Used by only one process at a time and
not depleted by that use
• Processes obtain resources that they later
release for reuse by other processes
• Processors, I/O channels, main and
secondary memory, devices, and data
structures such as files, databases
• Deadlock occurs if each process holds
one resource and requests the other
Resource Allocation Graphs
• Directed graph that depicts a state of the
system of resources and processes
Resource Allocation Graphs
Resource Allocation Graphs
Resource Allocation Graphs
Conditions for Deadlock
• Mutual exclusion
– Only one process may use a resource at a
time
• Hold-and-wait
– A process may hold allocated resources while
awaiting assignment of others
Conditions for Deadlock
• No preemption
– No resource can be forcibly removed form a
process holding it
• Circular wait
– A closed chain of processes exists, such that
each process holds at least one resource
needed by the next process in the chain
Possibility of Deadlock
• Mutual Exclusion
• No preemption
• Hold and wait
Existence of Deadlock
• Mutual Exclusion
• No preemption
• Hold and wait
• Circular wait
Handling Deadlocks
• Prevent
• Avoid
• Detect
Deadlock Prevention
4. If Finish [i] == true for all i, then the system is in a safe state
Resource-Request Algorithm for Process Pi
Need
ABC
P1 743
P2 122
P3 600
P4 011
P5 431
• The system is in a safe state since the sequence < P2, P4, P5,
P1, P3> satisfies safety criteria
Example of Banker’s
Algorithm
• 5 processes P1 through P5; (4,2,1) P1
3 resource types:
A (10 instances), B (5 instances), and C (7 instances)
• Snapshot at time T0:
Need Allocation Max Available(Work)
ABC ABC ABC ABC
743 P1 010 753 332
122 P2 200 322 532
600 P3 302 902 743
011 P4 211 222 745
431 P5 002 433 755 10 5 7
• Executing safety algorithm shows that sequence < P1, P3, P4,
P0, P2> satisfies safety requirement
• Detection algorithm
• Recovery scheme
Single Instance of Each Resource Type
f1
P3
f4
P0
f0
P4
Dining Philosophers Problem
Case-1 : Philosophers eat one at a time
void philosopher (void) P0 – f0 , f1
{ P1 - f1 , f2
while(true) P2 - f2 , f3
P3 – f3 , f4
{ P4 - f4 , f0
Thinking();
take_fork(i);
take_fork((i+1)%N);
Eat();
put_fork(i);
put_fork((i+1)%N);
}
}
Dining Philosophers Problem
Case-2 : Two or more Philosophers want to eat at same time
semaphore s[5]=1;
void philosopher (void) S0 S1 S2 S3 S4
{ 1 1 1 1 1
while(true)
{
Thinking();
wait(take_fork(si)); P0 – f0 , f1 – S0 , S1
wait(take_fork((si+1)%N)); P1 - f1 , f2 - S1 , S2
P2 - f2 , f3 - S2 , S3
Eat(); // Critical Section P3 – f3 , f4 - S3 , S4
signal(put_fork(si)); P4 - f4 , f0 - S4, S0
signal(put_fork((si+1)%N));
}
}
Dining Philosophers Problem
Case-3 : Deadlock
semaphore s[5]=1;
void philosopher (void) S0 S1 S2 S3 S4
{ 1 1 1 1 1
while(true)
{
Thinking();
wait(take_fork(si));
wait(take_fork((si+1)%N)); P0 – f0 , f1 – S0 , S1
Eat(); // Critical Section P1 - f1 , f2 - S1 , S2
P2 - f2 , f3 - S2 , S3
signal(put_fork(si));
P3 – f3 , f4 - S3 , S4
signal(put_fork((si+1)%N)); P4 - f4 , f0 - S4, S0
}
}
Consider the following snapshot of a system
Answer the following using Banker’s Algorithm
•What is the content of Need Matrix?
•Is the system in safe state?
•If the request from P1 arrives for (0, 4, 2, 1); can the request be granted immediately?