<table class="text">
<tr class="li1"><td class="ln"><pre class="de1">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 loop(name,pid) #收到注册信息,当下一个进程是根部时,会将下个进程修改为新生成的进程 reg = {:register,_pid} -> #若不是,则会将注册信息发送给下个进程 send(next_ticker,reg) {:tick,pid_name} -> #收到tick IO.puts "#{name}:#{inspect self} tick received from #{String.strip(pid_name)}" #输出 :timer.sleep(@interval) #休眠2秒 send(next_ticker,name}) #将tick发送给下个进程 end loop(name,next_ticker) #继续调用loop end def get_root do :global.whereis_name(@name) end def join(name) do pid = spawn(__MODULE__,[name,get_root]) send(get_root,{:register,pid}) end end #因为休眠时间的缘故,在下一次经过root后,join才会生效