Meregister proses

Kode berikut ini adalah contoh bagaimana meregister sebuah proses.

-module(echo2).
-export([go/0, loop/0]).

go() ->
register(echo, spawn(echo, loop, [])),
echo ! {self(), hello},
receive
{_Pid, Msg} ->
io:format(“~w~n”,[Msg])
end.

loop() ->
receive
{From, Msg} ->
From ! {self(), Msg},
loop();
stop ->
true
end.

Pid proses akan disimpan dalam VM Erlang dan dirujuk ke suatu alias. Kita bisa memanggil alias itu dari mana saja. Untuk mengetahui Pid kita bisa panggil whereis(alias). Untuk mengetahui list yang teregister kita bisa memakai regs().

Kaitkata:

Tinggalkan Balasan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Ubah )

Twitter picture

You are commenting using your Twitter account. Log Out / Ubah )

Facebook photo

You are commenting using your Facebook account. Log Out / Ubah )

Connecting to %s


Ikuti

Get every new post delivered to your Inbox.