2

我有一个进程,许多线程都链接到该进程,以便将自己与该进程相关联。我以这种方式制作系统,以便在任何时候我都可以轻松查看谁与中央进程相关联,而不必跟踪应用程序本身内部的任何列表。我可以简单地做process_info(self(),links),而 erlang 会跟踪哪些进程仍然存在或不存在,等等......

至少,我是这么认为的,直到我发现这个线程上返回的列表此时不准确:

% P is my central pid
(node@host)212> P.
<0.803.0>

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

% Counting out all the links
(node@host)214> length(L).
31154

% Filtering out all of the dead processes, put them in Lf
(node@host)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!
(node@host)216> length(Lf).
15654

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

我能想到的唯一会导致这种情况的是网络连接问题,因为某些链接可能来自另一台机器上节点上的线程。这是一个可能的解释吗?有没有办法让线程清理它的链接列表?

4

1 回答 1

0

这是我的错。这实际上是我如何检查进程是否已死的问题。process_info 不适用于远程节点上的线程。哦!

于 2013-02-20T03:50:21.637 回答