-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat.erl
76 lines (67 loc) · 2.1 KB
/
stat.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
-module(stat).
-export([start/1, center/1, first/0, second/0,third/0,fourth/0]).
center(0) ->
first ! finished ,
second ! finished,
third!finished,
fourth!finished,
io:format("~p P0 finished~n", [self()]);
center(N) ->
first ! {center, self()},
second ! {center,self()},
third ! {center,self()},
fourth ! {center,self()},
receive
{first,F_PID} ->
io:format("~p P0 received ~p P1~n", [self(),F_PID]);
{second,S_PID} ->
io:format("~p P0 received ~p P2 ~n",[self(),S_PID]);
{third,T_PID} ->
io:format("~p P0 received ~p P3 ~n",[self(),T_PID]);
{fourth,FO_PID} ->
io:format("~p P0 received ~p P4 ~n",[self(),FO_PID])
end,
center(N - 1).
first() ->
receive
finished ->
io:format("~p P1 finished~n", [self()]);
{center, Center_PID} ->
io:format("~p P1 received ~p P0~n", [self(),Center_PID]),
Center_PID ! {first,self()},
first()
end.
second() ->
receive
finished ->
io:format("~p P2 finished~n", [self()]);
{center, Center_PID} ->
io:format("~p P2 received ~p P0~n", [self(),Center_PID]),
Center_PID ! {second,self()},
second()
end.
third() ->
receive
finished ->
io:format("~p P3 finished~n", [self()]);
{center, Center_PID} ->
io:format("~p P3 received ~p P0~n", [self(),Center_PID]),
Center_PID ! {third,self()},
third()
end.
fourth() ->
receive
finished ->
io:format("~p P4 finished~n", [self()]);
{center, Center_PID} ->
io:format("~p P4 received ~p P0~n", [self(),Center_PID]),
Center_PID ! {fourth,self()},
fourth()
end.
start(A) ->
register(first, spawn(stat, first, [])),
register(second, spawn(stat, second, [])),
register(third, spawn(stat, third, [])),
register(fourth, spawn(stat, fourth, [])),
CID = spawn(stat, center, [A]),
io:format("~p centre(P0) started~n", [CID]).