3
次のErlangコードをUbuntu Serverマシンで実行するとエラーが発生します。Solusを実行しているマシンで正しく動作します。私はErlangを初めて使用しており、他のすべての例はエラーコード内に1つの関数とモジュールしか持たないので、エラーの読み方がわかりません。終了値Undef Erlang Spawn
-module(sensor).
-export([start/2, senseAndReport/3]).
start(WatcherPid, SensorId) ->
Ref = make_ref(),
senseAndReport(WatcherPid, SensorId, Ref).
senseAndReport(WatcherPid, SensorId, Ref) ->
Measurement = rand:uniform(11),
if
Measurement == 11 ->
WatcherPid ! {kill, {self(), SensorId}, error},
exit(error);
true ->
WatcherPid ! {Measurement, {self(), SensorId}, Ref}
end,
receive
{ok, Ref} ->
Sleep_time = rand:uniform(10000),
timer:sleep(Sleep_time),
senseAndReport(WatcherPid, SensorId, Ref)
end.
ウォッチャー:
-module(watcher).
-export([start/1, createSensor/3, restartASensor/2, watch/1]).
start(NumOfSensor) ->
if
NumOfSensor == 0 ->
io:format("Please enter a number greater than 0.~n");
true ->
createSensor(NumOfSensor, [], 0)
end.
createSensor(NumOfSensor, SensorList, SensorId) ->
if
length(SensorList) == 10 ->
io:format("Start watching:~n"),
[io:format(" Id: ~p, Pid: ~p~n", [Id, Pid]) || {Id, Pid} <- SensorList],
if NumOfSensor /= 0 ->
spawn(watcher, createSensor, [NumOfSensor, [], SensorId]),
watch(SensorList);
true ->
watch(SensorList)
end;
NumOfSensor == 0 ->
io:format("Start watching~n"),
[io:format(" Id: ~p, Pid: ~p~n", [Id, Pid]) || {Id, Pid} <- SensorList],
watch(SensorList);
true ->
SensorPid = spawn_monitor(sensor, start, [self(), SensorId]),
createSensor(NumOfSensor - 1, lists:merge(SensorList, [{SensorId, SensorPid}]), SensorId + 1)
end.
restartASensor(SensorList, SensorId) ->
{SensorPid, _} = spawn_monitor(sensor, start, [self(), SensorId]),
io:format(" Restarted sensor: ~p, new Pid: ~p~n", [SensorId, SensorPid]),
NewList = lists:merge(SensorList, [{SensorId, SensorPid}]),
watch(NewList).
watch(SensorList) ->
receive
{kill, {From, FromId}, error} ->
io:format(" Sensor ~p died~n", [FromId]),
restartASensor(lists:delete({FromId, From}, SensorList), FromId);
{Measurement, {From, FromId}, Ref} ->
io:format("MSG: ~2p, From sensor ~4p~n", [Measurement, FromId]),
From ! {ok, Ref},
watch(SensorList)
end.
これは私に次の出力を与える:
Eshell V5.8.5 (abort with ^G)
1> c(watcher).
{ok,watcher}
2> watcher:start(1).
Start watching
Id: 0, Pid: <0.39.0>
=ERROR REPORT==== 18-Nov-2016::19:32:35 ===
Error in process <0.39.0> with exit value: {undef,[{rand,uniform,[11]},{sensor,senseAndReport,3}]}
'rand'モジュールが最近追加されました。あなたはUbuntuマシン上でErlangの古いバージョンをSolusのマシンよりも動かしていますか? – Dogbert
@Dogbertそれでした!代わりにランダムが必要です。本当にありがとう。 – spicelord