Deadlock CDS News: Brainy IBM Chip Packs One Million Neuron Punch Overview: ideas, 4 four necessary and sufficient conditions deadlock analysis - primitive processes, parallel composition, avoidance the Dining Philosophers problem model, structure diagram, analysis implementation: class diagram, fork monitor, philosopher threads deadlock free version solving the maze problem by deadlock analysis (these slides including graphics are a modified version of the slides from Ch6 of Magee & Kramer: Concurrency) COMP2310 Lecture 10: Deadlock 2014 1
Deadlock and its Avoidance ideas for deadlock: concepts: system deadlock: no further progress model: deadlock: no eligible actions practice: blocked threads our aim: deadlock avoidance: to design systems where deadlock cannot occur done by removing one of the four necessary and sufficient conditions: serially reusable resources: the processes involved share resources which they use under mutual exclusion incremental acquisition: processes hold on to resources already allocated to them while waiting to acquire additional resources no pre-emption: once acquired by a process, resources cannot be pre-empted (forcibly withdrawn) but are only released voluntarily. wait-for cycle: a circular chain (or cycle) of processes exists such that each process holds a resource which its successor in the cycle is waiting to acquire COMP2310 Lecture 10: Deadlock 2014 2
Wait-for Cycles operating systems must deal with deadlock arising from processes requesting resources (e.g. printers, memory, co-processors) pre-emption involves constructing a resource allocation graph, detecting a cycle and removal of a resource avoidance involves never granting a request that could lead to such a cycle (the Banker s Algorithm) COMP2310 Lecture 10: Deadlock 2014 3
Deadlock analysis: Primitive Processes (within a process) a deadlocked state is one with no outgoing transitions in FSP, ËÌÇÈ the process ÅÇÎ ÒÓÖØ e.g. ÓÙØ > ÅÇÎ > ÒÓÖØ ËÌÇȵµº > we can use animation to produce a trace analysis using LTSA: finds the shortest trace to ËÌÇÈ: Trace to DEADLOCK: north north doesn t it take two to deadlock? COMP2310 Lecture 10: Deadlock 2014 4
Deadlock Analysis: Parallel Composition in systems, deadlock may arise from the parallel composition of interacting processes Ø > ÔÙØ > Ê ËÇÍÊ µº Ê ËÇÍÊ ÔÖ ÒØ Ö º Ø > ÒÒ Ö º Ø > È > ÓÔÝ º ÔÙØ > ÒÒ Ö º ÔÙØ > È µº ÔÖ ÒØ Ö ÒÒ Ö º Ø > ÔÖ ÒØ Ö º Ø > É > ÓÔÝ ÔÖ ÒØ Ö º ÔÙØ > ÒÒ Ö º ÔÙØ > É µº Deadlock Trace? Avoidance? Ë Ë Ô È Õ É { Ô Õ ÔÖ ÒØ Ö Ê ËÇÍÊ { Ô Õ ÒÒ Ö Ê ËÇÍÊ µº Q1: if we rewrote É ÔÖ ÒØ Öº Ø > ÒÒ Öº Ø > ºººµ, would we get deadlock? COMP2310 Lecture 10: Deadlock 2014 5
Deadlock Analysis: Avoidance acquire the resources in the same order? (in practice?) provide a timeout: Ê ËÇÍÊ Ø > ÔÙØ > Ê ËÇÍÊ µº ÔÖ ÒØ Ö º Ø > ÌË ÆÆ Ê µ È ÒÒ Ö º Ø > ÓÔÝ > ÌË ÆÆ Ê º ÔÙØ > ÒÒ Ö º ÔÙØ > È ÔÖ ÒØ Ö Ø Ñ ÓÙØ ÔÖ ÒØ Ö º ÔÙØ > È > µº ÒÒ Ö º Ø > ÌÈÊÁÆÌ Ê µ É ÔÖ ÒØ Ö º Ø > ÓÔÝ > ÌÈÊÁÆÌ Ê º ÔÙØ > ÒÒ Ö º ÔÙØ > É ÔÖ ÒØ Ö Ø Ñ ÓÙØ ÒÒ Ö º ÔÙØ > É > Q2: given µº µº ɽ ½ >½ >ɽ Ƚ ½ >½ >Ƚ ¾ >¾ >ɽ ¾ >¾ >Ƚ µº ɾ ½ > ½ >ɾ ¾ > ¾ >ɾ µº Ⱦ ½ > ½ >Ⱦ ¾ > ¾ >Ⱦ µº which of the following compositions can deadlock? (a) Ƚ ɽµ (b) Ƚ ɾµ (c) Ⱦ ɽµ (d) Ⱦ ɾµ COMP2310 Lecture 10: Deadlock 2014 6
The Dining Philosophers Problem Five philosophers sit around a circular table. Each philosopher spends his life alternately thinking and eating. In the centre of the table is a large bowl of spaghetti. A philosopher needs two forks to eat a helping of spaghetti. One fork is placed between each pair of philosophers and they agree that each will only use the fork to his immediate right and left COMP2310 Lecture 10: Deadlock 2014 7
Dining Philosophers: Model Structure Diagram Each ÇÊÃ is a shared resource with actions Ø and ÔÙØ. When hungry, each ÈÀÁÄ must first get his right and left forks before he can start eating. Each ÇÊÃ is either in a taken or not taken state. This is an example when the resulting monitor state can be distributed (resulting in greater system concurrency), COMP2310 Lecture 10: Deadlock 2014 8
Dining Philosophers: Model & Deadlock Analysis Ø >»» Ø Ò ÇÊà > ÇÊà µº»» ÒÓØ Ø Ò ÔÙØ ÈÀÁÄ Ø ÓÛÒ > Ö Ø º Ø > Ð Ø º Ø > Ø > Ð Ø º ÔÙØ > Ö Ø º ÔÙØ > > ÈÀÁÄ µº Ö Table of philosophers: ÁÆ ÊË Æ µ ¼ººÆ ½ ÓÖ ÐÐ Ô Ð ÈÀÁÄ Ô Ð º Ð Ø Ô Ð ½µ Æ µ± Æ º Ö Ø ÇÊà µº { Deadlock analysis by LTSAtool: Trace to DEADLOCK: phil.0.sitdown phil.0.right.get phil.1.sitdown phil.1.right.get phil.2.sitdown phil.2.right.get phil.3.sitdown phil.3.right.get phil.4.sitdown phil.4.right.get This is the situation where all the philosophers become hungry at the same time, sit down at the table and each philosopher picks up the fork to his right. The system can make no further progress since each philosopher is waiting for a fork held by his neighbor, i.e. a wait-for cycle exists! COMP2310 Lecture 10: Deadlock 2014 9
Dining Philosophers: Implementation in Java philosophers: active entities implement as threads forks: shared passive entities implement as monitors display COMP2310 Lecture 10: Deadlock 2014 10
Dining Philosophers: the Fork Monitor Full code in Fork.java ÓÓÐ Ò Ø Ò Ð ÔÖ Ú Ø È Ð ÒÚ ÔÐ Ý ÔÖ Ú Ø Ø Ò encodes the state of the fork ÔÖ Ú Ø ÒØ È Ð ÒÚ Ô ÒØ ÒØ µ{ ÓÖ Ô ÒØ ÔÐ Ý ÝÒ ÖÓÒ Þ ÚÓ ÔÙØ µ { Ø > ÔÙØ > ÇÊõº ÇÊà We could ÇÊà rewrite as: ÇÊà ¼ ÇÊÃ Ø ¼ºº½ ÇÊÃ Ð Ø Ò º Ø ÓÖ Ø Ò µ ÔÐ Ý ÒÓØ Ý µ Û Ò Ø ¼µ Ø > ÇÊà ½ Û Ò Ø ½µ ÔÙØ ÇÊà ¼ > µº ÚÓ Ø µ ÝÒ ÖÓÒ Þ Ú º Ð Ò º ÁÒØ ººº Ü ÔØ ÓÒ{ Ø ÖÓÛ Ø Ò µ Û Ø µ Û Ð ØÖÙ Ø Ò ÔÐ Ý º Ø ÓÖ Ø Ò µ Q3: why isn t there a Ø Òµ Û Ø µ Û Ð loop in ÔÙØ µ? COMP2310 Lecture 10: Deadlock 2014 11
Dining Philosophers: Philosopher Implementation Follows from the ÈÀÁÄÇË model, sitting down and arising actions omitted (full code): Ð È ÐÓ ÓÔ Ö ÜØ Ò Ì Ö { ÓÖ Ð Ø Ö Ø ÒØ ººº ÔÖ Ú Ø Ò Ö ØÖ ÒØ ÒØ ÓÖ Ð ÓÖ Öµ { ººº È ÐÓ ÓÔ Ö ÔÙ Ð ÚÓ ÖÙÒ µ { {»» ØÖÝ ØÖÙ µ {»» Ø Ò Ò Û Ð º ØÈ Ð Ú Û º ÌÀÁÆÃÁÆ µ Ú Û ÓÒØÖÓÐÐ Ö º Ð ÔÌ Ñ µµ»» ÙÒ ÖÝ Ð Ô º ØÈ Ð Ú Û º ÀÍÆ Ê µ Ú Û º Ø µ»» ÓØ Ö Ø ÓÖ Ö Ø º ØÈ Ð Ú Û º ÇÌÊÁ ÀÌ µ Ú Û ¼¼µ»» ÓØ Ð Ø ÓÖ Ð Ô º Ø µ»» Ø Ò Ð Ø º ØÈ Ð Ú Û º ÌÁÆ µ Ú Û ÓÒØÖÓÐÐ Ö º ØÌ Ñ µµ Ð Ô º ÔÙØ µ Ö Ø º ÔÙØ µ»» Ø Ò Ò Ð Ø Ø Ú º Ð Ò º ÁÒØ ÖÖÙÔØ Ü ÔØ ÓÒ µ { COMP2310 Lecture 10: Deadlock 2014 12
model: Dining Philosophers: Implementation of Startup Ô Ð ÈÀÁÄ {Ô Ð ºÐ Ø Ô Ð ½ ºÖ Ø ÇÊõ ÓÖ ÐÐ ¼ººÆ ½ (full code is in Diners.java) Ð Ò Ö ÜØ Ò ÔÔÐ Ø { ÔÙ Ð Ô Ð Ò Û Ì Ö ÆÍÅÈÀÁÄË Ì Ö ÓÖ Ò Û ÓÖ ÆÍÅÈÀÁÄË ÓÖ ººº ÚÓ Ø ÖØ µ { ÔÙ Ð ÒØ ¼ < ÆÍÅÈÀÁÄË µ ÓÖ Ò Û ÓÖ ÔÐ Ý µ ÓÖ ÒØ ¼ < ÆÍÅÈÀÁÄË µ { ÓÖ Ð ØÁ ½ ÆÍÅÈÀÁÄË µ ± ÆÍÅÈÀÁÄË»» Ö ØÁ ÒØ È ÐÓ ÓÔ Ö Ø ÓÖ Ð ØÁ ÓÖ µ Ô Ð º Ø ÖØ µ Ô Ð Q: why are there two loops? It was easy to detect deadlock in our model. How would we detect it in our implementation? COMP2310 Lecture 10: Deadlock 2014 13
Dining Philosophers: Implementation Exposing Deadlock To ensure deadlock occurs eventually, the applet s slider control may be moved to the left. This reduces the time each philosopher spends thinking and eating. This speedup increases the probability of deadlock occurring. COMP2310 Lecture 10: Deadlock 2014 14
Deadlock-free Philosophers Deadlock can be avoided by ensuring that a wait-for cycle cannot exist. How? introduce an asymmetry into our definition of philosophers: Use the Á id of a philosopher to make even numbered philosophers get their left forks first, odd their right first. other strategies? ÈÀÁÄ Á ¼µ Û Ò Á ±¾ ¼µ Ø ÓÛÒ > Ð Ø º Ø > Ö Ø º Ø > Ø > Ð Ø º ÔÙØ > Ö Ø º ÔÙØ > Ö > ÈÀÁÄ Û Ò Á ±¾ ½µ Ø ÓÛÒ > Ö Ø º Ø > Ð Ø º Ø > Ø > Ð Ø º ÔÙØ > Ö Ø º ÔÙØ > Ö > ÈÀÁÄ µº Q4: the minimum number of philosophers for a Dining Philosophers to have deadlock is: (a) 1 (b) 2 (c) 3 (d) 4 (e) 5 COMP2310 Lecture 10: Deadlock 2014 15
Maze Example Solution via Shortest path to Deadlock We can exploit the shortest path to deadlock trace of the LTSA to find the shortest path out of a maze to the ËÌÇÈ process! We first model the maze. Each position is modelled by the moves that it permits. The Å parameter gives the starting position ËØ ÖØ µ È ËØ ÖØ Å ¼ ÒÓÖØ > ËÌÇÈ Ø > È ½ µ È ººº ÒÓÖØ > È Û Ø > È µº È ÌÇÍÌ Å µº Shortest path to escape trace from 7: trace to deadlock: Ø ÒÓÖØ ÒÓÖØ Û Ø Û Ø ÒÓÖØ COMP2310 Lecture 10: Deadlock 2014 16
Summary concepts deadlock: no further progress four necessary and sufficient conditions serially reusable resources incremental acquisition no preemption wait-for cycle models no eligible actions (analysis gives shortest path trace) practice: blocked threads our aim: to design systems where deadlock cannot occur! COMP2310 Lecture 10: Deadlock 2014 17