Advertisement
Guest User

Linking 3 process

a guest
Mar 8th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 1.34 KB | None | 0 0
  1. -module(newbie).
  2. -compile([export_all]).
  3.  
  4. starter() ->
  5.   Pid = spawn(?MODULE, the_first_p, []),
  6.   register(pidfirstp, Pid).
  7.  
  8. the_first_p() ->
  9.   IsPidExists = whereis(pidsecondp),
  10.   if IsPidExists =:= undefined ->
  11.     Pid = spawn_link(?MODULE, the_second_p, []),
  12.     register(pidsecondp, Pid);
  13.     true -> true
  14.   end,
  15.   receive
  16.     {From, "to first p", Msg} ->
  17.       From ! {self(), "the_first_p()", Msg}
  18.   end,
  19.   the_first_p().
  20.  
  21. the_second_p() ->
  22.   IsPidExists = whereis(pidthirdp),
  23.   if IsPidExists =:= undefined ->
  24.     Pid = spawn_link(?MODULE, the_third_p, []),
  25.     register(pidthirdp, Pid);
  26.     true -> true
  27.   end,
  28.   receive
  29.     {From, "to second p", Msg} ->
  30.       From ! {self(), "the_second_p()", Msg}
  31.   end,
  32.   the_second_p().
  33.  
  34. the_third_p() ->
  35.   receive
  36.     {From, "to third p", Msg} ->
  37.       From ! {self(), "the_third_p()", Msg}
  38.   end,
  39.   the_third_p().
  40.  
  41. call_the_first() ->
  42.   pidfirstp ! {self(), "to first p", "Halo first p!"},
  43.   receive
  44.     {Pid, FuncName, Msg} ->
  45.       {Pid, FuncName, Msg}
  46.   end.
  47.  
  48. call_the_second() ->
  49.   pidsecondp ! {self(), "to second p", "Halo second p!"},
  50.   receive
  51.     {Pid, FuncName, Msg} ->
  52.       {Pid, FuncName, Msg}
  53.   end.
  54.  
  55. call_the_third() ->
  56.   pidthirdp ! {self(), "to third p", "Halo third p!"},
  57.   receive
  58.     {Pid, FuncName, Msg} ->
  59.       {Pid, FuncName, Msg}
  60.   end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement