You are on page 1of 51

Operating

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;

 Deadlock occurs if both processes progress to their


second request
Consumable Resources
Deadlock
 Consider a pair of processes, in which each process attempts to receive a
message from the other process and then send a message to the other process:

 Deadlock occurs if the Receive is blocking


Table 6.1

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

• adopt a policy that eliminates one of the conditions

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

if access to a resource requires mutual


exclusion then it must be supported by
the OS
Deadlock Condition
Prevention
 No Preemption
 if a process holding certain resources is denied a further request, that
process must release its original resources and request them again
 OS may preempt the second process and require it to release its
resources

 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

TOTAL RESOURCES AVAILABLE


A B C A B C
10 5 7 3 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 2 1 1
P5 0 0 2 5 3 3 5 3 1
Total 7 2 5
Allocated

TOTAL RESOURCES AVAILABLE State-


A B C A B C P2

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

TOTAL RESOURCES AVAILABLE State-


A B C A B C P2-P4

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

TOTAL RESOURCES AVAILABLE State-


A B C A B C P2-P4-P5

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

TOTAL RESOURCES AVAILABLE State-


A B C A B C P2-P4-P5-P1

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

TOTAL RESOURCES AVAILABLE State-


A B C A B C P2-P4-P5-P1-P3

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

TOTAL RESOURCES AVAILABLE Unsafe State-


A B C A B C Can lead to deadlock

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

If Available = [ 0 0 X 1 1 ], what is the smallest value of x for which this is a


safe state?

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

• Processes under consideration must be independent and


with no synchronization requirements

• There must be a fixed number of resources to allocate

• No process may exit while holding resources


Deadlock Strategies

Deadlock prevention strategies are very


conservative
• limit access to resources by imposing restrictions on
processes

Deadlock detection strategies do the opposite


• resource requests are granted whenever possible
Deadline Detection
Algorithms
Advantages:
• it leads to early
 A check for deadlock can detection
be made as frequently as • the algorithm is
each resource request or, relatively simple
less frequently, depending
on how likely it is for a
deadlock to occur
Disadvantage
• frequent checks
consume
considerable
processor time
Deadline Detection –Wait
For Graph
 If all resource types has only single instance, then we can use
a graph called wait-for-graph, which is a variant of resource
allocation graph.

 Here, vertices represent processes and a directed edge from


P1 to P2 indicate that P1 is waiting for a resource held by P2.

 A cycle in a wait-for-graph indicate a deadlock. So the


system can maintain a wait-for-graph and check for cycles
periodically to detect any deadlocks.
Deadline Detection –Wait
For Graph

 Disadvantage- The wait-for-graph is not much useful if there are


multiple instances for a resource, as a cycle may not imply a
deadlock. In such a case, use an algorithm similar to Banker’s
algorithm to detect deadlock.
Deadlock Detection
Algorithm
Deadlock Detection
Algorithm
Process Allocation A Request Q

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

TOTAL RESOURCES AVAILABLE W


R1 R2 R3 R4 R5 R1 R2 R3 R4 R5 R1 R2 R3 R4 R5
2 1 1 2 1 0 0 0 0 1 0 0 0 0 1
Deadlock Detection
Algorithm
Process Allocation A Request Q

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

TOTAL RESOURCES AVAILABLE W


R1 R2 R3 R4 R5 R1 R2 R3 R4 R5 R1 R2 R3 R4 R5
2 1 1 2 1 0 0 0 1 1 0 0 0 1 1
Deadlock Detection
Algorithm
 No other process has a row in Q that is less than or equal to W.
 Therefore, Terminate the algorithm
 The Algorithm concludes with P1 and P2 unmarked indicating these
processes are deadlocked.
Recovery Strategies
 Abort all deadlocked processes
 Back up each deadlocked process to some previously defined
checkpoint and restart all processes
 Successively abort deadlocked processes until deadlock no longer
exists
 Successively preempt resources until deadlock no longer exists
Dining Philosophers Problem
 No two philosophers
can use the same fork at
the same time (mutual
exclusion)

 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

Figure 6.13 A Second Solution to the Dining Philosophers Problem


Another Solution to Dining
Philosophers Deadlock Problem
Reverse the order of Fork picking for last philosopher.

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

You might also like