2012-11-24 45 views
0

遠程pid註冊其"local pid""its node"名稱已註冊到mnesia表中。如果另一個節點的pid讀取上面的"local pid<0.xxx.xxx>"和節點名稱,如何轉換爲remote pid<xxx.xxx.xxx>給出本地pid和節點名稱,如何轉換爲遠程pid?

+2

如果一個進程本地註冊在節點上,你能不能:rpc:call('node @ remote-host',erlang,whereis,[localpid])來檢索遠程pid? – 2012-11-24 11:00:07

+0

whereis的參數是regname,不是localpid。 –

回答

4

我真的沒有看到爲什麼你必須做任何特別的事情。在這裏,我將創建一個運行在[email protected][email protected]上的mnesia表,並且從各個節點寫入和讀取pid,並顯示它們不需要「轉換」pid作爲Erlang的分發機制自動照顧它。這一切都會持續很長時間。

節點[email protected]我們連接或其它節點首先創建我們Mnesia模式:

Eshell V5.9.1 (abort with ^G) 
([email protected])1> net_adm:ping([email protected]). 
pong 
([email protected])3> mnesia:create_schema([node()|nodes()]). 
ok 
([email protected])5> mnesia:start(). 
ok 

在節點[email protected]我們剛開始的Mnesia:

Eshell V5.9.1 (abort with ^G) 
([email protected])1> mnesia:start(). 
ok 

在節點[email protected]我們再創建一個表並插入包含本地條目(至[email protected])pid:

([email protected])6> mnesia:create_table(local, [{attributes,[node,pid]},{ram_copies,[node()|nodes()]}]). 
{atomic,ok} 
([email protected])8> mnesia:transaction(fun() -> mnesia:write({local,node(),self()}) end). 
{atomic,ok} 

請注意,我沒有定義記錄,但顯式讀/寫記錄元組。你可以這樣做,但最好使用記錄。現在,在節點[email protected]我們可以看到相同的記錄:

([email protected])10> {atomic,[Lr]} = mnesia:transaction(fun() -> mnesia:read(local, [email protected]) end). 
{atomic,[{local,[email protected],<5893.37.0>}]} 
([email protected])11> node(element(3, Lr)). 
[email protected] 
([email protected])12> mnesia:transaction(fun() -> mnesia:write({local,node(),self()}) end). 
{atomic,ok} 

,我們看到,PID指進程另一個節點上(),節點[email protected]。然後我們向包含當地(至[email protected])pid的表格添加了一條記錄。如果我們接着往下看節點[email protected]該記錄我們看到,它包含節點[email protected]進程的PID:

([email protected])13> {atomic,[Rp]} = mnesia:transaction(fun() -> mnesia:read(local, [email protected]) end). 
{atomic,[{local,[email protected],<6007.85.0>}]} 
([email protected])14> node(element(3,Rp)). 
[email protected] 

所以我們看到,一個「轉換」是沒有必要的。

實際上任何形式的「轉換」其實都是毫無意義的。 pid是指特定節點上的特定進程,因此試圖將其轉換爲無意義。它是一個原子數據類型。即使你可以轉換它,你也不知道它將實際引用什麼進程,如果有的話。

+0

非常感謝。我現在明白了「自動轉換」。 –