Mapping processes

Vladimir Sekissov svg@REDACTED
Tue Dec 17 20:50:31 CET 2002


Good day,

eduardo> What do you think about using register(reqId, PID) and
eduardo> whereis(reqId) to  
eduardo> retrieve the PID so to send the response, in an async. way,
eduardo> to the calling process. 

I think you could easily exceed the atoms table limit on long living
system. Using simple dictionary of {callerPid, reqId} and trapping
exit messages from callers to delete records from dictionary would be
better solution. May be like this or as gen_server:

create_dict() ->
  spawn_link(fun dict_srv/0).

remove_dict(DPid) ->
  DPid ! {self(), stop},
  receive
    {DPid, stopped} ->
      ok
  end.

dict_add(DPid, Pid, Reg) ->
  DPid ! {self(), add, {Pid, Reg}}.

dict_get(DPid, Reg) ->
  DPid ! {self(), get, Reg},
  receive
    {DPid, {value, {Pid, _}}} ->
      Pid;
    {DPid, false} ->
      undefined 
  end.
    
dict_srv() ->
  process_flag(trap_exit, true),
  dict_loop([]).

dict_loop(Dict) ->
  receive
    {_From, add, V={Pid, Reg}} ->
      link(Pid),
      dict_loop([V|Dict]);
    {From, get, Reg} ->
      From ! {self(), lists:keysearch(Reg, 2, Dict)},
      dict_loop(Dict);
    {'EXIT', Pid, Reson} ->
      case lists:keymember(Pid, 1, Dict) of
	true ->
	  dict_loop(lists:keydelete(Pid, 1, Dict));
	false ->
	  exit(Reson)
      end;
    {From, stop} ->
      From ! {self(), stopped},
      ok
  end.

Best Regards,
Vladimir Sekissov

eduardo> 
eduardo> I'm thinking how to map processes with request Id so to manage
eduardo> callbacks when using ports.
eduardo> What do you think about using register(reqId, PID) and whereis(reqId) to 
eduardo> retrieve the PID so to send the response, in an async. way, to the calling process.
eduardo> 
eduardo> 
eduardo> 
eduardo> Thanks,
eduardo> Eduardo Figoli
eduardo> INSwitch Solutions
eduardo> 
eduardo> 
eduardo> 
eduardo> 



More information about the erlang-questions mailing list