Transcript deadlock

Lecture 8
Restrictions on Resource Allocation
Dining Philosophers - Havender's Linear Ordering
Deadlock Detection
In some systems the OS allows deadlock to occur and then suspends or restarts the
deadlocked processes allowing others to proceed. To accomplish this recovery we must
first have a means of detecting deadlock. Remember that all processes running in a
multiprogramming environment need not be involved in the deadlock. Resource
allocation diagrams are a graphical representation of an algorithm for deadlock
detection.
R1
P1
R2
P2
P3
Each resource type is shown as a large circle,
each instance of a resource is shown as a
smaller circle in one of the large circles. Each
process is shown as a rectangle. When a
resource is allocated to a process an arrow is
drawn from the resource instance to the
process. When a process requests a resource
an arrow is drawn from the process to the
desired resource type.
Using Resource Allocation Graphs
to Prevent Deadlock
Consider a special case of resource allocation graphs in which you require that each
resource type contains no more than one more resource that any other resource type and
that a process may request at most one resource of each type.
Given 3 resource types (R), and 2 processes (P) what is the total number of resources N
(evenly disributed among the 3 resource types) needed to prevent the possibility of
deadlock?
R1
R2
R3
Develop an expression for the total number of
resources N needed to be distributed evenly across R
resource types to prevent deadlock in P processes
(again assume that each process can request at most
1 of each resource type).
P1
P2
A Few Sample Cases
For a maximum of m resource types allowed for each process find the maximum number
N-1 of resources, evenly distributed across the types for which deadlock can still occur.
m=1
m=2
:
R1
P1
R1
R2
P1
P2
R1
R2
R3
P2
P3
R1
P1
R2
R3
P1
P2
R2
R3
P2
R4
P3
Minimum Number of Total Resources to "Prevent" Deadlock
P
R
m
N
1
2
2
2
1
1
2
3
1
1
1
1
0
0
2
4
P = number of processes
R = number of resource types
m = max number allowed for each process
N = min total # resources needed to "prevent" deadlock
Dining Philosophers
P0
R4
R0
P4
P1
R1
R3
R2
P3
P2
Features of the Dining Philosopher Simulation
We will implement Havender's Linear Ordering (HLO) in Dining Philosophers
using Dekker's Algorithm in the Laboratory.
HLO can be implemented so that it is restarted in each thread (process) at the top of
its execution cycle (i.e. the while(!done) loop).
The only restriction is that the rules of HLO must be adhered to while a thread is
competing for shared resources. Otherwise, individual threads can "jump into" and
"jump out of" the HLO at any time.
Compare this with the effect on the Banker's Algorithm (deadlock avoidance by
Safe State detection), if the number of competing threads was to change during
resource management.
We will need to implement five separate Dekker's Algorithms, because two
philosophers will be competing for each of the five forks.
We will use random sleep intervals inside and outside each critical section to
simulate the passage of time.
Part of this task will be to implement cataloging of performance. At a minimum, we
will count the number of times that each philosopher eats.
Main Program
static void Main(string[] args)
{
Thread p0 = new Thread(new ThreadStart(P0));
Thread p1 = new Thread(new ThreadStart(P1));
Thread p2 = new Thread(new ThreadStart(P2));
Thread p3 = new Thread(new ThreadStart(P3));
Thread p4 = new Thread(new ThreadStart(P4));
p0.Start();
p1.Start();
p2.Start();
p3.Start();
p4.Start();
Thread.Sleep(20000);
done = true;
Thread.Sleep(1000);
Console.WriteLine("P0 P1 P2 P3 P4");
for (int i = 0; i < 5; i++)
Console.Write(count[i] + " ");
Console.ReadLine();
}
Global Parameters
public static bool done = false;
public static int[] count = new int[5] {0, 0, 0, 0, 0};
public static char[] status = new char[5] {'T','T','T','T','T'};
public
public
public
public
public
public
public
public
public
public
static
static
static
static
static
static
static
static
static
static
bool
bool
bool
bool
bool
bool
bool
bool
bool
bool
public
public
public
public
public
static
static
static
static
static
int
int
int
int
int
p0wantsf0
p0wantsf4
p1wantsf0
p1wantsf1
p2wantsf1
p2wantsf2
p3wantsf2
p3wantsf3
p4wantsf3
p4wantsf4
=
=
=
=
=
=
=
=
=
=
favoredforf0
favoredforf1
favoredforf2
favoredforf3
favoredforf4
false;
false;
false;
false;
false;
false;
false;
false;
false;
false;
=
=
=
=
=
0;
1;
2;
3;
4;
Philosopher 0
public static void P0()
{
System.Random rnd = new Random();
while (!done)
{
Thread.Sleep(rnd.Next(100));
status[0] = 'H'; // P0 is Hungry
p0wantsf0 = true;
favoredforf0 = 1;
while (p1wantsf0 && favoredforf0 == 1) ;
p0wantsf4 = true;
favoredforf4 = 4;
while (p4wantsf4 && favoredforf4 == 4) ;
status[0] = 'E'; // P0 is Eating
show_status();
Thread.Sleep(rnd.Next(100));
status[0] = 'T'; // P0 is Talking
count[0] += 1;
p0wantsf4 = false; // P0 puts down Forks
p0wantsf0 = false;
}
}
Sample Runs
P0
T
T
T
H
H
E
E
H
H
H
H
E
T
T
T
H
H
E
T
T
T
E
:
P1
T
E
E
T
T
H
H
H
E
E
T
T
H
H
E
H
H
H
H
E
E
T
:
P2
E
T
T
E
E
H
H
E
H
H
H
H
H
E
T
H
E
E
E
T
T
T
:
P3
T
H
E
T
T
H
E
T
H
H
E
E
T
T
T
E
H
H
H
H
E
E
:
P4
E
T
T
H
E
H
H
H
H
E
T
T
E
E
E
T
H
H
E
T
T
T
:.
:
E
E
T
T
E
E
H
H
H
H
H
H
E
E
T
T
T
E
T
H
H
H
E
:
T
T
E
E
H
H
H
H
H
E
E
T
T
T
E
E
E
T
H
E
E
T
T
:
H
E
T
T
H
H
H
E
T
T
H
H
H
E
T
T
T
H
E
T
T
T
T
:
E
T
H
H
H
E
T
T
E
E
T
E
E
H
H
H
E
E
T
H
H
E
E
:
T
T
E
E
T
T
E
E
T
T
E
T
T
T
H
E
T
T
H
H
E
T
T.
66
64
69
76
89
Summary
Restricting (or partitioning) resources can "prevent" deadlock while permitting all
four conditions for deadlock . . .Can you explain?
Resource allocation formula based on exceeding by a single resource (of any type)
the maximum number of resources (evenly distributed across the resource type) that
still permit deadlock to occur.
Implement Dining Philosophers using Havender's Linear Ordering (HLO) requires
the use of five critical sections.
We used an implementation of Dekker's Algorithm for enforcing mutual exclusion
for each fork.
We can ensure that mutual exclusion is enforced (for each fork) and that every
philosopher eventually gets a chance to eat.
How do we ensure that starvation will not occur?