Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@googya
Created June 12, 2019 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save googya/f9dd03942ef4acdc35ec2dd618c8573b to your computer and use it in GitHub Desktop.
Save googya/f9dd03942ef4acdc35ec2dd618c8573b to your computer and use it in GitHub Desktop.
-module(server1).
-export([start/2, rpc/2]).
start(Name, Mod) ->
register(Name, spawn(fun() -> loop(Name, Mod, Mod:init()) end)).
rpc(Name, Request) ->
io:format(" self at rpc is ~p~n", [self()]),
io:format(" Name at rpc is ~p~n", [Name]),
Name ! { self(), Request },
receive
{ Name, Response } ->
io:format(" self is ~p~n", [self()]),
Response
end.
loop(Name, Mod, State) ->
io:format(" self at loop is ~p~n", [self()]),
receive
{ From, Request } ->
io:format(" From in loop is ~p~n", [From]),
{ Response, State1 } = Mod:handle(Request, State),
From ! { Name, Response},
loop(Name, Mod, State1)
end.
-module(name_server).
-export([init/0, add/2, find/1, handle/2]).
-import(server1, [rpc/2]).
add(Name, Place) ->
io:format(" self in add is ~p~n", [self()]),
rpc(name_server, { add, Name, Place}).
find(Name) ->
io:format(" self at find is ~p~n", [self()]),
rpc(name_server, {find, Name}).
init() -> dict:new().
handle({add, Name, Place}, Dict) ->
io:format(" self at handle add is ~p~n", [self()]),
{ok, dict:store(Name, Place, Dict)};
handle({find, Name}, Dict) ->
io:format(" self at handle find is ~p~n", [self()]),
{dict:find(Name, Dict), Dict}.
@googya
Copy link
Author

googya commented Jun 12, 2019

使用以下命令查看进程信息

observer:start()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment