1 / 12

Lecture 8

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

shawn
Download Presentation

Lecture 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. 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. R1 R2 P1 P2 P3

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

  4. 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. R1 R2 R1 R2 R3 m=1 m=2 : P1 P2 P1 P2 R1 R2 R3 R1 R2 R3 R4 P1 P2 P1 P2 P3 P3

  5. PRmN Minimum Number of Total Resources to "Prevent" Deadlock 1 1 1 0 2 1 1 0 2 2 1 2 2 3 1 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

  6. P0 R4 R0 P4 P1 R1 R3 R2 P3 P2 Dining Philosophers

  7. 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.

  8. Main Program staticvoid Main(string[] args) { Thread p0 = newThread(newThreadStart(P0)); Thread p1 = newThread(newThreadStart(P1)); Thread p2 = newThread(newThreadStart(P2)); Thread p3 = newThread(newThreadStart(P3)); Thread p4 = newThread(newThreadStart(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(); }

  9. Global Parameters publicstaticbool done = false; publicstaticint[] count = newint[5] {0, 0, 0, 0, 0}; publicstaticchar[] status = newchar[5] {'T','T','T','T','T'}; publicstaticbool p0wantsf0 = false; publicstaticbool p0wantsf4 = false; publicstaticbool p1wantsf0 = false; publicstaticbool p1wantsf1 = false; publicstaticbool p2wantsf1 = false; publicstaticbool p2wantsf2 = false; publicstaticbool p3wantsf2 = false; publicstaticbool p3wantsf3 = false; publicstaticbool p4wantsf3 = false; publicstaticbool p4wantsf4 = false; publicstaticint favoredforf0 = 0; publicstaticint favoredforf1 = 1; publicstaticint favoredforf2 = 2; publicstaticint favoredforf3 = 3; publicstaticint favoredforf4 = 4;

  10. Philosopher 0 publicstaticvoid P0() { System.Random rnd = newRandom(); 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; } }

  11. Sample Runs : : : : : ETHET ETET T TETHE TETHE EH H HT EH HET H H HTE H HETE H HTET HETET HEHTE HTHET ETHET ETEHT TETH H TETHE TETET ETHET THETH HETH H HETHE HT TET ET TET. 66 64 69 76 89 P0 P1 P2 P3 P4 T TETE TETHT TETET HTETH HTETE EH H H H EH HEH H HETH HEH H H HEH HE HTHET ETHET TH HTE THE TE TETTE H H HET H HEH H EHEH H THEHE TETHT TETET ETTET : : : : :.

  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?

More Related