<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7462074687319972038</id><updated>2012-02-16T01:44:10.692-08:00</updated><title type='text'>TechNotes-DJG</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://technotes-djg.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7462074687319972038/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://technotes-djg.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>TechNotes-DJ-Greaves</name><uri>http://www.blogger.com/profile/11362617270714000439</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7462074687319972038.post-7811263587927401349</id><published>2012-02-02T06:25:00.000-08:00</published><updated>2012-02-02T06:25:49.274-08:00</updated><title type='text'>Dining Philosophers Deadlock Example Using Bluespec</title><content type='html'>The &lt;a href="http://en.wikipedia.org/wiki/Dining_philosophers_problem" target="_blank"&gt;dining philosophers&lt;/a&gt; were invented in 1965 by &lt;a href="http://en.wikipedia.org/wiki/Edsger_W._Dijkstra" title="Edsger W. Dijkstra"&gt;Edsger Dijkstra&lt;/a&gt; and the problem they pose has become the classical example of a system that is liable to deadlock.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Five philosophers are evenly spaced around a round table. &amp;nbsp; Each has a bowl of soup but there are only five spoons and each needs (for some strange reason) two spoons to eat.&amp;nbsp; So they cannot all eat at once, but must take it in turns.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In &lt;a href="http://www.bluespec.com/" target="_blank"&gt;Bluespec Verilog&lt;/a&gt;, a spoon is easily coded as follows. The pickup method is blocking, so any philosopher attempting to pickup a fork that is already held by his neighbour will wait until his neighbour puts it down.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;interface Spoon_if;&lt;br /&gt;  method Action pickup();&lt;br /&gt;  method Action putdown();&lt;br /&gt;endinterface&lt;br /&gt;&lt;br /&gt;module spoon (Spoon_if) ;&lt;br /&gt;&lt;br /&gt;  Reg#(Bool) inuse &amp;lt;- mkReg(?);&lt;br /&gt;&lt;br /&gt;  method Action pickup() if (!inuse);&lt;br /&gt;   action&lt;br /&gt;     inuse &amp;lt;= True;&lt;br /&gt;   endaction&lt;br /&gt;  endmethod&lt;br /&gt;&lt;br /&gt;  method Action putdown();&lt;br /&gt;   action&lt;br /&gt;     inuse &amp;lt;= False;&lt;br /&gt;   endaction&lt;br /&gt;  endmethod&lt;br /&gt;&lt;br /&gt;endmodule&amp;nbsp;&lt;/pre&gt;&lt;br /&gt;The complete system can be described as follows:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;(*synthesize *)&lt;br /&gt;module philoBENCH (Empty) ;&lt;br /&gt;&lt;br /&gt;       Spoon_if spoon0 &amp;lt;- spoon;&lt;br /&gt;       Spoon_if spoon1 &amp;lt;- spoon;&lt;br /&gt;       Spoon_if spoon2 &amp;lt;- spoon;&lt;br /&gt;       Spoon_if spoon3 &amp;lt;- spoon;&lt;br /&gt;       Spoon_if spoon4 &amp;lt;- spoon;&lt;br /&gt;&lt;br /&gt;       Diner_if din0 &amp;lt;- mkDiner (3, 7, spoon0, spoon1);&lt;br /&gt;       Diner_if din1 &amp;lt;- mkDiner (4, 4, spoon1, spoon2);&lt;br /&gt;       Diner_if din2 &amp;lt;- mkDiner (2, 9, spoon2, spoon3);&lt;br /&gt;       Diner_if din3 &amp;lt;- mkDiner (3, 6, spoon3, spoon4);&lt;br /&gt;       Diner_if din4 &amp;lt;- mkDiner (3, 6, spoon4, spoon0);&lt;br /&gt;&lt;br /&gt;endmodule: philoBENCH&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;And the behaviour of an individual philosopher can be coded as follows:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;module mkDiner #(UInt#(15) on, UInt#(15) seed) (Spoon_if left, Spoon_if right, Diner_if i);&lt;br /&gt;&lt;br /&gt;  Reg#(Bool) eating &amp;lt;- mkReg(?);&lt;br /&gt;  Reg#(UInt#(15)) timer &amp;lt;- mkReg(0);&lt;br /&gt;&lt;br /&gt;  Random_if gen &amp;lt;- mkRandom_gen(seed);&lt;br /&gt;&lt;br /&gt;  rule foo (timer != 0);&lt;br /&gt;     timer &amp;lt;= timer - 1;&lt;br /&gt;  endrule&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  Stmt seq_behaviour =&lt;br /&gt;  (seq while (True) seq&lt;br /&gt;     action UInt#(15) x &amp;lt;- gen.gen(); timer&amp;lt;=x &amp;amp; 31; endaction await(timer== 0);&lt;br /&gt;&lt;br /&gt;     left.pickup();&lt;br /&gt;     noAction;&lt;br /&gt;&lt;br /&gt;     action UInt#(15) x &amp;lt;- gen.gen(); timer&amp;lt;=x &amp;amp; 31; endaction await(timer== 0);&lt;br /&gt;&lt;br /&gt;     right.pickup();&lt;br /&gt;&lt;br /&gt;     eating &amp;lt;= True;&lt;br /&gt;     timer &amp;lt;= on; await(timer==0);&lt;br /&gt;     eating &amp;lt;= False;&lt;br /&gt;&lt;br /&gt;     noAction;&lt;br /&gt;&lt;br /&gt;     left.putdown();&lt;br /&gt;     noAction;&lt;br /&gt;     right.putdown();&lt;br /&gt;     noAction;&lt;br /&gt;  endseq endseq);&lt;br /&gt;&lt;br /&gt;  FSM fsm &amp;lt;- mkFSM (seq_behaviour);&lt;br /&gt;&lt;br /&gt;  rule kickoff  ;&lt;br /&gt;     fsm.start;&lt;br /&gt;  endrule&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;endmodule&lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;&lt;br /&gt;&lt;span style="color: magenta;"&gt;What happens ?&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;Not suprisingly, after a little while the system reaches deadlock, as shown in this waveform trace:&lt;br /&gt;&lt;pre&gt;&lt;/pre&gt;&lt;blockquote class="tr_bq"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-hIZnczsX0pw/TyqVvf-GsSI/AAAAAAAAAAM/wGuG5tVHyrU/s1600/deadlocking-dining-phiosophers-bluespec.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="351" src="http://3.bp.blogspot.com/-hIZnczsX0pw/TyqVvf-GsSI/AAAAAAAAAAM/wGuG5tVHyrU/s400/deadlocking-dining-phiosophers-bluespec.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;br /&gt;The obvious source of deadlock is that each philosopher first picks up his left fork and then tries for his right.&amp;nbsp; This is clearly a bad policy: in concurrency terms, each spoon is a lock and the resulting cyclic ring of dependencies is known as a `knot' in the lock graph.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: magenta;"&gt;The Solution ?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There are many solutions the problem:&amp;nbsp; one that appears to work is for one philosopher to first pick up his right fork.&amp;nbsp; We change the system configuration as follows:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;       Diner_if din0 &amp;lt;- mkDiner (3, 7, spoon0, spoon1);&lt;br /&gt;       Diner_if din1 &amp;lt;- mkDiner (4, 4, spoon1, spoon2);&lt;br /&gt;       Diner_if din2 &amp;lt;- mkDiner (2, 9, spoon3, spoon2); // &amp;lt;-- Spoon swap!&lt;br /&gt;       Diner_if din3 &amp;lt;- mkDiner (3, 6, spoon3, spoon4);&lt;br /&gt;       Diner_if din4 &amp;lt;- mkDiner (3, 6, spoon4, spoon0);&lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;Now when we run the system, it does not seem to lock up however long we simulate for.  Here's some example output:&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-nyDILW2mRIc/TyqVzwtoa-I/AAAAAAAAAAU/sw4KGcq9zE0/s1600/nondeadlocking-dining-phiosophers-bluespec.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="337" src="http://1.bp.blogspot.com/-nyDILW2mRIc/TyqVzwtoa-I/AAAAAAAAAAU/sw4KGcq9zE0/s400/nondeadlocking-dining-phiosophers-bluespec.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: magenta;"&gt;A good and permanent fix ?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;But can we be sure this system is now deadlock free?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7462074687319972038-7811263587927401349?l=technotes-djg.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://technotes-djg.blogspot.com/feeds/7811263587927401349/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://technotes-djg.blogspot.com/2012/02/dining-philosophers-deadlock-example.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7462074687319972038/posts/default/7811263587927401349'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7462074687319972038/posts/default/7811263587927401349'/><link rel='alternate' type='text/html' href='http://technotes-djg.blogspot.com/2012/02/dining-philosophers-deadlock-example.html' title='Dining Philosophers Deadlock Example Using Bluespec'/><author><name>TechNotes-DJ-Greaves</name><uri>http://www.blogger.com/profile/11362617270714000439</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-hIZnczsX0pw/TyqVvf-GsSI/AAAAAAAAAAM/wGuG5tVHyrU/s72-c/deadlocking-dining-phiosophers-bluespec.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7462074687319972038.post-4632606202029493230</id><published>2011-11-10T02:11:00.000-08:00</published><updated>2011-11-10T02:20:45.699-08:00</updated><title type='text'>TNDJG:0001 TechNotes-DJG Established</title><content type='html'>Hello World!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So, it's time I stopped abusing my home page at &lt;a href="http://www.cl.cam.ac.uk/users/djg11"&gt;www.cl.cam.ac.uk/users/djg11&lt;/a&gt; with all sorts of random technical articles and tried out a proper bloggsite.&amp;nbsp; &lt;br /&gt;&lt;br /&gt;I have three or four recent notes to upload straight away, ranging from washing machine repair to structural hazards in BlueSpec Verilog...&lt;br /&gt;&lt;br /&gt;DJG&lt;br /&gt;/*&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7462074687319972038-4632606202029493230?l=technotes-djg.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://technotes-djg.blogspot.com/feeds/4632606202029493230/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://technotes-djg.blogspot.com/2011/11/tndjg0001-technotes-djg-established.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7462074687319972038/posts/default/4632606202029493230'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7462074687319972038/posts/default/4632606202029493230'/><link rel='alternate' type='text/html' href='http://technotes-djg.blogspot.com/2011/11/tndjg0001-technotes-djg-established.html' title='TNDJG:0001 TechNotes-DJG Established'/><author><name>TechNotes-DJ-Greaves</name><uri>http://www.blogger.com/profile/11362617270714000439</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
