fork(1) download
  1. -module(dinphil).
  2. -compile([export_all]).
  3.  
  4. college() ->
  5. %%Spawns all the Report, forks and philosophers
  6. Report = spawn(?MODULE, report, []),
  7.  
  8. F1 = spawn(?MODULE, initFork, ["Fork1", on_table, Report]),
  9. F2 = spawn(?MODULE, initFork, ["Fork2", on_table, Report]),
  10. F3 = spawn(?MODULE, initFork, ["Fork3", on_table, Report]),
  11. F4 = spawn(?MODULE, initFork, ["Fork4", on_table, Report]),
  12. F5 = spawn(?MODULE, initFork, ["Fork5", on_table, Report]),
  13.  
  14. Philo1 = spawn(?MODULE, initPhil, ["Bob", F1, F2, Report]),
  15. Philo2 = spawn(?MODULE, initPhil, ["Jim", F2, F3, Report]),
  16. Philo3 = spawn(?MODULE, initPhil, ["Tim", F3, F4, Report]),
  17. Philo4 = spawn(?MODULE, initPhil, ["Olly", F4, F5, Report]),
  18. Philo5 = spawn(?MODULE, initPhil, ["Ted", F5, F1, Report]),
  19. waitdone(5).
  20.  
  21. report() ->
  22. receive
  23. {fork, X} ->
  24. io:format ("~s~n", [X]);
  25. {taken_fork, X} ->
  26. io:format ("~s~n", [X]);
  27. {thinking, X} ->
  28. io:format ("~s~n", [X]);
  29. {hungry, X} ->
  30. io:format ("~s~n", [X]);
  31. {msg, X} ->
  32. io:format ("~s~n", [X]);
  33. Any ->
  34. io:format ("XX~s~n", [Any])
  35. end,
  36. report().
  37.  
  38. initFork(Name, Isavail, Report) ->
  39. Report ! {fork, string:concat(Name, ": has been spawned")},
  40. fork(Name, on_table, Report).
  41.  
  42. fork(Name, on_table, Report) ->
  43. receive
  44. {take_left, {Pidphil, Philname}} ->
  45. Pidphil ! {l_fork, self()},
  46. Report ! {taken_fork, string:concat(Philname, ": picked up a fork")},
  47. Report ! {fork, string:concat(Name, ": Is picked up")},
  48. fork(Name, in_hand, Report);
  49.  
  50. {take_right, {Pidphil, Philname}} ->
  51. Pidphil ! {r_fork, self()},
  52. Report ! {taken_fork, string:concat(Philname, ": picked up a fork")},
  53. Report ! {fork, string:concat(Name, ": Is picked up")},
  54. fork(Name, in_hand, Report)
  55. end;
  56.  
  57. fork(Name, in_hand, Report) ->
  58. receive
  59. {put_left, {Pidphil, Philname}} ->
  60. Pidphil ! {l_fork, self()},
  61. Report ! {taken_fork, string:concat(Philname, ": put down a fork")},
  62. Report ! {fork, string:concat(Name, ": Is put down")},
  63. fork(Name, on_table, Report);
  64.  
  65. {put_right, {Pidphil, Philname}} ->
  66. Pidphil ! {r_fork, self()},
  67. Report ! {taken_fork, string:concat(Philname, ": put down a fork")},
  68. Report ! {fork, string:concat(Name, ": Is put down")},
  69. fork(Name, on_table, Report)
  70. end.
  71.  
  72. initPhil(Name, LeftFork, RightFork, Report) ->
  73. Seed = random:seed(now()),
  74. philosopher(thinking, Name, LeftFork, RightFork, Report).
  75.  
  76. philosopher(thinking, Name, LFork, RFork, Report) ->
  77. Report ! {thinking, string:concat(Name, ": thinking")},
  78. timer:sleep(random:uniform(901)+99),
  79. philosopher(hungry, Name, LFork, RFork, Report);
  80.  
  81. philosopher(hungry, Name, LFork, RFork, Report) ->
  82. Report ! {hungry, string:concat(Name, ": is hungry")},
  83. LFork ! {take_left, {self(), Name}},
  84. RFork ! {take_right, {self(), Name}},
  85. receive
  86. {l_fork, LFork} -> philosopher(has_left, Name, LFork, RFork, Report);
  87. {r_fork, RFork} -> philosopher(has_right, Name, LFork, RFork, Report)
  88. end;
  89.  
  90. philosopher(has_left, Name, LFork, RFork, Report) ->
  91. RFork ! {take_right, {self(), Name}},
  92. receive
  93. {r_fork, _ForkPID} -> philosopher(eating, Name, LFork, RFork, Report)
  94. end;
  95.  
  96. philosopher(has_right, Name, LFork, RFork, Report) ->
  97. LFork ! {take_left, {self(), Name}},
  98.  
  99. receive
  100. {l_fork, _ForkPID} -> philosopher(eating, Name, LFork, RFork, Report)
  101. end;
  102.  
  103. philosopher(eating, Name, LFork, RFork, Report) ->
  104. Report ! {hungry, string:concat(Name, ": is eating")},
  105. timer:sleep(random:uniform(901)+99),
  106. LFork ! {put_left, {self(), Name}},
  107. RFork ! {put_right, {self(), Name}},
  108. philosopher(thinking, Name, LFork, RFork, Report).
  109.  
  110. waitdone (0) -> finished ;
  111. waitdone (N) -> receive finished -> waitdone (N -1) end.
  112.  
  113.  
  114.  
  115.  
  116.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
dinphil:college().
compilation info
/home/wV1pPi/prog.beam: Module name 'dinphil' does not match file name 'prog'
./prog.erl:14: Warning: variable 'Philo1' is unused
./prog.erl:15: Warning: variable 'Philo2' is unused
./prog.erl:16: Warning: variable 'Philo3' is unused
./prog.erl:17: Warning: variable 'Philo4' is unused
./prog.erl:18: Warning: variable 'Philo5' is unused
./prog.erl:38: Warning: variable 'Isavail' is unused
./prog.erl:73: Warning: variable 'Seed' is unused
stdout
Standard output is empty