[root@han erlangsrc]# cat codereload.erl
-module(codereload).
-export([main/0, master_loop/2, worker_loop/0]).
-define(VERSION, "0.2").
main() ->
process_flag(trap_exit, true),
Pid1 = spawn(?MODULE, worker_loop, []),
Pid2 = spawn(?MODULE, worker_loop, []),
spawn(fun() -> register(master, self()), master_loop(Pid1, Pid2) end).
master_loop(Pid1, Pid2) ->
io:format("Pid1, Pid2 is alive ~p ~p~n",[is_process_alive(Pid1), is_process_alive(Pid2)]),
receive
refresh ->
io:format("Master code reload~n"),
Pid1 ! refresh,
Pid2 ! refresh,
codereload:master_loop(Pid1, Pid2);
Any ->
io:format("Master ~p receive message: ~p~n", [?VERSION, Any]),
Pid1 ! Any,
Pid2 ! Any,
master_loop(Pid1, Pid2)
end.
worker_loop() ->
receive
refresh ->
io:format("Worker code ~p reload~n", [self()]),
codereload:worker_loop();
Any ->
io:format("Worker ~p at ~p also receive message: ~p~n", [?VERSION, self(), Any]),
worker_loop()
end.
[root@han erlangsrc]# erl -sname foo@localhost
Erlang/OTP 22 [erts-10.7.2.1] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [hipe]
Eshell V10.7.2.1 (abort with ^G)
(foo@localhost)1> c(codereload).
{ok,codereload}
(foo@localhost)2> codereload:main().
Pid1, Pid2 is alive true true
<0.94.0>
Master "0.2" receive message: "HI"
Pid1, Pid2 is alive true true
Worker "0.2" at <0.92.0> also receive message: "HI"
Worker "0.2" at <0.93.0> also receive message: "HI"
(foo@localhost)3>
(foo@localhost)3> c(codereload).
{ok,codereload}
(foo@localhost)4> {master, 'foo@localhost'} ! refresh.
Master code reload
refresh
Pid1, Pid2 is alive true true
Worker code <0.92.0> reload
Worker code <0.93.0> reload
(foo@localhost)5>
Master "0.1" receive message: "HI"
Pid1, Pid2 is alive true true
Worker "0.1" at <0.92.0> also receive message: "HI"
Worker "0.1" at <0.93.0> also receive message: "HI"
(foo@localhost)5>
(foo@localhost)5> {master, 'foo@localhost'} ! "HI".
Master "0.1" receive message: "HI"
"HI"
Pid1, Pid2 is alive true true
Worker "0.1" at <0.92.0> also receive message: "HI"
Worker "0.1" at <0.93.0> also receive message: "HI"
(foo@localhost)6>
(foo@localhost)6> <0.94.0> ! "HI".
Master "0.1" receive message: "HI"
"HI"
Pid1, Pid2 is alive true true
Worker "0.1" at <0.92.0> also receive message: "HI"
Worker "0.1" at <0.93.0> also receive message: "HI"
(foo@localhost)7>