2013-02-18 53 views
2

我有一個進程,大量的線程鏈接到自己與這個過程。我以這種方式製作系統,以便在任何時候我都可以輕鬆查看誰與中央進程相關聯,而無需跟蹤應用程序本身內的任何列表。我可以簡單地做process_info(self(),links)和erlang記錄哪些進程仍然存在或沒有,等等......Erlang:進程保持鏈接到死亡線程

至少,我這麼認爲,直到我發現列表在這個線程返回不準確在這一刻:

% P is my central pid 
([email protected])212> P. 
<0.803.0> 

% Get all the links to it 
([email protected])213> {links,L} = process_info(P,links). 
{links,[<0.29179.155>,<0.6492.250>,<0.29990.293>|...]} 

% Counting out all the links 
([email protected])214> length(L). 
31154 

% Filtering out all of the dead processes, put them in Lf 
([email protected])215> Lf = lists:filter(fun(Pid) -> try process_info(Pid,links) of {links,_} -> true; _ -> false catch _:_ -> false end end, L). 
[<0.29179.155>,<0.6492.250>,<0.29990.293>,<0.23619.530>|...] 

% Lf is only about half the size, half the linked processes are dead! 
([email protected])216> length(Lf). 
15654 

% Proof that the links haven't changed in the interim 
([email protected])217> {links,L} = process_info(P,links). 
{links,[<0.29179.155>,<0.6492.250>,<0.29990.293>|...]} 

我能想到的唯一的事情就是因爲這是網絡連接問題,因爲有些鏈接可能來自線程另一臺機器上的一個節點上。這是一個可能的解釋嗎?有沒有辦法讓線程清理鏈接列表?

回答

0

這是我的錯。這實際上是我如何檢查進程是否死亡的問題。 process_info不適用於遠程節點上的線程。 D'哦!