Note that there are some explanatory texts on larger screens.

plurals
  1. POErlang - Dining Philosophers errors
    primarykey
    data
    text
    <p>I apologize if the code is hard to follow. This is the classic dining philosophers problem, where 5 philosophers are eating, but there are only 5 sticks - and you need two to eat. </p> <p>These are the instructions, if anyone is interested: <a href="http://www.kth.se/polopoly_fs/1.260940!/Menu/general/column-content/attachment/philosophers.pdf" rel="nofollow">http://www.kth.se/polopoly_fs/1.260940!/Menu/general/column-content/attachment/philosophers.pdf</a></p> <p>Anyways, here is the code, the chopstick process code:</p> <pre><code>-module(chopstick). -export([start/0]). start() -&gt; spawn_link(fun() -&gt; init() end). init() -&gt; available(). available() -&gt; receive {request, From} -&gt; From ! granted, gone(); quit -&gt; ok end. gone() -&gt; receive returned -&gt; available(); quit -&gt; ok end. </code></pre> <p>The philosopher process code:</p> <pre><code>-module(eater). -import(timer, [sleep/1]). -import(random, [uniform/1]). -export([start/5, dream/5, eat/5, wait/5]). start(Hungry, Right, Left, Name, Ctrl) -&gt; dream(Hungry, Right, Left, Name, Ctrl). **%This was wrong, it should say start(Hungry, Right, Left, Name, Ctrl) -&gt; spawn_link(fun() -&gt; dream(Hungry, Right, Left, Name, Ctrl) end).** dream(Hungry, Right, Left, Name, Ctrl) -&gt; Time = 500+uniform:random(500), **%This was wrong, it should say random:uniform** timer:sleep(Time), Right! {request, self()}, Left! {request, self()}, %skicka {request, self()} till två pinnar wait(Hungry, Right, Left, Name, Ctrl). wait(Hungry, Right, Left, Name, Ctrl) -&gt; receive granted -&gt; io:format("~s received a chopstick~n", [Name]), receive granted -&gt; io:format("~s received a chopstick~n", [Name]), io:format("~s started eating~n", [Name]), eat(Hungry, Right, Left, Name, Ctrl) end; _ -&gt; wait(Hungry, Right, Left, Name, Ctrl) end. eat(Hungry, Right, Left, Name, Ctrl) -&gt; Time = 500+uniform:random(500), **%This was wrong, it should say random:uniform** timer:sleep(Time), Right! returned, Left! returned, io:format("~s put back two chopsticks~n", [Name]), if Hungry =&lt; 1 -&gt; Ctrl ! done; true -&gt; dream(Hungry-1, Right, Left, Name, Ctrl) end. </code></pre> <p>And finally the host process:</p> <pre><code>-module(dinner). -export([start/0]). start() -&gt; spawn(fun() -&gt; init() end). init() -&gt; C1 = chopstick:start(), C2 = chopstick:start(), C3 = chopstick:start(), C4 = chopstick:start(), C5 = chopstick:start(), Ctrl = self(), eater:start(5, C1, C2, "Confucios", Ctrl), **% This is where it crashes** eater:start(5, C2, C3, "Avicenna", Ctrl), eater:start(5, C3, C4, "Plato", Ctrl), eater:start(5, C4, C5, "Kant", Ctrl), eater:start(5, C5, C1, "Descartes", Ctrl), wait(5, [C1, C2, C3, C4, C5]). wait(0, Chopsticks) -&gt; lists:foreach(fun(C) -&gt; C ! quit end, Chopsticks); wait(N, Chopsticks) -&gt; receive done -&gt; wait(N-1, Chopsticks); abort -&gt; erlang:exit(abort) end. </code></pre> <p>Output:</p> <pre><code>11&gt; dinner:start(). &lt;0.85.0&gt; 12&gt; =ERROR REPORT==== 10-Nov-2011::02:19:10 === Error in process &lt;0.85.0&gt; with exit value: {undef,[{uniform,random,[500]}, {eater,dream,5},{dinner,init,0}]} </code></pre> <p>Thanks a lot if you even read through all of this, I haven't learnt how to read the error reports of erlang yet. If you can, and want to tell me what it means please do.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload