Professional Documents
Culture Documents
Systems:
Internals Chapter 6
and Design Concurrency:
Principles
Deadlock and
Starvation
Eighth Edition
By William Stallings
Deadlock
The permanent blocking of a set of processes that
either compete for system resources or communicate
with each other
A set of processes is deadlocked when each process
in the set is blocked awaiting an event that can only
be triggered by another blocked process in the set
Permanent
No efficient solution
o
a
r
n
s
d
,
Resource Categories
d
e
s
I
/
O
t
r
c
o
h
y
a
e
n
d
n
e
(
l
c
s
o
,
n
s
m
u
a
m
i
e
n
d
)
• ai
n
n
d
t
e
s
r
e
Figure 6.4
Example of Two Processes Competing for Reusable Resources
Example 2:
Memory Request
Space is available for allocation of 200Kbytes, and the
following sequence of events occur:
P1 P2
... ...
Request 80 Kbytes; Request 70 Kbytes;
... ...
Request 60 Kbytes; Request 80 Kbytes;
Summary of
Deadlock
Detection,
Prevention, and
Avoidance
Approaches for
Operating
Systems
[ISLO80]
Conditions for Deadlock
Mutual No Pre-
Hold-and-Wait Circular Wait
Exclusion emption
•only one •a process •no resource •a closed chain
may hold of processes
process can be exists, such that
allocated forcibly each process
may use resources removed holds at least
a while one resource
from a
awaiting needed by the
resource assignment process next process in
at a time of others holding it the chain
Dealing with Deadlock
Three general approaches exist for dealing with deadlock:
Prevent Deadlock
Avoid Deadlock
• make the appropriate dynamic choices based on the current state
of resource allocation
Detect Deadlock
• attempt to detect the presence of deadlock and take action to
recover
Deadlock Prevention
Strategy
Design a system in such a way that the possibility of deadlock is
excluded
Two main methods:
Indirect
prevent the occurrence of one of the three necessary conditions
Direct
prevent the occurrence of a circular wait
Deadlock Condition
Prevention
Mutual Exclusion
Circular Wait
define a linear ordering of resource types
If a process has been allocated resources of type R , then it may
subsequently request only those resources of types following R in the
ordering. Index is associated with each resource.
A resource Ri precedes Rj in the ordering if i < j . Now suppose that two
processes, A and B, are deadlocked because A has acquired Ri and
requested Rj , and B has acquired Rj and requested Ri . This condition is
Deadlock Avoidance
A decision is made dynamically whether the current resource
allocation request will, if granted, potentially lead to a
deadlock
Requires knowledge of future process requests
Two Approaches to
Deadlock Avoidance
Resource
Allocatio Deadloc
n Denial Process
• do not k Initiation
grant an Avoidan Denial
increment • do not
al
ce start a
resource process if
request to its
a process demands
if this might
allocation lead to
might deadlock
lead to
Resource Allocation Denial
Referred to as the banker’s algorithm
State of the system reflects the current allocation of resources
to processes
Safe state is one in which there is at least one sequence of
resource allocations to processes that does not result in a
deadlock
Unsafe state is a state that is not safe
Banker’s Algorithm-
Example
Process Allocation A Claim C ( Max Need) Remaining Need ( C-
A)
A B C A B C A B C
P1 0 1 0 7 5 3 7 4 3
P2 2 0 0 3 2 2 1 2 2
P3 3 0 2 9 0 2 6 0 0
P4 2 1 1 4 2 2 2 1 1
P5 0 0 2 5 3 3 5 3 1
Total 7 2 5
Allocated
10 5 7 5 3 2
Banker’s Algorithm
Process Allocation A Claim C ( Max Need) Remaining Need ( C-
A)
A B C A B C A B C
P1 0 1 0 7 5 3 7 4 3
P2 2 0 0 3 2 2 0 0 0
P3 3 0 2 9 0 2 6 0 0
P4 2 1 1 4 2 2 0 0 0
P5 0 0 2 5 3 3 5 3 1
Total 7 2 5
Allocated
10 5 7 7 4 3
Banker’s Algorithm
Process Allocation A Claim C ( Max Need) Remaining Need ( C-
A)
A B C A B C A B C
P1 0 1 0 7 5 3 7 4 3
P2 2 0 0 3 2 2 0 0 0
P3 3 0 2 9 0 2 6 0 0
P4 2 1 1 4 2 2 0 0 0
P5 0 0 2 5 3 3 0 0 0
Total 7 2 5
Allocated
10 5 7 7 4 5
Banker’s Algorithm
Process Allocation A Claim C ( Max Need) Remaining Need ( C-
A)
A B C A B C A B C
P1 0 1 0 7 5 3 0 0 0
P2 2 0 0 3 2 2 0 0 0
P3 3 0 2 9 0 2 6 0 0
P4 2 1 1 4 2 2 0 0 0
P5 0 0 2 5 3 3 0 0 0
Total 7 2 5
Allocated
10 5 7 7 5 5
Banker’s Algorithm
Process Allocation A Claim C ( Max Need) Remaining Need ( C-
A)
A B C A B C A B C
P1 0 1 0 7 5 3 0 0 0
P2 2 0 0 3 2 2 0 0 0
P3 3 0 2 9 0 2 0 0 0
P4 2 1 1 4 2 2 0 0 0
P5 0 0 2 5 3 3 0 0 0
Total 7 2 5
Allocated
10 5 7 10 5 7
Banker’s Algorithm
The sequence
P2-P4-P5-P1-P3 leads to safe state.
This sequence will not lead to deadlock
Banker’s Algorithm-
Unsafe sequence Example
Process Allocation A Claim C ( Max Need) Remaining Need ( C-
A)
A B C A B C A B C
P1 0 1 0 7 5 3 7 4 3
P2 2 0 0 3 2 2 1 2 2
P3 3 0 2 9 0 2 6 0 0
P4 2 4 1 5 4 4 3 0 3
P5 0 0 2 5 3 3 5 3 1
Total 7 5 5
Allocated
10 5 7 3 0 2
Figure 6.9
Deadlock
Avoidance Logic
Practice Questions
Ans- P2
Practice Questions
A system has 4 processes and 5 allocatable resource. The current allocation and
maximum needs are as follows-
Allocated Maximum
A 1 0 2 1 1 1 1 2 1 3
B 2 0 1 1 0 2 2 2 1 0
C 1 1 0 1 1 2 1 3 1 1
D 1 1 1 1 0 1 1 2 2 0
Ans- X=2
Practice Questions
Deadlock Avoidance
Advantages
It is not necessary to preempt and rollback processes, as in deadlock
detection
It is less restrictive than deadlock prevention
Deadlock Avoidance
Restrictions
• Maximum resource requirement for each process must
be stated in advance
R1 R2 R3 R4 R5 R1 R2 R3 R4 R5
P1 1 0 1 1 0 0 1 0 0 1
P2 1 1 0 0 0 0 0 1 0 1
P3 0 0 0 1 0 0 0 0 0 1
P4 0 0 0 0 0 1 0 1 0 1
Total 2 1 1 2 0
Allocated
R1 R2 R3 R4 R5 R1 R2 R3 R4 R5
P1 1 0 1 1 0 0 1 0 0 1
P2 1 1 0 0 0 0 0 1 0 1
P3 0 0 0 1 0 0 0 0 0 1
P4 0 0 0 0 0 1 0 1 0 1
Total 2 1 1 2 0
Allocated
No philosopher must
starve to death (avoid
deadlock and
starvation)
Dining Philosophers Problem
void philosopher
F2
{
While (true)
F3
{
Thinking();
Take_fork(i)//left fork
Take_fork((i+1)% N)//right fork F1
Eat();
put_fork(i)//left fork
put_fork((i+1)% N)//right fork F4
}
} Fo
Dining Philosophers Problem:
Sequential Execution
void philosopher Case 1- Po Followed by P1
{
While (true) Po takes fork F0
{ Po takes fork F1
Thinking(); Eat;
Take_fork(i)//left fork Po puts fork F0
Take_fork((i+1)% N)//right fork Po puts fork F1
Eat();
put_fork(i)//left fork P1 takes fork F1
put_fork((i+1)% N)//right fork P1 takes fork F2
} Eat;
} P1 puts fork F1
P1 puts fork F2
Dining Philosophers Problem:
Concurrent Execution
void philosopher Case 2- Po pre-empted by P1 after
{ taking left fork
While (true)
{ Po takes fork F0
Thinking(); //pre-empted by P1
Take_fork(i)//left fork
Take_fork((i+1)% N)//right fork P1 takes fork F1
Eat(); // pre-empted by P0
put_fork(i)//left fork
put_fork((i+1)% N)//right fork P0 waiting for F1 currently held
} by P1. Race Condition. Hence P0
} Blocked.
P1 Takes F2 if available.
Dining Philosophers Problem:
Concurrent Execution
void philosopher Case 3- Every philosopher takes
{ left fork and preempted by next
While (true) philosopher
{ Po takes fork F0
Thinking(); //pre-empted by P1
Take_fork(i)//left fork P1 takes fork F1
Take_fork((i+1)% N)//right fork // pre-empted by P2
Eat(); P2 takes fork F2
put_fork(i)//left fork // pre-empted by P3
put_fork((i+1)% N)//right fork P3 takes fork F3
} // pre-empted by P4
} P4 takes fork F4
Waits for right fork F0 currently
held by P0.
Leads to deadlock
Solution to Dining Philosophers
Problem using semaphores
void philosopher
{ N=5;
For (i=0 to N-1)
{
S[i]=1 //declare 5 semaphores each for a philosopher
}
S0 S1 S2 S3 S4
While (true)
1 1 1 1 1
{
Thinking(); P0 : S0,S1
Wait(Take_fork(Si))//left fork P1 : S1,S2
Wait (Take_fork(S(i+1)% N)//right fork P2 : S2,S3
Eat(); P3: S3,S4
put_fork(i)//left fork P4: S4, S0
put_fork((i+1)% N)//right fork
} Suffers From Deadlock
}
Solution to Dining Philosophers
Deadlock Problem
• Buy Five additional Forks/teach philosopher to eat using
single fork
• Add an attendant who will allow only four philosophers at a
time in a dining room
S0 S1 S2 S3 S4
1 1 1 1 1
Room = 4
P0 : S0,S1
P1 : S1,S2
P2 : S2,S3
P3: S3,S4
P4: S4, S0
S0 S1 S2 S3 S4
1 1 1 1 1
P0 : S0,S1
P1 : S1,S2
P2 : S2,S3
P3: S3,S4
P4: S0, S4