-
Notifications
You must be signed in to change notification settings - Fork 0
/
dining_simple_bug.erl
105 lines (93 loc) · 2.95 KB
/
dining_simple_bug.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
-module(dining_simple_bug).
-export([main/0, waiter/0, fork/1, philo/2]).
main() ->
spawn(?MODULE, waiter, []).
spawn_forks(0, Dict) ->
Dict;
spawn_forks(N, Dict) ->
Pair = {N, spawn(?MODULE, fork, [free])},
spawn_forks(N-1, [Pair] ++ Dict).
spawn_philos(0, Dict, _) ->
Dict;
spawn_philos(N, Dict, Pid) ->
Pair = {spawn(?MODULE, philo, [Pid, N]), N},
spawn_philos(N-1, [Pair] ++ Dict, Pid).
waiter() ->
ForkDict = spawn_forks(5, []),
PhiloDict = spawn_philos(5, [], self()),
waiter_1(ForkDict, PhiloDict).
waiter_1(ForkDict, PhiloDict) ->
receive
{eaten, PhiloPid} ->
PhiloId = proplists:get_value(PhiloPid, PhiloDict),
LeftForkId = PhiloId,
RightForkId = 1 + (LeftForkId rem 5),
LeftPid = proplists:get_value(LeftForkId, ForkDict),
RightPid = proplists:get_value(RightForkId, ForkDict),
set_state(LeftPid, free),
set_state(RightPid, free);
{hungry, PhiloPid} ->
PhiloId = proplists:get_value(PhiloPid, PhiloDict),
LeftForkId = PhiloId,
RightForkId = 1 + (LeftForkId rem 5),
LeftPid = proplists:get_value(LeftForkId, ForkDict),
RightPid = proplists:get_value(RightForkId, ForkDict),
LeftForkState = ask_state(LeftPid),
RightForkState = ask_state(RightPid),
case {LeftForkState, RightForkState} of
{free, free} ->
set_state(LeftPid, used),
set_state(RightPid, used),
PhiloPid ! eat;
_ ->
PhiloPid ! think
end
end,
waiter_1(ForkDict, PhiloDict).
ask_state(Pid) ->
Pid ! {get_state, self()},
receive
% {state, State, _} -> State % Correct version
State -> State % Buggy version
end.
set_state(Pid, State) ->
Pid ! {set_state, State, self()},
receive
% {been_set, _} -> ok % Correct version
been_set -> ok % Buggy version
end.
philo(WaiterPid, PhiloId) ->
think(PhiloId),
request_until_eaten(WaiterPid, PhiloId),
philo(WaiterPid, PhiloId).
think(PhiloId) ->
io:fwrite("Philo " ++ integer_to_list(PhiloId) ++ " is thinking~n"),
ThinkTime = rand:uniform(1000),
timer:sleep(ThinkTime),
ok.
eat(PhiloId) ->
io:fwrite("Philo " ++ integer_to_list(PhiloId) ++ " has eaten~n"),
timer:sleep(1000),
ok.
request_until_eaten(WaiterPid, PhiloId) ->
io:fwrite("Philo " ++ integer_to_list(PhiloId) ++ " is hungry~n"),
WaiterPid ! {hungry, self()},
receive
think ->
think(PhiloId),
request_until_eaten(WaiterPid, PhiloId);
eat ->
eat(PhiloId),
WaiterPid ! {eaten, self()}
end.
fork(State) ->
receive
{get_state, WaiterPid} ->
% WaiterPid ! {state, State, self()}, % Correct version
WaiterPid ! State, % Buggy version
fork(State);
{set_state, NewState, WaiterPid} ->
% WaiterPid ! {been_set, self()}, % Correct version
WaiterPid ! been_set, % Buggy version
fork(NewState)
end.