Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6793407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:05:48+00:00 2026-05-26T18:05:48+00:00

I apologize if the code is hard to follow. This is the classic dining

  • 0

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.

These are the instructions, if anyone is interested:
http://www.kth.se/polopoly_fs/1.260940!/Menu/general/column-content/attachment/philosophers.pdf

Anyways, here is the code, the chopstick process code:

-module(chopstick).
-export([start/0]).

start() ->
spawn_link(fun() -> init() end).
init() ->
available().

available() ->
receive
    {request, From} -> 
        From ! granted,
        gone();
    quit ->
        ok
        end.
gone() ->
receive
    returned ->
        available();
    quit ->
        ok
        end.

The philosopher process 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) ->
dream(Hungry, Right, Left, Name, Ctrl).

 **%This was wrong, it should say start(Hungry, Right, Left, Name, Ctrl) ->
 spawn_link(fun() -> dream(Hungry, Right, Left, Name, Ctrl) end).**

dream(Hungry, Right, Left, Name, Ctrl) -> 
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) ->
receive
    granted ->  
        io:format("~s received a chopstick~n", [Name]),
        receive
            granted ->
            io:format("~s received a chopstick~n", [Name]),
            io:format("~s started eating~n", [Name]),
            eat(Hungry, Right, Left, Name, Ctrl)
            end;
    _ -> wait(Hungry, Right, Left, Name, Ctrl)
end.

eat(Hungry, Right, Left, Name, Ctrl) ->
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 =< 1 ->
        Ctrl ! done;
    true ->
        dream(Hungry-1, Right, Left, Name, Ctrl)
end.    

And finally the host process:

-module(dinner).
-export([start/0]).


start() ->
spawn(fun() -> init() end).

init() ->
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) ->
lists:foreach(fun(C) -> C ! quit end, Chopsticks);
wait(N, Chopsticks) ->
receive
    done ->
        wait(N-1, Chopsticks);
    abort ->
        erlang:exit(abort)
end.

Output:

11> dinner:start().
<0.85.0>
12> 
=ERROR REPORT==== 10-Nov-2011::02:19:10 ===
 Error in process <0.85.0> with exit value: {undef,[{uniform,random,[500]}, {eater,dream,5},{dinner,init,0}]}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:05:49+00:00Added an answer on May 26, 2026 at 6:05 pm

    I think the problem is that you’ve got three modules: dinner, eater, and chopstick, but try to call philospher:start in your dinner:init/0 function. Try eater:start instead.

    The second problem is the order of module and function name when generating random numbers; replace uniform:random with random:uniform in your eater.erl:

    1> dinner:start().
    <0.35.0>
    Confucios received a chopstick
    Confucios received a chopstick
    Confucios started eating
    Confucios put back two chopsticks
    Confucios received a chopstick
    Confucios received a chopstick
    Confucios started eating
    Confucios put back two chopsticks
    Confucios received a chopstick
    Confucios received a chopstick
    Confucios started eating
    Confucios put back two chopsticks
    Confucios received a chopstick
    Confucios received a chopstick
    Confucios started eating
    Confucios put back two chopsticks
    Confucios received a chopstick
    Confucios received a chopstick
    Confucios started eating
    Confucios put back two chopsticks
    Avicenna received a chopstick
    Avicenna received a chopstick
    Avicenna started eating
    ...
    

    This pretty quickly shows the third problem — something we should have spotted from the first error report — that the eaters aren’t actually in their own processes. So edit eater.erl so that the start() function reads:

    start(Hungry, Right, Left, Name, Ctrl) ->
        spawn_link(fun() -> dream(Hungry, Right, Left, Name, Ctrl) end).
    

    Now it works as intended:

    1> dinner:start().
    <0.35.0>
    Confucios received a chopstick
    Plato received a chopstick
    Confucios received a chopstick
    Confucios started eating
    Descartes received a chopstick
    Kant received a chopstick
    Confucios put back two chopsticks
    Avicenna received a chopstick
    ...
    

    Thanks. This was good fun.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try hard to find the problem in this Java code, but I can't
I apologize for making my first question not the hard-hitting code-related question I was
I apologize if this code looks a bit like a mess (considering the length);
I apologize for the subjectiveness of this question, but I am a little stuck
First I apologize for a somewhat longer sample code and question... There are three
I apologize if this is a duplicate post, but I couldn't find the answer
I've written code to restore the state of my app, but there's a memory
I apologize if this has already been asked a different way but I couldn't
I apologize ahead of time if this code isn't formatted correctly, trying to paste
I apologize if this was asked somewhere else, but I do not know how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.