Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 8 Restrictions on Resource Allocation Dining Philosophers - Havender's Linear Ordering.

Similar presentations


Presentation on theme: "Lecture 8 Restrictions on Resource Allocation Dining Philosophers - Havender's Linear Ordering."— Presentation transcript:

1 Lecture 8 Restrictions on Resource Allocation Dining Philosophers - Havender's Linear Ordering

2 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 R2 P1P2 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.

3 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 R3 P1P2 R2 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).

4 R1R3 P1P2 R2R1 P1P2 R2 R1R3 P1P2 R2 P3 R1R3 P1P2 R2 P3 R4 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 :

5 Minimum Number of Total Resources to "Prevent" Deadlock P R m NP R m N P = number of processes R = number of resource types m = max number allowed for each process N = min total # resources needed to "prevent" deadlock

6 P0P0 P1P1 P2P2 P3P3 P4P4 R0R0 R4R4 R1R1 R3R3 R2R2 Dining Philosophers

7 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. Features of the Dining Philosopher Simulation

8 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(); } Main Program

9 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 static bool p0wantsf0 = false; public static bool p0wantsf4 = false; public static bool p1wantsf0 = false; public static bool p1wantsf1 = false; public static bool p2wantsf1 = false; public static bool p2wantsf2 = false; public static bool p3wantsf2 = false; public static bool p3wantsf3 = false; public static bool p4wantsf3 = false; public static bool p4wantsf4 = false; public static int favoredforf0 = 0; public static int favoredforf1 = 1; public static int favoredforf2 = 2; public static int favoredforf3 = 3; public static int favoredforf4 = 4; Global Parameters

10 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; } Philosopher 0

11 P0 P1 P2 P3 P4 T T E T E T E T H T T E T E T H T E T H H T E T E E H H H H E H H E H H H E T H H E H H H H E H H E H T H E T E T H E T T H H T E T H E T E T E T T E H H H E T H H E H H E H E H H T H E H E T E T H T T E T E T E T T E T : : : : :. Sample Runs : : : : : E T H E T E T E T T T E T H E E H H H T E H H E T H H H T E H H E T E H H T E T H E T E T H E H T E H T H E T E T H E T E T E H T T E T H H T E T H E T E T E T E T H E T T H E T H H E T H H H E T H E H T T E T E T T E T

12 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?


Download ppt "Lecture 8 Restrictions on Resource Allocation Dining Philosophers - Havender's Linear Ordering."

Similar presentations


Ads by Google