deadlock - SNS Courseware

PARALLEL PROGRAM
CHALLENGES
Deadlocks and livelocks
Learning Objective s& Outcomes
• Objective:
– To get exposed to the basics of deadlocks and Livelocks
• Outcomes:
After completing this class, you should be able
to describe:
– Causes of system deadlock and livelock
– The difference between deadlocks and livelocks
2
Deadlock
Analogy : Traffic Jam
3
Deadlock
• Deadlock, where two or more threads cannot
make progress because the resources that
they need are held by the other threads.
4
Deadlock
• Suppose two threads need to acquire mutex
locks A and B to complete some task. If thread
1 has already acquired lock A and thread 2 has
already acquired lock B, then
• A cannot make forward progress because it is
waiting for lock B, and thread 2 cannot make
progress because it is waiting for lock A. The
two threads are deadlocked.
5
Two Threads in a Deadlock
Thread 1
Thread 2
•
•
•
•
•
•
•
•
•
•
•
•
void update1()
{
acquire(A);
acquire(B); <<< Thread 1 waits here
variable1++;
release(B);
release(A);
}
•
•
•
•
void update2()
{
acquire(B);
acquire(A); <<< Thread 2
waits here
variable1++;
release(B);
release(A);
}
6
Livelock
• A livelock is similar to a deadlock, except that
the states of the processes involved in
the livelock constantly change with regard to
one another, none progressing. Livelock is a
special case of resource starvation; the
general definition only states that a specific
process is not progressing
7
Livelock
• A livelock traps threads in an unending loop
releasing and acquiring locks. Livelocks can be
caused by code to back out of deadlocks. The
following code
tried to implement a
mechanism that avoids deadlocks. If the
thread cannot obtain the second lock it
requires, it releases the lock that it already
holds.
8
Two Threads in a Live lock
Thread 1
Thread 2
void update1()
{
int done=0;
while (!done)
{
acquire(A);
if ( can Acquire (B) )
{
variable1++;
release(B);
release(A);
done=1;
}
else
release(A);
}
}
void update2()
{
int done=0;
while (!done)
{
acquire(B);
if ( can Acquire (A) )
{
variable1++;
release(A);
release(B);
done=1;
}
else
release(B);
}
}
9
Quiz
1.
Which of the following condition is
required for deadlock to be possible?
A. mutual exclusion
B. a process may hold allocated resources
while awaiting assignment of other
resources
C. no resource can be forcibly removed from a
process holding it
D. all of the mentioned
2. For effective operating system, when to
check for deadlock?
A. every time a resource request is made
B. at fixed time intervals
C. both (a) and (b)
D. none of the mentioned
• Answer C
• Answer D
10