0
我想編寫創建2個進程的程序。 第一個打印號碼,發送到第二個進程等。 但是我在啓動功能有問題。Erlang,寄存器/ 2:異常錯誤壞參數
-module(zad).
-export([start/0, one/0, two/0]).
one() ->
receive
{go1, N} ->
io:format("1 ~w~n", [N]),
two ! {go2, N+1},
one();
{go2, 50} ->
io:format("END ~w~n", [N+1]),
two ! finished
end.
two() ->
receive
finished ->
io:format("two finished~n", []);
{go2, N} ->
io:format("2 ~w~n", [N]),
one ! {go1, N+1 },
two()
end.
start() ->
register(two, spawn(zad, two, [])),
register(one, spawn(zad, one, [])),
one ! {go1, 0}.
這裏的錯誤
** exception error: bad argument
in function register/2
called as register(two,<0.146.0>)
in call from zad:start/0 (zad.erl, line 29)
這是否意味着我不能註冊在啓動2個進程?這段代碼有什麼問題?
UPDATE 這真的很奇怪。但我改變
{go2, 50} -> io:format("END ~w~n", [N+1]),
後
{go2, 50} -> io:format("END ~w~n", [50]),
錯誤消失。 這真的很奇怪,因爲問題出在one()
,但錯誤出現在register(two)
也許在'two/0'也有語法錯誤?也許'two/0'中的變化沒有被編譯,當你重新編譯爲'one/0'時,它被修正了嗎? – Stratus3D
您嘗試過多少次運行此操作?如果您再次運行它而不註銷名稱,那麼當您嘗試重新註冊名稱時,如果名稱已被使用,則會出現錯誤。 – rvirding